C#实现把txt文本数据快速读取到excel中

主要介绍了C#实现把txt文本数据快速读取到excel中,本文直接给出示例代码,需要的朋友可以参考下

今天预实现一功能,将txt中的数据转到excel表中,做为matlab的数据源。搜集一些c#操作excel的程序。步骤如下:

下载一个Microsoft.Office.Interop.Excel.dll 在项目中引用。

编写代码如下:

string path = “c://date//xyu.txt”;

StreamReader sr = new StreamReader(path);

string strLine = sr.ReadLine();

int rowNum = 1;

object missing = System.Reflection.Missing.Value;

ApplicationClass app = new ApplicationClass();

app.Application.Workbooks.Add(true);

Workbook book = (Workbook)app.ActiveWorkbook;

Worksheet sheet = (Worksheet)book.ActiveSheet;

while (!string.IsNullOrEmpty(strLine))

{

string[] tempArr;

tempArr = strLine.Split(‘,’);

for (int k = 1; k <= tempArr.Length; k++)

{

sheet.Cells[rowNum, k] = tempArr[k - 1];

}

strLine = sr.ReadLine();<

猜你喜欢

转载自blog.csdn.net/weixin_44301520/article/details/130397912