1 using NPOI.XSSF.UserModel; 2 using System; 3 using System.Collections.Generic; 4 using System.IO; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace 学习NOPI 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //创建workbook工作簿 16 XSSFWorkbook workbook = new XSSFWorkbook(); 17 //在workbook工作簿下创建两个工作表 18 workbook.CreateSheet("SheetFirst"); 19 workbook.CreateSheet("SheetSecond"); 20 //获取工作表SheetFirst 21 XSSFSheet worksheet = (XSSFSheet)workbook.GetSheet("SheetFirst"); 22 //在工作表下面创建10行 23 for (int i = 0; i < 10; i++) 24 { 25 worksheet.CreateRow(i); 26 } 27 //获取10行中的首行 28 XSSFRow sheetrow = (XSSFRow)worksheet.GetRow(0); 29 //申明一个长度10的单元格数组 30 XSSFCell[] sheetcell = new XSSFCell[10]; 31 32 for (int i = 0; i < sheetcell.Length; i++) 33 { //创建10个单元格 34 sheetcell[i] = (XSSFCell)sheetrow.CreateCell(i); 35 //分别赋值 36 sheetcell[i].SetCellValue(i); 37 } 38 //写入流 39 using (FileStream fs = new FileStream("TestNOPI.xlsx", FileMode.Create)) 40 { 41 workbook.Write(fs); 42 } 43 Console.WriteLine("OK"); 44 Console.ReadKey(); 45 46 } 47 } 48 }