简体   繁体   English

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

[英]C# NPOI library : xlsx file is not updated after modification

so I have the following code where I'm trying to modify the excel sheet.所以我有以下代码,我正在尝试修改 Excel 工作表。 At 4th row, I am adding additional cell with test as a string but my file is not updating.4th行,我添加了带有test作为字符串的额外单元格,但我的文件没有更新。 I have read many articles on NPOI library and found that few versions don't support writing the xlsx file.看了很多关于NPOI库的文章,发现很少有版本不支持写xlsx文件。 But I guess I'm using 2.2.1 and it should do so.但我想我使用的是2.2.1 ,它应该这样做。 Please help me.请帮帮我。

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();
            }
        }
    }
}

Perhaps you are trying to run code from this answer .也许您正在尝试从这个答案运行代码。 In your code:在您的代码中:

  1. You are trying to write on file with FileMode.Open mode.您正在尝试使用FileMode.Open模式写入文件。 Change it to FileMode.OpenOrCreate or FileMode.CreateNew .将其更改为FileMode.OpenOrCreateFileMode.CreateNew
  2. If you modify/edit the input file (in your case source.xlsx ), file will be corrupted, may be this is a bug of NPOI .如果您修改/编辑输入文件(在您的情况下source.xlsx ),文件将被损坏,这可能是NPOI的错误。 Give a different name to output file (eg-> source2.xlsx ).为输出文件指定一个不同的名称(例如-> source2.xlsx )。

Try following code.尝试以下代码。 It works for me.它对我有用。

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();
    }
}

Solution 2解决方案2

If you don't want to change the file name, delete the input file and create a new file with same file name.如果您不想更改文件名,请删除输入文件并创建一个具有相同文件名的新文件。

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();
    }
}

Solution 3: When you do not want to delete and recreate file ( like solution 2 )解决方案 3:当您不想删除和重新创建文件时(如解决方案 2

In that case, Instead of creating excel file manually (see image below), you should create file with NPOI .在这种情况下,您应该使用NPOI创建文件,而不是手动创建 excel 文件(见下图)。

在此处输入图片说明

If you create excel file with NPOI, you can edit/modify the same file.如果您使用 NPOI 创建 excel 文件,您可以编辑/修改相同的文件。 This time it will not be corrupted.这一次它不会被破坏。 To work following code properly, I am assuming you have already created source.xlsx with NPOI .为了正确使用以下代码,我假设您已经使用NPOI创建了source.xlsx

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();
        }
    }

I have modified what was suggested but nothing happened then I used IKVM.net to port Apache poi into Xamarin studio and it worked absolutely fine.我已经修改了建议的内容,但什么也没发生,然后我使用 IKVM.net 将 Apache poi 移植到 Xamarin Studio 中,它工作得非常好。 You may try as well你也可以试试

I was able to overcome the issue of writing to a secondary file by simply calling the Write() method with a null parameter.通过简单地调用带有空参数的 Write() 方法,我能够克服写入辅助文件的问题。

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

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

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

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

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