1

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();
            }
        }
    }
}
Khalid Hussain
  • 1,675
  • 17
  • 25

3 Answers3

0

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 (e.g->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.

enter image description here

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();
        }
    }
Community
  • 1
  • 1
Khalid Hussain
  • 1,675
  • 17
  • 25
0

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

0

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();
Nico M
  • 173
  • 1
  • 8