首页 > 解决方案 > 如何在 C# 中使用 NPOI 从现有 excel 文件中的列表中写入元素?

问题描述

我是一名实习生,我现在正在用 C# 编程,我必须从一个列表中写入现有的 excel 文件元素,目前我将实例化它。这个 excel 实际上是从 2 台不同的机器进行称为 ICP 的化学分析后收集的结果。我的问题是我无法正确写入 excel 文件,我正在使用 npoi,我想在我的表格旁边的文件第二页的第一列中写入。

在此处输入图像描述

我想在表格旁边的第 1 列中的一行中写下我列表中的每一项。例如第 5 行中的第一项;第 6 行中的第 2 项等...

这是我的代码:

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

    }
}

你能帮我解决我的问题吗?

感谢您的理解。

标签: c#excelnpoi

解决方案


我从您的问题中了解到,您的目标是将整数列表写入第二张表的第一列,从第 5 行开始。

为此,您需要:

  1. 打开文件并将其读入工作簿
  2. 获取工作表
  3. 循环遍历列表中的数字,将每个数字设置为从第 5 行开始的 A 列中的不同单元格。
  4. 将工作簿写回文件,覆盖原始文件。

您的代码正确执行了第 1 步和第 2 步,但之后您的代码出现了问题。并且在写入文件时,需要使用FileMode.Create将其完全覆盖,否则会得到损坏的文件。试试这样:

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

推荐阅读