简体   繁体   中英

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. At 4th row, I am adding additional cell with test as a string but my file is not updating. I have read many articles on NPOI library and found that few versions don't support writing the xlsx file. But I guess I'm using 2.2.1 and it should do so. 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. Change it to FileMode.OpenOrCreate or FileMode.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 . Give a different name to output file (eg-> 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

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 )

In that case, Instead of creating excel file manually (see image below), you should create file with NPOI .

在此处输入图片说明

If you create excel file with NPOI, you can edit/modify the same file. This time it will not be corrupted. To work following code properly, I am assuming you have already created source.xlsx with 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();
        }
    }

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. 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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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