简体   繁体   English

如何在C#中使用OleDb在excel文件中获取所需的单元格数据?

[英]how to get required cell data in excel file using OleDb in c#?

actually i am trying to convert excel file data to xml file. 实际上,我正在尝试将Excel文件数据转换为xml文件。 my requirement is i need to get required cell data from excel and i can edit the xml tags according to my requirement. 我的要求是我需要从excel获取必需的单元格数据,并且可以根据我的要求编辑xml标签。 is it possible????? 可能吗?????

and is it write way to use OleDb for this???? 并为此编写使用OleDb的方式?

this is my code: 这是我的代码:

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            System.Data.OleDb.OleDbConnection MyConnection;
            System.Data.DataSet ds;
            System.Data.OleDb.OleDbDataAdapter MyCommand;

            MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\\Desktop\\xl2xml\\test_2003.xls';Extended Properties=Excel 8.0;");

            MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
            //MyCommand.TableMappings.Add("Table", "Product");

            MyCommand.TableMappings.Add("Table", "Myrequireddata from required cell");  

            ds = new System.Data.DataSet();
            MyCommand.Fill(ds);
            MyConnection.Close();
            ds.WriteXml(@"C:\Desktop\xl2xml\Product.xml");
        }

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

Yes of course. 当然是。 I have done the same thing before. 我以前做过同样的事情。 Your concept is correct. 您的概念是正确的。 Here I used Microsoft DAO 3.6 object library to read excel file. 在这里,我使用Microsoft DAO 3.6对象库读取excel文件。

However you can read the excel file into a Dataset and then you can go through the Dataset object to find required data. 但是,您可以将excel文件读入数据集,然后可以通过数据集对象查找所需的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM