合并DataTable c#

时间:2017-02-14 10:44:12

标签: c# excel datatable merge

我正在尝试将两张来自excel文件的表合并到一个DataTable。

该函数构建并运行,但是当我试图从第二个表中获取数据时没有任何反应。 (它从第一张表开始)

这就是我目前所拥有的。 什么想法可能是错的?

 private static DataTable ExcelToDataTable(string fileName)
    {
        //open file and returns as Stream
        FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
        //Createopenxmlreader via ExcelReaderFactory
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //.xlsx
        //Set the First Row as Column Name
        excelReader.IsFirstRowAsColumnNames = true;
        //Return as DataSet
        DataSet result = excelReader.AsDataSet();
        //Get all the Tables
        DataTableCollection table = result.Tables;
        //Store it in DataTable

        DataTable resultTable1 = table["Sheet1"];
        DataTable resultTable2 = table["Extra"];
        DataTable dtAll = resultTable1.Copy();
        dtAll.Merge(resultTable2);

        //return
        return dtAll;
    }

0 个答案:

没有答案