c# - 如何在 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();
}
}
}
}
你能帮我解决我的问题吗?
感谢您的理解。
解决方案
我从您的问题中了解到,您的目标是将整数列表写入第二张表的第一列,从第 5 行开始。
为此,您需要:
- 打开文件并将其读入工作簿
- 获取工作表
- 循环遍历列表中的数字,将每个数字设置为从第 5 行开始的 A 列中的不同单元格。
- 将工作簿写回文件,覆盖原始文件。
您的代码正确执行了第 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);
}
}
推荐阅读
- html - 如何减少 Html 中的行长
- scala - 如何在 Scala 中将 ArrayBuffer[Implementation] 分配给 ArrayBuffer[Interface]
- lamp - 在 bitnami 灯堆栈实例上运行 nodejs 应用程序
- python - 为什么我的 pycharm 代码给出“列表索引超出范围”?我在关注youtuber
- python - pycparser 中的未知 uint8_t 类型
- c++ - 使用 C++ 和 rapidjson 处理 JSON 信息
- flutter - 如何使用 just_audio 和 audio_service 同时播放两个(或更多)音频文件?
- tensorflow2.0 - 使用 XLA tf.function 运行 TensorFlow 会引发错误
- python - 我加载了一个 csv 文件,默认情况下,它将特定列加载为 X,另一列加载为 Y。X 和 Y 是如何选择的?
- r - 在R中组合不同类别的图