其他分享
首页 > 其他分享> > 通过execle读取txt文档

通过execle读取txt文档

作者:互联网

class ExcleReadTxt
{
public void ExcleRead(string testFilePath,string testFile,out List<List<string>> getData, out int row,out int column)
{
Application excle = new Application();//打开EXCLE程序
if (excle == null)
{
MessageBox.Show("未能正确打开程序!");
}
Workbooks workbooks = excle.Workbooks;//定义工作簿集合
excle.Visible = true;
XlTextQualifier xlTextQualifier = (XlTextQualifier)1;
workbooks.OpenText(testFilePath(文件夹路径)+"\\"+testFile(文件名)+".txt", 936,System.Type.Missing, System.Type.Missing, xlTextQualifier, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
Workbook workbook = workbooks.Item[testFile];
string FileFormat = workbook.FileFormat.ToString();
Worksheet worksheet = workbook.Sheets.Item[1];
int count = worksheet.UsedRange.Count;
row = worksheet.UsedRange.Rows.Count;
column = worksheet.UsedRange.Columns.Count;
List<string> getDataTemp;
getData = new List<List<string>>();
var data =worksheet.UsedRange.Value;
if (count == 1)
{
getData[0].Add(Convert.ToString(data));
}
else if (count >= 2)
{
for (int i = 0; i < row; i++)
{
getDataTemp = new List<string>();
getData.Add(getDataTemp);
for (int j = 0; j < column; j++)
{
getData[i].Add(Convert.ToString(data[i + 1, j + 1]));
}
}
getData.RemoveAt(0);
}
else
{
getData = null;
}
workbook.Close();
workbooks.Close();
excle.Visible = false;
}
}

标签:getData,Missing,int,worksheet,execle,System,文档,txt,Type
来源: https://www.cnblogs.com/ytlmj290/p/15896531.html