excel中的“888A”在C#中读取不出来,显示在datagridview中为空白,代码如下:
DataSetds=newDataSet();stringstrConn=string.Empty;if(System.IO.Path.GetExtension(sExc...
DataSet ds = new DataSet();
string strConn = string.Empty;
if (System.IO.Path.GetExtension(sExcelFile).Equals(".xlsx"))
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;';data source=" + sExcelFile;
}
else if (System.IO.Path.GetExtension(sExcelFile).Equals(".xls"))
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + sExcelFile + ";" + "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;';";
}
OleDbConnection objConn = new OleDbConnection(strConn);
objConn.Open();
string sheetName = cmbSheetName.Text;
string strSql = "select * from [" + sheetName + "]";
OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn);
System.Data.DataTable dt = new System.Data.DataTable();
myData.Fill(dt);
dgBom.DataSource = dt;
objConn.Close(); 展开
string strConn = string.Empty;
if (System.IO.Path.GetExtension(sExcelFile).Equals(".xlsx"))
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;';data source=" + sExcelFile;
}
else if (System.IO.Path.GetExtension(sExcelFile).Equals(".xls"))
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + sExcelFile + ";" + "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;';";
}
OleDbConnection objConn = new OleDbConnection(strConn);
objConn.Open();
string sheetName = cmbSheetName.Text;
string strSql = "select * from [" + sheetName + "]";
OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn);
System.Data.DataTable dt = new System.Data.DataTable();
myData.Fill(dt);
dgBom.DataSource = dt;
objConn.Close(); 展开
2个回答
展开全部
用OLEDB读EXCEL,类型是自动判断的,比如,如果这一列第一行数据是888,那么EXCEL就会判断这一列是DOUBLE类型,那么888A就会显示空白(无法转换成浮点数),如果一列空白数或是字符单元格多,那么很有可能OLEDB会认为这一列全是空白或是字符串,那么数字反而全不能读,最好的方法就是用NPOI这样的开源EXCEL读取控件,可以智能识别每个单元格的公式和类型,而且导出神速,十几W条数据的导出和读入都是比较快的,但是20W以上会报错
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询