且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用c#.net读取xlsx文件时出现问题。

更新时间:2022-10-19 19:17:36

,MyConnection);
MyCommand.TableMappings.Add( TestTable);
DtSet = new System.Data.DataSet();
MyCommand.Fill (DtSet);
dtRead = DtSet.Tables [ 0 ];
MyConnection.Close();
}

catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

return pan> 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.