Advertisement
kklevi

c# xlsx create

Jun 22nd, 2023
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. //https://stackoverflow.com/questions/23041021/how-to-write-some-data-to-excel-file-xlsx
  2.  
  3. using NPOI.SS.UserModel;
  4. using NPOI.XSSF.UserModel;
  5.  
  6. namespace MacroUCS.Backend
  7. {
  8.     class Sample
  9.     {
  10.         public void SampleFunction()
  11.         {
  12.             IWorkbook workbook = new XSSFWorkbook();
  13.             ISheet sheet1 = workbook.CreateSheet("Sheet1");
  14.  
  15.             IRow row1 = sheet1.CreateRow(0);
  16.             row1.CreateCell(0).SetCellValue("Name");
  17.             row1.CreateCell(1).SetCellValue("Age");
  18.             row1.CreateCell(2).SetCellValue("City");
  19.  
  20.             IRow row2 = sheet1.CreateRow(1);
  21.             row2.CreateCell(0).SetCellValue("Ben");
  22.             row2.CreateCell(1).SetCellValue("20");
  23.             row2.CreateCell(2).SetCellValue("xyz");
  24.  
  25.             IRow row3 = sheet1.CreateRow(2);
  26.             row3.CreateCell(0).SetCellValue("Jack");
  27.             row3.CreateCell(1).SetCellValue("25");
  28.             row3.CreateCell(2).SetCellValue("xyz");
  29.  
  30.             IRow row4 = sheet1.CreateRow(3);
  31.             row4.CreateCell(0).SetCellValue("Mike");
  32.             row4.CreateCell(1).SetCellValue("45");
  33.             row4.CreateCell(2).SetCellValue("zyx");
  34.  
  35.             FileStream sw = File.Create("work1_4.13.14.xlsx");
  36.             workbook.Write(sw, true);
  37.             sw.Close();
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement