繁体   English   中英

我想在一个文件中读取多个Excel工作表而不在C#中使用OLEDB

[英]I want to Read Multiple excel sheets in one file without using OLEDB in C#

我从C#Windows窗体中读取Excel文档。Excel工作簿中有25个工作表。 OLEDB ..

我想在每张纸上阅读100行。以下是我的代码...

`       dt.Columns.Add("Amount", typeof(double));
        dt.Columns.Add("ChequeNo", typeof(int));
        dt.Columns.Add("month", typeof(int));


        int AmountRow = 100;
        int ChequeNoRow = 101;
        int Column = 3;

        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[2];\\This place is the changing worksheets

        range = xlWorkSheet.UsedRange;

        double chequeAmount;
        double chequeNo;

        for (int i = Column; i < 15; i++)
        {
            chequeAmount = (double)(range.Cells[AmountRow, i] as Excel.Range).Value2;
            chequeNo = (double)(range.Cells[ChequeNoRow, i] as Excel.Range).Value2;

            if (chequeNo != 0.0)
            {
                dt.Rows.Add(Convert.ToDouble(chequeAmount), Convert.ToInt32(chequeNo), i);
            }
        }

        dataGridView1.DataSource = dt;
        xlWorkBook.Close(true, null, null);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);`

releaseObject方法不在这里..那些工作正常...

 `xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];`

这就是我更改工作表的方式。.下一行给出了一个例外.. [空点例外]

chequeAmount = (double)(range.Cells[AmountRow, i] as Excel.Range).Value2;

希望你会知道答案..

尝试以下方法:

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[2];

Sheets属性可以包括非工作表工作表,这可能是您的方案中存在的问题。

暂无
暂无

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

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