TXT导到EXCEL 比如TXT文件:aaa,bbb,ccc,ddd要求导到EXCEL中的4列中 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以把txt当做数据库来使用excel操作:using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.OleDb;using System.Data.SqlClient;using System.IO;using Microsoft.Office.Interop.Excel;namespace TestAccess{ class Program { static void Main(string[] args) { string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;"; strConnection += @"Data Source=C:\Documents and Settings\v-changl\My Documents\couse.xlsx;"; strConnection += "Extended Properties=\"Excel 12.0 Xml;HDR=YES\";"; OleDbConnection objConnection = new OleDbConnection(strConnection); objConnection.Open(); OleDbDataAdapter myCommandd = new OleDbDataAdapter("select * from [Sheet1$]", objConnection); DataSet ds = new DataSet(); myCommandd.Fill(ds, "[Sheet1$]"); System.Data.DataTable dt = ds.Tables["[Sheet1$]"]; Console.WriteLine(dt.Columns[0].ToString()); Console.WriteLine(dt.Columns[1].ToString()); DataRow drDisplay = dt.Rows[0]; int[] num = new int[dt.Columns.Count]; for (int j = 0; ; ) { for (int i = 0; i < dt.Columns.Count; i++) { if (drDisplay[i] is DBNull) ; else num[i] += Convert.ToInt32(drDisplay[i]); } if (++j >= dt.Rows.Count) break; drDisplay = dt.Rows[j]; } objConnection.Close(); object MissingValue = Type.Missing; Microsoft.Office.Interop.Excel.Application app = new Application(); Microsoft.Office.Interop.Excel.Workbook wbook = app.Workbooks.Open(@"C:\Documents and Settings\v-changl\My Documents\couse.xlsx", MissingValue,MissingValue, MissingValue, MissingValue,MissingValue, MissingValue, MissingValue,MissingValue, MissingValue, MissingValue,MissingValue, MissingValue, MissingValue,MissingValue); Microsoft.Office.Interop.Excel.Worksheet wsheet = wbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet; for (int i = 0; i < dt.Columns.Count; i++) { //注意下面是i+1,,excel小标默认从1开始 wsheet.Cells[dt.Rows.Count + 2, i + 1] = num[i].ToString(); } wbook.Save(); wbook.Close(true, null, null); System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); wsheet = null; wbook = null; app = null; GC.Collect(); } }}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LCL_data/archive/2009/05/06/4154784.aspxtxt操作:用txt做数据源连接字符串//text作为数据源的链接字符串 string strConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test;Extended Properties=""text;HDR=Yes;FMT=Delimited"";";注意:c:\\test是个文件夹,里面有相应的txt文档做数据源。 string[] arr=File.ReadAllLine(""); 遍历或数组写入excelExcel.Range range = myworksheet.get_Range(myworksheet.Cells[1, 1], myworksheet.Cells[rownum + 4, colnum]); range.Value2 = arr; 关于datagridview的几个问题 Timer和MouseWheel冲突? C#中的扩展类带参数构造函数不会调用基类中相同的构造函数么? C#中有类似MEMSET的方法吗? p2p连接的问题,对你的挑战有多大?分不够坚决散 添加服务引用的应用程序不能运行 我做桌面程序遇到的问题。 如何绑定 ? 在线帮助!(初学的困惑!) 如何处理这个问题 文件夹右键菜单问题 急急@!!@#¥%……&*
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using Microsoft.Office.Interop.Excel;
namespace TestAccess
{
class Program
{
static void Main(string[] args)
{ string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";
strConnection += @"Data Source=C:\Documents and Settings\v-changl\My Documents\couse.xlsx;";
strConnection += "Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
OleDbConnection objConnection = new OleDbConnection(strConnection);
objConnection.Open();
OleDbDataAdapter myCommandd = new OleDbDataAdapter("select * from [Sheet1$]", objConnection);
DataSet ds = new DataSet();
myCommandd.Fill(ds, "[Sheet1$]");
System.Data.DataTable dt = ds.Tables["[Sheet1$]"];
Console.WriteLine(dt.Columns[0].ToString());
Console.WriteLine(dt.Columns[1].ToString());
DataRow drDisplay = dt.Rows[0];
int[] num = new int[dt.Columns.Count];
for (int j = 0; ; )
{
for (int i = 0; i < dt.Columns.Count; i++)
{ if (drDisplay[i] is DBNull) ;
else
num[i] += Convert.ToInt32(drDisplay[i]); }
if (++j >= dt.Rows.Count) break;
drDisplay = dt.Rows[j];
}
objConnection.Close();
object MissingValue = Type.Missing;
Microsoft.Office.Interop.Excel.Application app = new Application();
Microsoft.Office.Interop.Excel.Workbook wbook = app.Workbooks.Open(@"C:\Documents and Settings\v-changl\My Documents\couse.xlsx", MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue);
Microsoft.Office.Interop.Excel.Worksheet wsheet = wbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
for (int i = 0; i < dt.Columns.Count; i++)
{
//注意下面是i+1,,excel小标默认从1开始
wsheet.Cells[dt.Rows.Count + 2, i + 1] = num[i].ToString();
} wbook.Save();
wbook.Close(true, null, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
wsheet = null;
wbook = null;
app = null;
GC.Collect();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LCL_data/archive/2009/05/06/4154784.aspxtxt操作:
用txt做数据源连接字符串//text作为数据源的链接字符串
string strConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test;
Extended Properties=""text;HDR=Yes;FMT=Delimited"";";注意:c:\\test是个文件夹,里面有相应的txt文档做数据源。
遍历或数组写入excel
Excel.Range range = myworksheet.get_Range(myworksheet.Cells[1, 1], myworksheet.Cells[rownum + 4, colnum]);
range.Value2 = arr;