I facing problem to read excel sheet using oledb reader, first column not returning by reader and showing at last column head F14 and column has been empty. But when I open excel sheet and double click on header row border for auto adjust and auto re size save excel and again read then all columns returning perfectly.
Excel sheet which I try to read that generates using php application and after download that excel we put on my application to read data from excel but above issue come.
I already did lots of R&D even I give width in excel sheet while generating excel using web application. My code is like this
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';" />
当我们下载锁定第一个单元格的bcz标头或写入模式时,我们需要在默认的第一列和第二行为此bcz标头位置设置IMEX = 3。
I just solved same problem. Maybe stand someone in good stead.
In my case, the excel file contains a filter on a column. So when reading the file, getting the hidden sheet that named as _xlnm#_FilterDatabase
.
Actually I expect one sheet, so I am looking at the first row on the schema table. But I get two sheets which one is the filter sheet and this sheet does not contains all columns. So I do not get the first column.
To solve this, loop through schema and get the first sheet name that not contains FilterDatabase
.
Code talks:
DataTable excelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = null;
for (int i = 0; i < dtExcelSchema.Rows.Count; i++)
{
string tableNameOnRow = dtExcelSchema.Rows[i]["TABLE_NAME"].ToString();
if (!tableNameOnRow.Contains("FilterDatabase"))
{
sheetName = tableNameOnRow;
break;
}
}
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet(); cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.