【问题标题】:How to return a Single DataTable From All Excel Sheets Using Interop.Excel Library如何使用 Interop.Excel 库从所有 Excel 工作表返回单个数据表
【发布时间】:2015-05-24 21:34:58
【问题描述】:

如何从所有 Excel 工作表中返回单个数据表,我已经编写了此方法的所有代码,但是当读取多个工作表时,第二个、第三个等工作表中的数据将插入到第一个工作表中列。请纠正这个问题,它将帮助很多想要使用 interop.excel COM 阅读所有 Excel 工作表的人,这是一个非常好的工具,因为 ado.net 阅读会产生版本兼容性问题。

public System.Data.DataTable SetOne(string ExcelFilePath)
{       
    System.Data.DataTable table = new System.Data.DataTable();
    Microsoft.Office.Interop.Excel.Application app = new     Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(ExcelFilePath,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
           Type.Missing, Type.Missing, Type.Missing, Type.Missing,
           Type.Missing, Type.Missing, Type.Missing, Type.Missing,
           Type.Missing, Type.Missing);
            int NoOfSheetRows=0;
    foreach (Worksheet item in app.Worksheets)
    {
        string sheetname = item.Name;
        Worksheet sheet = (Worksheet)wb.Sheets[sheetname];

        Range excelRange = sheet.UsedRange;
        string fileRange = sheet.UsedRange.Address;
        string filecolums = fileRange.Substring(6, 1);
        List<string> str = new List<string>();
        int cntr = 0;

        foreach (Microsoft.Office.Interop.Excel.Range row in excelRange.Rows)
        {
            int rowNumber = row.Row;
            string[] A4D4 = this.GetRange("A" + rowNumber + ":" + filecolums + "" + rowNumber + "", sheet);

            if (rowNumber.Equals(1))
            {
                foreach (var itm in A4D4)
                {
                    if (table.Columns.Contains(itm)==false)
                    {
                        table.Columns.Add(itm);
                        str.Add(itm);
                        cntr++;
                    }
                    else
                    {
                        table.Columns.Add(itm + "..");
                        str.Add(itm);
                        cntr++;
                    }
                }
            }
            else
            {
                DataRow drow;
                drow = table.NewRow();
                drow.ItemArray = A4D4;
               //table.Rows.InsertAt(drow, NoOfSheetRows);
                table.Rows.Add(drow); // This is Area where the Problem is created the the sheet 2,3,4 and so forth data is inserted to 1st Sheet Columns 
            }
        }
        NoOfSheetRows += cntr;
    }
    return table;
}

public string[] GetRange(string range, Worksheet excelWorksheet)
{
    Microsoft.Office.Interop.Excel.Range workingRangeCells =
      excelWorksheet.get_Range(range, Type.Missing);
    System.Array array = (System.Array)workingRangeCells.Cells.Value2;
    string[] arrayS = array.OfType<object>().Select(o => o.ToString()).ToArray();
    return arrayS;
}

【问题讨论】:

  • interop.excel的使用意味着你已经在web服务器上安装了microsoft excel(例如)。微软告诉微软office不能在服务器端使用,它不是为它设计的(它崩溃,消耗大量内存并冻结应用程序池)。
  • 看在上帝的份上,停止使用 Interop 进行此类操作。您不使用锤子耕种田地,并且在 Web 服务器上使用互操作则更加错误。使用像 EPPLUS、aspose.cells(xls 支持)这样的库或无数其他库之一。
  • @bdn02 我将它用于桌面应用程序或 Web,因此不需要 Web 服务器。因此,与 ado.net 相比,Interop 对独立系统有好处,这会产生兼容性问题。但是我的问题只是在 DataTable 中组织数据,因为我已经从 Excel 表中读取了文件中的数据。@Christian Sauer我错了吗
  • @Shakir.iti Interop 比 Ado.Net “更好”,因为一个树桩总比没有腿好。像 EPPLUS 这样的库速度要快很多(可以达到 1000 倍),更容易开发,并且不会出现像 excel kliing 本身这样的零星错误。
  • @Christian Sauer 我尝试了 EPPlus,这是一个非常好的 dll,我用它来将我所有的 Excel 文件数据转换为我在 Excel.Interop 中花费了很多时间的 DataTable。请添加您的问题,我想将其标记为其他人可以从您的问题中提供帮助的答案。不错的工具 EPPlust ...

标签: c#


【解决方案1】:

我会推荐 EPPLUS - 它是一个旨在读取/写入 xlsx/xlsm 文件的库。它比 Interop 非常快速、免费且强大得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多