C#NPOI库:修改后未更新xlsx文件

时间:2016-07-20 03:21:22

标签: c# excel npoi

所以我有以下代码,我正在尝试修改Excel工作表。在4th行,我添加了带test字符串的其他单元格,但我的文件未更新。我在NPOI库上阅读了很多文章,发现很少有版本不支持编写xlsx文件。但我想我正在使用2.2.1,它应该这样做。请帮帮我。

enter code here

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Excel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
namespace PantheonProject
{
    public class test
    {
        public static void testMethod()
        {
            XSSFWorkbook hssfwb;
            using (FileStream file = new FileStream(@"/Users/harshloomba/Documents/workspace/PantheonProject/source.xlsx", FileMode.Open, FileAccess.Read))
            {
                hssfwb = new XSSFWorkbook(file);
                file.Close();
            }

            ISheet sheet = hssfwb.GetSheetAt(0);
            IRow row = sheet.GetRow(4);

            //sheet.CreateRow(row.LastCellNum);
            ICell cell = row.CreateCell(row.LastCellNum);
            cell.SetCellValue("test");

            for (int i = 0; i < row.LastCellNum; i++)
            {
                Console.WriteLine(row.GetCell(i));
            }

            using (FileStream file = new FileStream(@"/Users/harshloomba/Documents/workspace/PantheonProject/source.xlsx", FileMode.Open, FileAccess.Write))
            {
                hssfwb.Write(file);
                file.Close();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

也许您正在尝试运行代码from this answer。在您的代码中:

  1. 您正尝试使用FileMode.Open模式写入文件。将其更改为FileMode.OpenOrCreateFileMode.CreateNew
  2. 如果您修改/编辑输入文件(在您的情况下为source.xlsx),文件将被损坏,这可能是NPOI的错误。为输出文件指定其他名称(例如&gt; source2.xlsx)。
  3. 尝试以下代码。它对我有用。

    public static void testMethod()
    {
        XSSFWorkbook hssfwb;
        using (FileStream file = new FileStream(@"/Users/harshloomba/Documents/workspace/PantheonProject/source.xlsx", FileMode.Open, FileAccess.Read))
        {
            hssfwb = new XSSFWorkbook(file);
            file.Close();
        }
    
        ISheet sheet = hssfwb.GetSheetAt(0);
        IRow row = sheet.GetRow(4);
    
        //sheet.CreateRow(row.LastCellNum);
        ICell cell = row.CreateCell(row.LastCellNum);
        cell.SetCellValue("test");
    
        for (int i = 0; i < row.LastCellNum; i++)
        {
            Console.WriteLine(row.GetCell(i));
        }
    
        using (FileStream file = new FileStream(@"/Users/harshloomba/Documents/workspace/PantheonProject/source2.xlsx", FileMode.OpenOrCreate, FileAccess.Write))
        {
            hssfwb.Write(file);
            file.Close();
        }
    }
    

    解决方案2

    如果您不想更改文件名,请删除输入文件并创建一个具有相同文件名的新文件。

    public static void testMethod()
    {
        XSSFWorkbook hssfwb;
        using (FileStream file = new FileStream(@"/Users/harshloomba/Documents/workspace/PantheonProject/source.xlsx", FileMode.Open, FileAccess.Read))
        {
            hssfwb = new XSSFWorkbook(file);
            file.Close();
        }
    
        ISheet sheet = hssfwb.GetSheetAt(0);
        IRow row = sheet.GetRow(4);
    
        //sheet.CreateRow(row.LastCellNum);
        ICell cell = row.CreateCell(row.LastCellNum);
        cell.SetCellValue("test");
    
        for (int i = 0; i < row.LastCellNum; i++)
        {
            Console.WriteLine(row.GetCell(i));
        }
    
        File.Delete(@"/Users/harshloomba/Documents/workspace/PantheonProject/source.xlsx");
    
        using (FileStream file = new FileStream(@"/Users/harshloomba/Documents/workspace/PantheonProject/source.xlsx", FileMode.OpenOrCreate, FileAccess.Write))
        {
            hssfwb.Write(file);
            file.Close();
        }
    }
    

    解决方案3:当您不想删除并重新创建文件时(类似解决方案2

    在这种情况下,您应该使用NPOI创建文件,而不是手动创建Excel文件(请参见下图)。

    enter image description here

    如果使用NPOI创建excel文件,则可以编辑/修改同一文件。这次它不会被破坏。要正确使用代码,我假设您已使用source.xlsx创建了NPOI

    public static void testMethod()
        {
            XSSFWorkbook hssfwb;
            using (FileStream file = new FileStream(@"/Users/harshloomba/Documents/workspace/PantheonProject/source.xlsx", FileMode.Open, FileAccess.Read))
            {
                hssfwb = new XSSFWorkbook(file);
                file.Close();
            }
    
            ISheet sheet = hssfwb.GetSheetAt(0);
            IRow row = sheet.GetRow(4);
    
            //sheet.CreateRow(row.LastCellNum);
            ICell cell = row.CreateCell(row.LastCellNum);
            cell.SetCellValue("test");
    
            for (int i = 0; i < row.LastCellNum; i++)
            {
                Console.WriteLine(row.GetCell(i));
            }
    
    
            using (FileStream file = new FileStream(@"/Users/harshloomba/Documents/workspace/PantheonProject/source.xlsx", FileMode.OpenOrCreate, FileAccess.Write))
            {
                hssfwb.Write(file);
                file.Close();
            }
        }
    

答案 1 :(得分:0)

我已经修改了建议但没有发生任何事情然后我使用IKVM.net将Apache poi移植到Xamarin工作室并且它工作得非常好。你也可以试试

答案 2 :(得分:0)

通过简单地使用null参数调用Write()方法,就可以解决写入辅助文件的问题。

IWorkbook myWorkbook = new XSSFWorkbook(@"c:\temp\Report.xlsx");
ISheet mySheet = myWorkbook.GetSheet("SheetName");

//...
//modify mySheet as needed
//...

myWorkbook.Write(null);
myWorkbook.Close();