NPOI 2.0 创建Excel文件

时间:2014-05-09 04:29:08   收藏:0   阅读:324

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

bubuko.com,布布扣
 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:\zhxl\NPOI\zhxl.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 }
bubuko.com,布布扣

 

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

 

bubuko.com,布布扣

NPOI 2.0 创建Excel文件,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!