【发布时间】:2015-04-28 02:50:57
【问题描述】:
我在使用 oledb 阅读器读取 excel 表时遇到问题,第一列未由阅读器返回,并在最后一列标题 F14 显示,并且列已为空。 但是当我打开 excel 表并双击标题行边框进行自动调整和自动调整大小保存 excel 并再次读取时,所有列都完美返回。
我尝试阅读使用 php 应用程序生成的 Excel 表,下载该 excel 后,我们将其放在我的应用程序中以从 excel 中读取数据,但出现上述问题。
我已经做了很多研发工作,即使我在使用 web 应用程序生成 excel 时在 excel 表中给出了宽度。我的代码是这样的
private bool Import_To_Grid(string FilePath, string Extension)
{
try
{
string conStr = "";
switch (Extension)
{
case ".xls": //Excel 97-03
conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"]
.ConnectionString;
break;
case ".xlsx": //Excel 07 and above
conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"]
.ConnectionString;
break;
}
conStr = String.Format(conStr, FilePath);
OleDbConnection connExcel = new OleDbConnection(conStr);
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
cmdExcel.Connection = connExcel;
//Get the name of First Sheet
connExcel.Open();
Exceldt = new DataTable();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connExcel.Close();
//Read Data from First Sheet
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
oda.SelectCommand = cmdExcel;
oda.Fill(Exceldt);
connExcel.Close();
//Bind Data to GridView
dgv_showexcel.DataSource = Exceldt;
BindDataToCmbClass(); //binddata to class for filter
cmb_userclass.SelectedIndex = 0;
return true;
}
catch (Exception ex) { MessageBox.Show("Its Error" + " " + ex.ToString()); return false; }
}
Connection string
<add name="Excel03ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';" />
<add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';" />
【问题讨论】:
-
很紧急,我没有得到任何关于此的任何信息,如果对此有任何想法,请与我分享,谢谢