NPOI导出excel

1、为项目添加NPOI引用。

  1  public IRow SetTJRow(HSSFWorkbook wb, ISheet sh)
  2         {
  3             #region 设置表头
  4             IRow row1 = sh.CreateRow(0);
  5             row1.Height = 20 * 20;
  6             ICell icell1top = row1.CreateCell(0);
  7             icell1top.CellStyle = Getcellstyle(wb, stylexls.头);
  8             icell1top.SetCellValue("部门");
  9             ICell icell2top = row1.CreateCell(1);
 10             icell2top.CellStyle = Getcellstyle(wb, stylexls.头);
 11             icell2top.SetCellValue("职员");
 12             ICell icell3top = row1.CreateCell(2);
 13             icell3top.CellStyle = Getcellstyle(wb, stylexls.头);
 14             icell3top.SetCellValue("人员编码");
 15             ICell icell4top = row1.CreateCell(3);
 16             icell4top.CellStyle = Getcellstyle(wb, stylexls.头);
 17             icell4top.SetCellValue("月份");
 18             ICell icell5top = row1.CreateCell(4);
 19             icell5top.CellStyle = Getcellstyle(wb, stylexls.头);
 20             icell5top.SetCellValue("岗位工资");
 21             ICell icell6top = row1.CreateCell(5);
 22             icell6top.CellStyle = Getcellstyle(wb, stylexls.头);
 23             icell6top.SetCellValue("学历");
 24             ICell icell7top = row1.CreateCell(6);
 25             icell7top.CellStyle = Getcellstyle(wb, stylexls.头);
 26             icell7top.SetCellValue("技术");
 27             ICell icell8top = row1.CreateCell(7);
 28             icell8top.CellStyle = Getcellstyle(wb, stylexls.头);
 29             icell8top.SetCellValue("资历");
 30             ICell icell9top = row1.CreateCell(8);
 31             icell9top.CellStyle = Getcellstyle(wb, stylexls.头);
 32             icell9top.SetCellValue("基本工资");
 33             ICell icell10top = row1.CreateCell(9);
 34             icell10top.CellStyle = Getcellstyle(wb, stylexls.头);
 35             icell10top.SetCellValue("工龄");
 36             ICell icell11top = row1.CreateCell(10);
 37             icell11top.CellStyle = Getcellstyle(wb, stylexls.头);
 38             icell11top.SetCellValue("卫生");
 39             ICell icell12top = row1.CreateCell(11);
 40             icell12top.CellStyle = Getcellstyle(wb, stylexls.头);
 41             icell12top.SetCellValue("门诊");
 42             ICell icell13top = row1.CreateCell(12);
 43             icell13top.CellStyle = Getcellstyle(wb, stylexls.头);
 44             icell13top.SetCellValue("提租");
 45             ICell icell14top = row1.CreateCell(13);
 46             icell14top.CellStyle = Getcellstyle(wb, stylexls.头);
 47             icell14top.SetCellValue("专家津贴");
 48             ICell icell15top = row1.CreateCell(14);
 49             icell15top.CellStyle = Getcellstyle(wb, stylexls.头);
 50             icell15top.SetCellValue("专家交通");
 51             ICell icell16top = row1.CreateCell(15);
 52             icell16top.CellStyle = Getcellstyle(wb, stylexls.头);
 53             icell16top.SetCellValue("子女");
 54             ICell icell30top = row1.CreateCell(16);
 55             icell30top.CellStyle = Getcellstyle(wb, stylexls.头);
 56             icell30top.SetCellValue("值班补贴");
 57             ICell icell17top = row1.CreateCell(17);
 58             icell17top.CellStyle = Getcellstyle(wb, stylexls.头);
 59             icell17top.SetCellValue("绩效");
 60             ICell icell18top = row1.CreateCell(18);
 61             icell18top.CellStyle = Getcellstyle(wb, stylexls.头);
 62             icell18top.SetCellValue("补发");
 63             ICell icell19top = row1.CreateCell(19);
 64             icell19top.CellStyle = Getcellstyle(wb, stylexls.头);
 65             icell19top.SetCellValue("一次性奖");
 66             ICell icell20top = row1.CreateCell(20);
 67             icell20top.CellStyle = Getcellstyle(wb, stylexls.头);
 68             icell20top.SetCellValue("月奖励工资");
 69             ICell icell21top = row1.CreateCell(21);
 70             icell21top.CellStyle = Getcellstyle(wb, stylexls.头);
 71             icell21top.SetCellValue("应发合计");
 72             ICell icell22top = row1.CreateCell(22);
 73             icell22top.CellStyle = Getcellstyle(wb, stylexls.头);
 74             icell22top.SetCellValue("局租房");
 75             ICell icell23top = row1.CreateCell(23);
 76             icell23top.CellStyle = Getcellstyle(wb, stylexls.头);
 77             icell23top.SetCellValue("保养");
 78             ICell icell24top = row1.CreateCell(24);
 79             icell24top.CellStyle = Getcellstyle(wb, stylexls.头);
 80             icell24top.SetCellValue("医保");
 81             ICell icell25top = row1.CreateCell(25);
 82             icell25top.CellStyle = Getcellstyle(wb, stylexls.头);
 83             icell25top.SetCellValue("失业");
 84             ICell icell26top = row1.CreateCell(26);
 85             icell26top.CellStyle = Getcellstyle(wb, stylexls.头);
 86             icell26top.SetCellValue("房金");
 87             ICell icell27top = row1.CreateCell(27);
 88             icell27top.CellStyle = Getcellstyle(wb, stylexls.头);
 89             icell27top.SetCellValue("年金");
 90             ICell icell28top = row1.CreateCell(28);
 91             icell28top.CellStyle = Getcellstyle(wb, stylexls.头);
 92             icell28top.SetCellValue("纳税");
 93             ICell icell29top = row1.CreateCell(29);
 94             icell29top.CellStyle = Getcellstyle(wb, stylexls.头);
 95             icell29top.SetCellValue("实发工资");
 96             //ICell icell30top = row1.CreateCell(29);
 97             //icell30top.CellStyle = Getcellstyle(wb, stylexls.头);
 98             //icell30top.SetCellValue("实发工资");
 99
100             #endregion
101             return row1;
102         }

 1 #region 定义单元格常用到样式的枚举
 2         public enum stylexls
 3         {
 4             头,
 5             url,
 6             时间,
 7             数字,
 8             钱,
 9             百分比,
10             中文大写,
11             科学计数法,
12             默认
13         }
14         #endregion

 private void btnadd_MouseUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                #region 打印导出统计数据
                if (dt != null && dt.Rows.Count > 0)
                {
                    //创建工作薄
                    HSSFWorkbook wb = new HSSFWorkbook();
                    //创建一个名称为mySheet的表
                    ISheet sh = wb.CreateSheet("mySheet");
                    #region  设置表格内容
                    SetTJRow(wb, sh);//设置表头
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        IRow row = sh.CreateRow(i + 1);
                        for (int j = 3; j < dt.Columns.Count; j++)
                        {
                            string content = dt.Rows[i][j].ToString();
                            if (j == 3)
                            {
                                ICell cell = row.CreateCell(j - 3);
                                cell.SetCellValue(content);
                            }
                            else if (j > 4)
                            {
                                if (dt.Rows[i][j - 2].ToString().Contains("小计"))
                                {
                                    ICell cell = row.CreateCell(j - 4);
                                    cell.SetCellValue("总人数:" + dt.Rows[i][j - 1].ToString());
                                }
                                else if (dt.Rows[i][j - 2].ToString().Contains("总计"))
                                {
                                    ICell cell = row.CreateCell(j - 4);
                                    cell.SetCellValue("总人数:" + dt.Rows[i][j - 1].ToString());
                                }
                                else
                                {
                                    ICell cell = row.CreateCell(j - 4);
                                    cell.SetCellValue(content);
                                }
                            }
                        }
                    }
                    string saveFileName = "人员工资统计表.xls";
                    //FileStream fs=new FileStream();
                    SaveFileDialog saveDialog = new SaveFileDialog();
                    saveDialog.DefaultExt = "xls";
                    saveDialog.Filter = "Excel文件|*.xls";
                    saveDialog.FileName = saveFileName;
                    saveDialog.ShowDialog();
                    saveFileName = saveDialog.FileName;
                    if (saveFileName.IndexOf(":") < 0) return; //被点了取消
                    if (saveFileName != "")
                    {
                        using (FileStream fs = File.OpenWrite(saveDialog.FileName))//打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件
                        {
                            try
                            {
                                wb.Write(fs);
                                MessageBox.Show("导出成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择数据源!");
                    }
                    #endregion
                }
                else
                {
                    MessageBox.Show("请选择数据源!");
                }
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

 1   #region 定义单元格常用到的样式
 2         static ICellStyle Getcellstyle(IWorkbook wb, stylexls str)
 3         {
 4             ICellStyle cellStyle = wb.CreateCellStyle();
 5
 6             //定义几种字体
 7             //也可以一种字体,写一些公共属性,然后在下面需要时加特殊的
 8             IFont font12 = wb.CreateFont();
 9             font12.FontHeightInPoints = 10;
10             font12.FontName = "微软雅黑";
11
12
13             IFont font = wb.CreateFont();
14             font.FontName = "微软雅黑";
15             //font.Underline = 1;下划线
16
17
18             IFont fontcolorblue = wb.CreateFont();
19             fontcolorblue.Color = HSSFColor.OliveGreen.Blue.Index;
20             fontcolorblue.IsItalic = true;//下划线
21             fontcolorblue.FontName = "微软雅黑";
22
23
24             //边框
25             cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
26             cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
27             cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
28             cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
29             //水平对齐
30             cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
31
32             //垂直对齐
33             cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
34
35             //自动换行
36             cellStyle.WrapText = true;
37
38             //缩进;当设置为1时,前面留的空白太大了。希旺官网改进。或者是我设置的不对
39             cellStyle.Indention = 0;
40
41             //上面基本都是设共公的设置
42             //下面列出了常用的字段类型
43             switch (str)
44             {
45                 case stylexls.头:
46                     // cellStyle.FillPattern = FillPatternType.LEAST_DOTS;
47                     cellStyle.SetFont(font12);
48                     break;
49                 case stylexls.时间:
50                     IDataFormat datastyle = wb.CreateDataFormat();
51
52                     cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd");
53                     cellStyle.SetFont(font);
54                     break;
55                 case stylexls.数字:
56                     cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
57                     cellStyle.SetFont(font);
58                     break;
59                 case stylexls.钱:
60                     IDataFormat format = wb.CreateDataFormat();
61                     cellStyle.DataFormat = format.GetFormat("¥#,##0");
62                     cellStyle.SetFont(font);
63                     break;
64                 case stylexls.百分比:
65                     cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
66                     cellStyle.SetFont(font);
67                     break;
68                 case stylexls.中文大写:
69                     IDataFormat format1 = wb.CreateDataFormat();
70                     cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0");
71                     cellStyle.SetFont(font);
72                     break;
73                 case stylexls.科学计数法:
74                     cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00");
75                     cellStyle.SetFont(font);
76                     break;
77                 case stylexls.默认:
78                     cellStyle.SetFont(font);
79                     break;
80             }
81             return cellStyle;
82
83
84         }
85         #endregion

时间: 2024-07-28 17:55:45

NPOI导出excel的相关文章

NPOI导出excel(带图片)

近期项目中用到Excel导出功能,之前都是用普通的office组件导出的方法,今天尝试用下NPOI,故作此文以备日后查阅. 1.NPOI官网http://npoi.codeplex.com/,下载最新工具包. 2.选择.net版本(例如dotnet4),引用其中的dll. 3.后台代码 using System; using System.Web; using System.Data; using System.Configuration; using System.IO; using Test

NPOI导出EXCEL 打印设置分页及打印标题

在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方法,但一直都没有起到作用.经过研究是要设置  sheet1.FitToPage = false; 而这个属性默认是true的,怪不得一直设置都不起作用. 设置打印标题用 hssfworkbook.SetRepeatingRowsAndColumns(0, 0, 5, 0, 5); 但最新版本已经有新的方法了. Sheet sheet1 = hssfworkbook.CreateSheet("She

分享使用NPOI导出Excel树状结构的数据,如部门用户菜单权限

大家都知道使用NPOI导出Excel格式数据 很简单,网上一搜,到处都有示例代码. 因为工作的关系,经常会有处理各种数据库数据的场景,其中处理Excel 数据导出,以备客户人员确认数据,场景很常见. 一个系统开发出来,系统要运行起来,很多数据要初始化,这个时候也是需要客户提供各种业务的基础数据.客户提供的数据中,其中除了word.pdf,最常见的就是Excel. 废话不多说,直接上图上代码: 如图, 左侧三列,作为 一个系统 所有菜单的树状结构. 其他列 以用户的信息(如用户名.登录名) 作为表

.NET NPOI导出Excel详解

NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件. 支持的文件格式包括xls, doc, ppt等. 官方网站:http://npoi.codeplex.com/ nuget直接获取使用 一.NPOI生成Excel //创建工作薄 var workbook = new HSSFWorkbook(); //创建表 var table = workbook.CreateSheet(

共享一个MVC4通过NPOI导出excel的通用方法

1 public static System.IO.MemoryStream ExportExcel<T>(string title, List<T> objList, params string[] excelPropertyNames) 2 { 3 NPOI.SS.UserModel.IWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook(); 4 NPOI.SS.UserModel.ISheet sheet = wo

Asp.Net 使用Npoi导出Excel

引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是使用OleDb的方式,这种方式的导入在服务器端似乎还是需要装office组件的,有没有不需要装组件并且能照常导入的呢? Npoi导出/下载Excel public void NpoiExcel(DataTable dt, string title) { NPOI.HSSF.UserModel.HSS

NPOI导出Excel时出现错误“Maximum column number is 255”

此错误并不是NPOI的问题,而是Excel的问题,原因是Excel的最大列255,所以导出的列超过255时就会出现此问题 但Excel2007以上并没有此问题,因为2007以上的列已经增加到16384列啦,但2003和2007的代码是不一样的 NPOI操作2007(NPOI 2.0):http://tonyqus.sinaapp.com/npoi2tutorial 如何解决此问题 1.使用2003将列限制到255之内, 2.使用2007列可以扩大到16384个,但之前是2003的代码所以更改成2

NPOI导出Excel - 自动适应中文宽度(帮助类下载)

前言 做了好几个Excel.Word导出,用了HTTP流导出伪Excel文件.用过Office组件(这东西在生产环境下相当麻烦,各种权限,**). 最后决定使用NPOI组件来导出,好处很多很多了,这里不多说. 这篇文章呢,主要说一下Excel导出的细节以及问题. 我在制作这个Demo的时候使用的环境: Visual Studio 2010.Office 2013 .Framework .NET 3.5 .NPOI 1.2.5(至于为什么没有选最新版 稍后说) 完成后的截图 从浏览器导出的Exce

NPOI导出Excel(含有超过65335的处理情况)

NPOI导出excel的网上有很多,正好自己遇到了学习并且记录一下: 首先说明几点: 1.Excel2003及一下:后缀xls,单个sheet最大行数为65335 Excel2007 单个sheet :后缀xlsx 单个sheet 最大行数为 1048576 2.在用NPOI.dll时,导出的excel两种形式(xls,xlsx)用到的组件不一样,xls是HSSF,xlsx是XSSF, 由于某种原因我选择的是HSSF组件的: 以linq 匿名类得到数据为数据源导出Excel 类如这样的: var

NPOI 导出 excel 性能测试

NPOI 导出 excel 性能测试 Intro 网上看到很多人说 NPOI 的性能不行,自己写了一个 NPOI 的扩展库,于是想尝试看看 NPOI 的性能究竟怎么样,道听途说始终不如自己动手一试. 测试环境 测试工具: BenchmarkDotNet v0.11.5 EPPlus.Core.Extensions v2.3.2 EPPlus v4.5.3.1 测试代码:(Github 源码) [SimpleJob(launchCount: 1, warmupCount: 1, targetCou