使用c#.net读取xlsx文件时出现问题。 [英] Problem on reading xlsx file using c#.net.
问题描述
亲爱的所有人,
我正在使用带有c#的VS2010 Wpf应用程序。我正在使用oledb连接读取xlsx文件,它的工作正常,但在阅读我的xlsx后,读取数据集中缺少一些数据。
这里我附加了我的编码,我有问题的屏幕截图,但这里没有附加屏幕截图的选项。
private DataTable ReadExcel( string tabName)
{
DataTable retTbl = new DataTable();
DataTable dtRead = new DataTable();
try
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.DataSet DtSet;
System.Data.OleDb.OleDbDataAdapter MyCommand;
MyConnection = new System.Data.OleDb.OleDbConnection( provider = Microsoft.Jet.OLEDB.4.0; Data Source ='c:\\csharp.net-informations.xls';扩展属性= Excel 8.0;);
MyConnection = conn;
MyConnection.Open();
MyCommand = new System.Data.OleDb.OleDbDataAdapter( select * from [ + tabName + $],MyConnection );
MyCommand.TableMappings.Add( 表, TestTable);
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
dtRead = DtSet.Tables [ 0 ];
MyConnection.Close();
}
catch (例外情况)
{
MessageBox.Show(ex.ToString() );
}
return dtRead;
}
Ex:
考虑为Excel列
'c1'c2'c3'
---------------
'1'测试'0'
---------------
'3'1'4'
- ---------------
阅读Excel后:
'c1'c2'c3'
---------------
'1''0'
---------------
'3'1'4'
-------- --------
请参阅以上示例Read excel后缺少Test。
对不起我的英文..
谢谢大家,
问候,
karthik M
我的尝试:
i我试图读取XLSX文件。
,MyConnection);
MyCommand.TableMappings.Add(< span class =code-string> 表, TestTable);
DtSet = new System.Data.DataSet();
MyCommand.Fill (DtSet);
dtRead = DtSet.Tables [ 0 ];
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return dtRead;
}
Ex:
考虑为Excel列
'c1'c2'c3'
---------------
'1'测试'0'
---------------
'3'1'4'
- ---------------
阅读Excel后:
'c1'c2'c3'
---------------
'1''0'
---------------
'3'1'4'
-------- --------
请参阅以上示例Read excel后缺少Test。
对不起我的英文..
谢谢大家,
问候,
karthik M
我的尝试:
i我试图读取XLSX文件。
您需要Microsoft ACE引擎来读取XLSX文件。请参阅使用MS Excel(xls / xlsx)使用MDAC和Oledb [ ^ ]。
快速思考:你说它是一个XLSX文件,但我注意到两件事:
1)这是一个XLS扩展,而不是XLSX - XLS文件根本不相同。
2)你试图将它读作Excel 8.0,但是XLSX文件不是使用Excel 2007引入V12.0: Microsoft Excel - 维基百科,免费的百科全书 [ ^ ]
请检查您的文件:查看其中的内容。
Dear All,
I am using VS2010 Wpf application with c#. I am read the xlsx file using oledb connection its working fine but after reading my xlsx some data missing in the reading data set.
Here i attached my Coding and i have the screen shot of the problem but here no option is there for attaching screen shot.
private DataTable ReadExcel(string tabName)
{
DataTable retTbl = new DataTable();
DataTable dtRead = new DataTable();
try
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.DataSet DtSet;
System.Data.OleDb.OleDbDataAdapter MyCommand;
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
MyConnection = conn;
MyConnection.Open();
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + tabName + "$]", MyConnection);
MyCommand.TableMappings.Add("Table", "TestTable");
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
dtRead = DtSet.Tables[0];
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return dtRead;
}
Ex:
Consider as Excel Columns
' c1' c2 ' c3'
---------------
' 1 ' Test ' 0 '
---------------
' 3 ' 1 ' 4 '
----------------
After Read the Excel:
' c1' c2 ' c3'
---------------
' 1 ' ' 0 '
---------------
' 3 ' 1 ' 4 '
----------------
see the Above example "Test" is missing after Read excel.
sorry for my English..
Thanks all,
Regards,
karthik M
What I have tried:
i am tried to read the XLSX file.
", MyConnection); MyCommand.TableMappings.Add("Table", "TestTable"); DtSet = new System.Data.DataSet(); MyCommand.Fill(DtSet); dtRead = DtSet.Tables[0]; MyConnection.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } return dtRead; }
Ex:
Consider as Excel Columns
' c1' c2 ' c3'
---------------
' 1 ' Test ' 0 '
---------------
' 3 ' 1 ' 4 '
----------------
After Read the Excel:
' c1' c2 ' c3'
---------------
' 1 ' ' 0 '
---------------
' 3 ' 1 ' 4 '
----------------
see the Above example "Test" is missing after Read excel.
sorry for my English..
Thanks all,
Regards,
karthik M
What I have tried:
i am tried to read the XLSX file.
You need the Microsoft ACE engine to read XLSX files. See Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^].
A quick thought: you say it's an XLSX file, but two things I note:
1) It's an XLS extension, not an XLSX - XLS files aren't the same at all.
2) You're trying to read it as Excel 8.0, but XLSX files weren't introduced until V12.0 with Excel 2007: Microsoft Excel - Wikipedia, the free encyclopedia[^]
So check your file: see what's in it.
这篇关于使用c#.net读取xlsx文件时出现问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!