如何与NPOI合并多个excel文档?

时间:2018-10-24 12:38:13

标签: c# excel npoi

我正在尝试使用NPOI将更多的excel文档合并到一个文档中。这是写的代码:``

static void Main(string[] args)
    {
        DataTable dt = new DataTable();
        string[] files = new string[] { @"C:\Users\Ionut\source\repos\ExcelMergeDocument\ExcelMergeDocument\bin\Debug\TAMUExport\Project1\Report3Item.xls",
        @"C:\Users\Ionut\source\repos\ExcelMergeDocument\ExcelMergeDocument\bin\Debug\TAMUExport\Project2\Report3Item.xls"};
        for (int i = 0; i < files.Length; i++)
        {

            MergeData(files[i], dt);
        }
        ExportEasy(dt, finalImagePathReport3full);
    }

    public static string imagePathReport3full = @"\ResultReport3Item.xls";

    public static string finalImagePathReport3full = AssemblyDirectory + imagePathReport3full;


    public static string AssemblyDirectory
    {
        get
        {
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            return System.IO.Path.GetDirectoryName(path);
        }
    }

    private static void MergeData(string path, DataTable dt)
    {
        XSSFWorkbook workbook = new XSSFWorkbook(path);
        XSSFSheet sheet = (XSSFSheet)workbook.GetSheetAt(0);
        XSSFRow headerRow = (XSSFRow)sheet.GetRow(0);
        int cellCount = headerRow.LastCellNum;
        if (dt.Rows.Count == 0)
        {
            for (int i = headerRow.FirstCellNum; i < cellCount; i++)
            {
                DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
                dt.Columns.Add(column);
            }
        }
        else
        {
        }

        int rowCount = sheet.LastRowNum + 1;
        for (int i = (sheet.FirstRowNum + 1); i < rowCount; i++)
        {
            XSSFRow row = (XSSFRow)sheet.GetRow(i);
            DataRow dataRow = dt.NewRow();
            for (int j = row.FirstCellNum; j < cellCount; j++)
            {
                if (row.GetCell(j) != null)
                    dataRow[j] = row.GetCell(j).ToString();
            }
            dt.Rows.Add(dataRow);
        }
        workbook = null;
        sheet = null;
    }
    public static void ExportEasy(DataTable dtSource, string strFileName)
    {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
        HSSFRow dataRow = (HSSFRow)sheet.CreateRow(0);
        foreach (DataColumn column in dtSource.Columns)
        {
            dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
        }
        for (int i = 0; i < dtSource.Rows.Count; i++)
        {
            dataRow = (HSSFRow)sheet.CreateRow(i + 1);
            for (int j = 0; j < dtSource.Columns.Count; j++)
            {
                dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i][j].ToString());
            }
        }
        using (MemoryStream ms = new MemoryStream())
        {
            using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
            {
                workbook.Write(fs);
            }
        }
    }
}

我跑步时出现以下错误:

  

ICSharpCode.SharpZipLib.Zip.ZipException:'找不到中央   目录”

我该怎么办?

NPUI仅以.xlsx扩展名工作吗?我只有excel文件的.xls扩展名。但是在以.xlsx扩展名运行程序的地方,我还有其他错误:

  

System.IO.InvalidDataException:“ Zip文件已关闭”

这两个错误均出现在以下代码行中: XSSFWorkbook工作簿=新的XSSFWorkbook(路径);

1 个答案:

答案 0 :(得分:0)

这是我对代码所做的修改:

private static void MergeData(string path, DataTable dt)
    {
        // HSSFWorkbook workbook = new HSSFWorkbook(path);
        HSSFWorkbook workbook;
        using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            workbook = new HSSFWorkbook();
        }
        HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0);
        HSSFRow headerRow = (HSSFRow)sheet.GetRow(0);
        int cellCount = headerRow.LastCellNum;
        if (dt.Rows.Count == 0)
        {
            for (int i = headerRow.FirstCellNum; i < cellCount; i++)
            {
                DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
                dt.Columns.Add(column);
            }
        }
        else
        {
        }

        int rowCount = sheet.LastRowNum + 1;
        for (int i = (sheet.FirstRowNum + 1); i < rowCount; i++)
        {
            HSSFRow row = (HSSFRow)sheet.GetRow(i);
            DataRow dataRow = dt.NewRow();
            for (int j = row.FirstCellNum; j < cellCount; j++)
            {
                if (row.GetCell(j) != null)
                    dataRow[j] = row.GetCell(j).ToString();
            }
            dt.Rows.Add(dataRow);
        }
        workbook = null;
        sheet = null;
    }

现在在此代码行:`

 HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0);

他告诉我 System.ArgumentOutOfRangeException: 指数超出范围。必须为非负数并且小于集合的大小。 参数名称:索引”。 我读到有关此错误的信息,并且知道要初始化索引或创建索引,我也尝试过这样做,但没有效果。我做错了什么,或者应该在哪里初始化索引? 其余代码与previos面板相同,只是我修改为合并.xls文档的空白。