1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
using NPOI.HSSF.UserModel;
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>();
testnumber.Add(75847);
testnumber.Add(52505);
testnumber.Add(56565);
testnumber.Add(78452);
string pathSource = @"C:\Users\Lionel84100\Desktop\Excel\C-4542454.xlsx";
IWorkbook templateWorkbook;
using (FileStream fs = new FileStream(pathSource, FileMode.Open, FileAccess.Read))
{
templateWorkbook = new XSSFWorkbook(fs);
}
string sheetName = "Formatage_IM";
var sheet = templateWorkbook.GetSheet(sheetName) ?? templateWorkbook.CreateSheet(sheetName);
for (int i = 4; i < 13; i++)
{
var dataRow = sheet.GetRow(i) ?? sheet.CreateRow(i);
for (int j = 0; j < 1; j++)
{
var cell = dataRow.GetCell(0) ?? dataRow.CreateCell(0);
cell.SetCellValue(testnumber[0]);
}
}
using (FileStream fs = new FileStream(pathSource, FileMode.Create, FileAccess.Write))
{
templateWorkbook.Write(fs);
}
}
}
} |
Partager