美好的一天!
我尝试打开并将excel文件解析为DataSet。
所以,我使用OleDbConnection:
if (_filePath.Substring(_filePath.LastIndexOf('.')).ToLower() == ".xlsx")
// strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
// + _filePath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ _filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=" + HDR + ";IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text;\"";
// strConn="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _filePath + ";Extended Properties=Excel 12.0;";
else
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _filePath + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=1\"";
但有些专栏是空的! 下一列解析得很好(它具有相同的数据)。
你能告诉我如何解决它吗?
然后我填写数据集:
OleDbConnection conn = new OleDbConnection(strConn);
System.Data.DataSet dtSet;
System.Data.OleDb.OleDbDataAdapter oleCommand;
oleCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetName + "]", conn);
oleCommand.TableMappings.Add("Table", sheetName);
dtSet = new System.Data.DataSet();
oleCommand.Fill(dtSet);
oleCommand.Dispose();
conn.Close();
return dtSet.Tables[0];
但是,有些列是空的!
可能是因为excel文件格式为:
Cell1--------------|Value1------------|
Cell2---|Cell3-----|Value2---|Value4--|
因此,数据集填充列:
Cell1---|-------|--Value1------|-----|
Cell2---|Cell3--|---Empty(!)---|Value4|
所以,我需要得到空(!)列。
关于列的无效数据。
我将此列复制并粘贴到右栏 - 它可以正常工作!
但是,我应该使用最后一种格式,而不是我的。
HDR =" NO&#34 ;;
答案 0 :(得分:1)