NPOI 2.0 创建Excel文件

如果只是简单的处理的话,只需要引用下载压缩包里的 NPOI.dll (office 2003)或 NPOI.OOXML.dll (office 2007) 文件而已。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using NPOI.SS.UserModel;
 6 using System.IO;
 7 using NPOI.XSSF.UserModel;
 8 
 9 namespace NPOI
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             //创建Excel文件名称
16             FileStream fs = File.Create(@"F:zhxlNPOIzhxl.xlsx");
17 
18             //创建工作薄
19             IWorkbook workbook = new XSSFWorkbook();
20 
21             //创建sheet
22             ISheet sheet = workbook.CreateSheet("sheet0");
23 
24             //依次创建行和列
25             for (int i = 0; i < 10; i++)
26             {
27                 IRow row = sheet.CreateRow(i);
28                 for (int j = 0; j < 10; j++)
29                 {
30                     ICell cell = row.CreateCell(j);
31                     cell.SetCellValue(i * 10 + j);
32                 }
33 
34             }
35 
36             //向excel文件中写入数据并保保存
37             workbook.Write(fs);
38             fs.Close();            
39 
40             Console.ReadKey();            
41         }
42     }
43 }

生成的excel文件打开效果图:

原文地址:https://www.cnblogs.com/zhxlsuyu/p/3716081.html