NPOI读写excel

Npoi 是什么?

1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表):Sheet;行:Row;单元格Cell。

2.Npoi 下载地址:http://npoi.codeplex.com/releases/view/38113

3.Npoi 学习系列教程推荐:http://www.cnblogs.com/tonyqus/archive/2009/04/12/1434209.html

4.忘了告诉大家npoi是做什么的了,npoi 能够读写几乎所有的Office 97-2003文件格式,至少能够支持Word, PowerPoint, Excel, Visio的格式。

使用Npoi创建一个简单的xls文件

 

 1  //创建xls文件
 2         private void button1_Click(object sender, EventArgs e)
 3         {
 4             //创建工作薄
 5             HSSFWorkbook wk = new HSSFWorkbook();
 6             //创建一个名称为mySheet的表
 7             ISheet tb = wk.CreateSheet("mySheet");
 8             //创建一行,此行为第二行
 9             IRow row = tb.CreateRow(1);
10             for (int i = 0; i < 20; i++)   
11             {
12                 ICell cell = row.CreateCell(i);  //在第二行中创建单元格
13                 cell.SetCellValue(i);//循环往第二行的单元格中添加数据
14             }
15             using (FileStream fs = File.OpenWrite(@"c:/myxls.xls")) //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
16             {
17                 wk.Write(fs);   //向打开的这个xls文件中写入mySheet表并保存。
18                 MessageBox.Show("提示:创建成功!");
19             }
20         }

 

 

使用Npoi创建一个常用的xls文件

  1 //创建一个常用的xls文件
  2         private void button3_Click(object sender, EventArgs e)
  3         {          
  4             IWorkbook wb = new HSSFWorkbook();
  5             //创建表  
  6             ISheet sh = wb.CreateSheet("zhiyuan");
  7             //设置单元的宽度  
  8             sh.SetColumnWidth(0, 15 * 256);
  9             sh.SetColumnWidth(1, 35 * 256);
 10             sh.SetColumnWidth(2, 15 * 256);
 11             sh.SetColumnWidth(3, 10 * 256);
 12             int i = 0;
 13             #region 练习合并单元格
 14             sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 3));
 15 
 16             //CellRangeAddress()该方法的参数次序是:开始行号,结束行号,开始列号,结束列号。
 17   
 18             IRow row0 = sh.CreateRow(0);
 19             row0.Height = 20 * 20;
 20             ICell icell1top0 = row0.CreateCell(0);
 21             icell1top0.CellStyle = Getcellstyle(wb, stylexls.头);
 22             icell1top0.SetCellValue("标题合并单元");
 23             #endregion
 24             i++;
 25             #region 设置表头
 26             IRow row1 = sh.CreateRow(1);
 27             row1.Height = 20 * 20;
 28 
 29             ICell icell1top = row1.CreateCell(0);
 30             icell1top.CellStyle = Getcellstyle(wb, stylexls.头);
 31             icell1top.SetCellValue("网站名");
 32 
 33             ICell icell2top = row1.CreateCell(1);
 34             icell2top.CellStyle = Getcellstyle(wb, stylexls.头);
 35             icell2top.SetCellValue("网址");
 36 
 37             ICell icell3top = row1.CreateCell(2);
 38             icell3top.CellStyle = Getcellstyle(wb, stylexls.头);
 39             icell3top.SetCellValue("百度快照");
 40 
 41             ICell icell4top = row1.CreateCell(3);
 42             icell4top.CellStyle = Getcellstyle(wb, stylexls.头);
 43             icell4top.SetCellValue("百度收录");
 44             #endregion  
 45   
 46             using(FileStream stm=File.OpenWrite(@"c:/myMergeCell.xls"))
 47             {
 48                 wb.Write(stm); 
 49                 MessageBox.Show("提示:创建成功!");
 50             }
 51         }
 52 
 53 
 54 
 55         #region 定义单元格常用到样式的枚举
 56         public enum stylexls
 57         {
 58             头,
 59             url,
 60             时间,
 61             数字,
 62             钱,
 63             百分比,
 64             中文大写,
 65             科学计数法,
 66             默认
 67         }
 68         #endregion
 69 
 70 
 71         #region 定义单元格常用到样式
 72         static ICellStyle Getcellstyle(IWorkbook wb, stylexls str)
 73         {
 74             ICellStyle cellStyle = wb.CreateCellStyle();
 75 
 76             //定义几种字体  
 77             //也可以一种字体,写一些公共属性,然后在下面需要时加特殊的  
 78             IFont font12 = wb.CreateFont();
 79             font12.FontHeightInPoints = 10;
 80             font12.FontName = "微软雅黑";
 81 
 82 
 83             IFont font = wb.CreateFont();
 84             font.FontName = "微软雅黑";
 85             //font.Underline = 1;下划线  
 86 
 87 
 88             IFont fontcolorblue = wb.CreateFont();
 89             fontcolorblue.Color = HSSFColor.OLIVE_GREEN.BLUE.index;
 90             fontcolorblue.IsItalic = true;//下划线  
 91             fontcolorblue.FontName = "微软雅黑";
 92 
 93 
 94             //边框  
 95             cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;
 96             cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.HAIR;
 97             cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.HAIR;
 98             cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.DOTTED;
 99             //边框颜色  
100             cellStyle.BottomBorderColor = HSSFColor.OLIVE_GREEN.BLUE.index;
101             cellStyle.TopBorderColor = HSSFColor.OLIVE_GREEN.BLUE.index;
102 
103             //背景图形,我没有用到过。感觉很丑  
104             //cellStyle.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;  
105             //cellStyle.FillForegroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;  
106             cellStyle.FillForegroundColor = HSSFColor.WHITE.index;
107             // cellStyle.FillPattern = FillPatternType.NO_FILL;  
108             cellStyle.FillBackgroundColor = HSSFColor.BLUE.index;
109 
110             //水平对齐  
111             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
112 
113             //垂直对齐  
114             cellStyle.VerticalAlignment = VerticalAlignment.CENTER;
115 
116             //自动换行  
117             cellStyle.WrapText = true;
118 
119             //缩进;当设置为1时,前面留的空白太大了。希旺官网改进。或者是我设置的不对  
120             cellStyle.Indention = 0;
121 
122             //上面基本都是设共公的设置  
123             //下面列出了常用的字段类型  
124             switch (str)
125             {
126                 case stylexls.头:
127                     // cellStyle.FillPattern = FillPatternType.LEAST_DOTS;  
128                     cellStyle.SetFont(font12);
129                     break;
130                 case stylexls.时间:
131                     IDataFormat datastyle = wb.CreateDataFormat();
132 
133                     cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd");
134                     cellStyle.SetFont(font);
135                     break;
136                 case stylexls.数字:
137                     cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
138                     cellStyle.SetFont(font);
139                     break;
140                 case stylexls.钱:
141                     IDataFormat format = wb.CreateDataFormat();
142                     cellStyle.DataFormat = format.GetFormat("¥#,##0");
143                     cellStyle.SetFont(font);
144                     break;
145                 case stylexls.url:
146                     fontcolorblue.Underline = 1;
147                     cellStyle.SetFont(fontcolorblue);
148                     break;
149                 case stylexls.百分比:
150                     cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
151                     cellStyle.SetFont(font);
152                     break;
153                 case stylexls.中文大写:
154                     IDataFormat format1 = wb.CreateDataFormat();
155                     cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0");
156                     cellStyle.SetFont(font);
157                     break;
158                 case stylexls.科学计数法:
159                     cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00");
160                     cellStyle.SetFont(font);
161                     break;
162                 case stylexls.默认:
163                     cellStyle.SetFont(font);
164                     break;
165             }
166             return cellStyle;
167 
168 
169         }
170         #endregion  
原文地址:https://www.cnblogs.com/ifutan/p/3642793.html