从.xls文件获取数据 [英] Get data from .xls file

查看:94
本文介绍了从.xls文件获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

请帮助我,我想在数据库中插入一个Excel工作表的特定行和列数据.我知道我可以使用"OleDbConnection"连接,但是它给了我整个工作表数据,而不是工作表的一部分.因此,在这种情况下,请帮助我.

提前谢谢.


谢谢大家,
非常感谢您提出的宝贵建议,但是我尝试了您的建议,但它给我带来了错误.我知道您的提及代码并不完整,这仅仅是我的目标的一种方式,而这并非仅此而已.您能不能进一步指导我解决您的问题,我真的非常感谢您.

ahsan sarfraz我已经尝试过了,但是数据是原始数据,因此某些数据成为了列名,因此无法使用此解决方案.
伙计们,我现在正在使用窗口应用程序,因此请就该咏叹调向我提出建议,我的目标是从Excel文件中获取数据并填充数据集,然后更新数据库中的那些数据集.

还有其他解决方案吗,请提供我确实需要的解决方案.

Hi guys,

Please help me, I want to insert data of one excel sheet’s particular row and column data in database. I know I can use "OleDbConnection" connection, but it give me whole sheet data not portion of sheet. So please help me in this case.

Thanks in advance.


Thanks guys,
I really appreciate your valuable suggestions, but Om Prakash Pant I try your suggestion and it give me error. I know that your mention code is not complete it only a way for my goal which not achieved by only this. Can you please guide me on your solutions more I really thank full to you.

ahsan sarfraz I already try this but data is raw then some data become column name so it not possible to use this solution.
Guys, I use window application right now so please suggest me on that aria and my goal is fetch data from Excel file and fill the dataset then update those dataset in database.

Is there any other solution please provide me I really need it.

推荐答案

如果数据不一致,但行和列固定,则可以使用Excel对象库而不是OleDB提供程序.类似于以下代码:

If the data is not uniform but the row and column is fixed then you can use Excel Objects Library instead of OleDB provider. something like the below code:

Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(openFileDialog1.FileName, 0, true, 5,
          "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
          0, true); 
     Excel.Sheets sheets = theWorkbook.Worksheets;
     Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
     for (int i = 1; i <= 10; i++)
     {
     Excel.Range range = worksheet.get_Range("A"+i.ToString(), "J" + i.ToString());
     System.Array myvalues = (System.Array)range.Cells.Value;
}



如果数据已格式化并具有顺序数据,则可以使用OleDB提供程序.



If the data is formatted and has sequential data then you can use OleDB provider.



您应该从excel工作表中从OLEDB连接获取数据.将数据填充到数据表/数据集中,然后可以从数据集中过滤行并插入所需的行,而保留不想要的行...
关键是先获取全部数据,然后进行过滤,而不是先进行过滤再进行检索.
希望对您有帮助.
祝您好运

you should get the data from OLEDB connection from the excel sheet. Populae the data in a datatable/Dataset and then you can filter the rows from the dataset and insert the ones you want and leave the ones you dont want...
the point is get the whole data and then filter rather then filter and then retrieve.
hope it helps.
best of luck


string Con_Str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FullPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
                    OleDbConnection con = new OleDbConnection(Con_Str);

                    String qry = "SELECT * FROM [Sheet1


这篇关于从.xls文件获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆