【发布时间】:2016-08-29 06:26:26
【问题描述】:
我需要在 ASP.NET MVC 中导入 excel 文件。导入正在根据需要进行,excel 文件中有 78 列。除最后一列外,所有列均已导入。
如何从 excel 文件中检索最后一列?
string conString = string.Empty;
if (getFileExtension.ToLower() == ".xls")
{
conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + getFileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; ;
}
else if (getFileExtension.ToLower() == ".xlsx")
{
conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + getFileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection con = new OleDbConnection(conString);
if (con.State == ConnectionState.Closed) con.Open();
DataTable ExcelSheets = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string SpreadSheetName = ExcelSheets.Rows[0]["TABLE_NAME"].ToString();
string query = "SELECT * FROM [" + SpreadSheetName + "]";
OleDbCommand cmd = new OleDbCommand(query, con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
da.Dispose();
con.Close();
con.Dispose();
【问题讨论】:
-
最后一列到底是什么?它是与其他信息不同的特定类型的信息吗?你的列有标题吗?
-
在 excel 列“BZ”标题名称为“CustomerID”,值为 1
标签: c#