这种表头不在第一行的,怎么读取啊?
8 回答
郎朗坤
TA贡献1921条经验 获得超9个赞
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace ExcelReader
{
public class ExcelHelper
{
private OleDbConnection conn = new OleDbConnection();
public ExcelHelper(string excelPath)
{
string connectionStr = string.Format(ConfigurationManager.AppSettings["ConnectionString"], excelPath);
conn.ConnectionString = connectionStr;
}
public DataTable GetExcelTable()
{
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from [Sheet1$]";
DataTable table = new DataTable();
OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
oda.Fill(table);
foreach (DataRow row in table.Rows)
{
if (!(row[0].ToString()).Equals("交易日期"))
{
row.Delete();
}
else
{
break;
}
}
for (int i = 0; i < table.Columns.Count; i++)
{
table.Columns[i].ColumnName = table.Rows[0][i].ToString();
}
table.Rows[0].Delete();
table = table.GetChanges(DataRowState.Unchanged);
return table;
}
}
}
经过实验,可以获取数据表~
下面是连接字符串:
- 8 回答
- 0 关注
- 1257 浏览
添加回答
举报
0/150
提交
取消