主要介绍了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();<