简体   繁体   中英

How to write element from a list in a existing excel file using NPOI in C#?

I am an intern, I am programming in C# right now and I have to write in a existing excel file elements from a list that at the moment i will instance it. This excel is actually results gathered after a chemical analysis called ICP from 2 diffrents machines. My problem is that i am not able no write in the excel file correctly, i am using npoi and I want to write in the first column of the second page of the file next to my table.

在此处输入图像描述

I would like to write each item of my list in one row in the column 1 next to my table. For example item one in line 5; item 2 in line 6 etc...

Here is my code:

using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace excel
{
    class Class2
    {
        public static void WriteExcel()
        {
            List<int> testnumber = new List<int> { 77847, 01475, 20521, 25485 };
            XSSFWorkbook hssfwb;
            using (FileStream file = new FileStream(@"C:\Users\Lionel84100\Desktop\Excel\C-4542454.xlsx", FileMode.Open, FileAccess.Read))
            {
                hssfwb = new XSSFWorkbook(file);
                file.Close();
            }

            ISheet sheet = hssfwb.GetSheetAt(1);
            IRow row = sheet.GetRow(3);

            sheet.CreateRow(row.FirstCellNum);
            ICell cell = row.CreateCell(row.FirstCellNum);
            

            for (int i = 0; i < row.FirstCellNum; i++)
            {
                cell.SetCellValue(testnumber);
            }

            using (FileStream file = new FileStream(@"C:\Users\Lionel84100\Desktop\Excel\C-4542454.xlsx", FileMode.Open, FileAccess.Write))
            {
                hssfwb.Write(file);
                file.Close();
            }
        }

    }
}

Can you please help me to solve my problem?

Thank you for your comprehension.

I understand from your question that your goal is to write a list of integers into the first column of the second sheet starting at row 5.

To do this you need to:

  1. Open the file and read it into a workbook
  2. Get the sheet
  3. Loop over the numbers in your list, setting each to a different cell in column A starting at row 5.
  4. Write the workbook back to the file, overwriting the original.

Your code does step 1 and 2 correctly, but there are problems with your code after that. And when you write the file, you need to use FileMode.Create to overwrite it completely, otherwise you will get a corrupted file. Try like this:

public static void WriteExcel()
{
    string filename = @"C:\Users\Lionel84100\Desktop\Excel\C-4542454.xlsx";

    List<int> testnumber = new List<int> { 77847, 01475, 20521, 25485 };

    // Read the xlsx file into a workbook
    XSSFWorkbook hssfwb;
    using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read))
    {
        hssfwb = new XSSFWorkbook(file);
    }

    ISheet sheet = hssfwb.GetSheetAt(1);

    // Start at the 5th row (counting from 0)
    int rowIndex = 4;

    // Loop over the entire list of numbers
    foreach (var number in testnumber)
    {
        // Try to get the row at the current row index, otherwise create it
        IRow row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex);

        // Try to get the first cell in the row (column A), otherwise create it
        ICell cell = row.GetCell(0) ?? row.CreateCell(0);

        // Set the cell value to the current number from the list we're looping over
        cell.SetCellValue(number);

        // Increment the row index
        rowIndex++;
    }

    // Use FileMode.Create here to overwrite the original file and prevent corruption
    using (FileStream file = new FileStream(filename, FileMode.Create, FileAccess.Write))
    {
        hssfwb.Write(file);
    }
}

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