-
用NPOI操作EXCEL--生成一张工资单
这一节,我们将综合NPOI的常用功能(包括创建和填充单元格、合并单元格、设置单元格样式和利用公式),做一个工资单的实例。先看创建标题行的代码:
//写标题文本
HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
HSSFCell cellTitle = sheet1.CreateRow(0).CreateCell(0);
cellTitle.SetCellValue("XXX公司2009年10月工资单");
//设置标题行样式
HSSFCellStyle style = hssfworkbook.CreateCellStyle();
style.Alignment = HSSFCellStyle.ALIGN_CENTER;
HSSFFont font = hssfworkbook.CreateFont();
font.FontHeight = 20 * 20;
style.SetFont(font);
cellTitle.CellStyle = style;
//合并标题行
sheet1.AddMergedRegion(new Region(0, 0, 1, 6));
其中用到了我们前面讲的设置单元格样式和合并单元格等内容。接下来我们循环创建公司每个员工的工资单:
DataTable dt=GetData();
HSSFRow row;
HSSFCell cell;
HSSFCellStyle celStyle=getCellStyle();
HSSFPatriarch patriarch = sheet1.CreateDrawingPatriarch();
HSSFClientAnchor anchor;
HSSFSimpleShape line;
int rowIndex;
for (int i = 0; i < dt.Rows.Count; i++)
{
//表头数据
rowIndex = 3 * (i + 1);
row = sheet1.CreateRow(rowIndex);
cell = row.CreateCell(0);
cell.SetCellValue("姓名");
cell.CellStyle = celStyle;
cell = row.CreateCell(1);
cell.SetCellValue("基本工资");
cell.CellStyle = celStyle;
cell = row.CreateCell(2);
cell.SetCellValue("住房公积金");
cell.CellStyle = celStyle;
cell = row.CreateCell(3);
cell.SetCellValue("绩效奖金");
cell.CellStyle = celStyle;
cell = row.CreateCell(4);
cell.SetCellValue("社保扣款");
cell.CellStyle = celStyle;
cell = row.CreateCell(5);
cell.SetCellValue("代扣个税");
cell.CellStyle = celStyle;
cell = row.CreateCell(6);
cell.SetCellValue("实发工资");
cell.CellStyle = celStyle;
DataRow dr = dt.Rows[i];
//设置值和计算公式
row = sheet1.CreateRow(rowIndex + 1);
cell = row.CreateCell(0);
cell.SetCellValue(dr["FName"].ToString());
cell.CellStyle = celStyle;
cell = row.CreateCell(1);
cell.SetCellValue((double)dr["FBasicSalary"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(2);
cell.SetCellValue((double)dr["FAccumulationFund"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(3);
cell.SetCellValue((double)dr["FBonus"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(4);
cell.SetCellFormula(String.Format("$B{0}*0.08",rowIndex+2));
cell.CellStyle = celStyle;
cell = row.CreateCell(5);
cell.SetCellFormula(String.Format("SUM($B{0}:$D{0})*0.1",rowIndex+2));
cell.CellStyle = celStyle;
cell = row.CreateCell(6);
cell.SetCellFormula(String.Format("SUM($B{0}:$D{0})-SUM($E{0}:$F{0})",rowIndex+2));
cell.CellStyle = celStyle;
//绘制分隔线
sheet1.AddMergedRegion(new Region(rowIndex+2, 0, rowIndex+2, 6));
anchor = new HSSFClientAnchor(0, 125, 1023, 125, 0, rowIndex + 2, 6, rowIndex + 2);
line = patriarch.CreateSimpleShape(anchor);
line.ShapeType = HSSFSimpleShape.OBJECT_TYPE_LINE;
line.LineStyle = HSSFShape.LINESTYLE_DASHGEL;
}
其中为了文件打印为单元格增加了黑色边框的样式(如果不设置边框样式,打印出来后是没有边框的)。另外,注意循环过程中excel中的行号随数据源中的行号变化处理。完整代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.HPSF;
using NPOI.HSSF.Util;
using System.Data;
namespace Payroll
{
public class Program
{
static HSSFWorkbook hssfworkbook;
static void Main(string[] args)
{
InitializeWorkbook();
//写标题文本
HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
HSSFCell cellTitle = sheet1.CreateRow(0).CreateCell(0);
cellTitle.SetCellValue("XXX公司2009年10月工资单");
//设置标题行样式
HSSFCellStyle style = hssfworkbook.CreateCellStyle();
style.Alignment = HSSFCellStyle.ALIGN_CENTER;
HSSFFont font = hssfworkbook.CreateFont();
font.FontHeight = 20 * 20;
style.SetFont(font);
cellTitle.CellStyle = style;
//合并标题行
sheet1.AddMergedRegion(new Region(0, 0, 1, 6));
DataTable dt=GetData();
HSSFRow row;
HSSFCell cell;
HSSFCellStyle celStyle=getCellStyle();
HSSFPatriarch patriarch = sheet1.CreateDrawingPatriarch();
HSSFClientAnchor anchor;
HSSFSimpleShape line;
int rowIndex;
for (int i = 0; i < dt.Rows.Count; i++)
{
//表头数据
rowIndex = 3 * (i + 1);
row = sheet1.CreateRow(rowIndex);
cell = row.CreateCell(0);
cell.SetCellValue("姓名");
cell.CellStyle = celStyle;
cell = row.CreateCell(1);
cell.SetCellValue("基本工资");
cell.CellStyle = celStyle;
cell = row.CreateCell(2);
cell.SetCellValue("住房公积金");
cell.CellStyle = celStyle;
cell = row.CreateCell(3);
cell.SetCellValue("绩效奖金");
cell.CellStyle = celStyle;
cell = row.CreateCell(4);
cell.SetCellValue("社保扣款");
cell.CellStyle = celStyle;
cell = row.CreateCell(5);
cell.SetCellValue("代扣个税");
cell.CellStyle = celStyle;
cell = row.CreateCell(6);
cell.SetCellValue("实发工资");
cell.CellStyle = celStyle;
DataRow dr = dt.Rows[i];
//设置值和计算公式
row = sheet1.CreateRow(rowIndex + 1);
cell = row.CreateCell(0);
cell.SetCellValue(dr["FName"].ToString());
cell.CellStyle = celStyle;
cell = row.CreateCell(1);
cell.SetCellValue((double)dr["FBasicSalary"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(2);
cell.SetCellValue((double)dr["FAccumulationFund"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(3);
cell.SetCellValue((double)dr["FBonus"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(4);
cell.SetCellFormula(String.Format("$B{0}*0.08",rowIndex+2));
cell.CellStyle = celStyle;
cell = row.CreateCell(5);
cell.SetCellFormula(String.Format("SUM($B{0}:$D{0})*0.1",rowIndex+2));
cell.CellStyle = celStyle;
cell = row.CreateCell(6);
cell.SetCellFormula(String.Format("SUM($B{0}:$D{0})-SUM($E{0}:$F{0})",rowIndex+2));
cell.CellStyle = celStyle;
//绘制分隔线
sheet1.AddMergedRegion(new Region(rowIndex+2, 0, rowIndex+2, 6));
anchor = new HSSFClientAnchor(0, 125, 1023, 125, 0, rowIndex + 2, 6, rowIndex + 2);
line = patriarch.CreateSimpleShape(anchor);
line.ShapeType = HSSFSimpleShape.OBJECT_TYPE_LINE;
line.LineStyle = HSSFShape.LINESTYLE_DASHGEL;
}
WriteToFile();
}
static DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("FName",typeof(System.String));
dt.Columns.Add("FBasicSalary",typeof(System.Double));
dt.Columns.Add("FAccumulationFund", typeof(System.Double));
dt.Columns.Add("FBonus", typeof(System.Double));
dt.Rows.Add("令狐冲", 6000, 1000, 2000);
dt.Rows.Add("任盈盈", 7000, 1000, 2500);
dt.Rows.Add("林平之", 5000, 1000, 1500);
dt.Rows.Add("岳灵珊", 4000, 1000, 900);
dt.Rows.Add("任我行", 4000, 1000, 800);
dt.Rows.Add("风清扬", 9000, 5000, 3000);
return dt;
}
static HSSFCellStyle getCellStyle()
{
HSSFCellStyle cellStyle = hssfworkbook.CreateCellStyle();
cellStyle.BorderBottom = HSSFCellStyle.BORDER_THIN;
cellStyle.BorderLeft = HSSFCellStyle.BORDER_THIN;
cellStyle.BorderRight = HSSFCellStyle.BORDER_THIN;
cellStyle.BorderTop = HSSFCellStyle.BORDER_THIN;
return cellStyle;
}
static void WriteToFile()
{
//Write the stream data of workbook to the root directory
FileStream file = new FileStream(@"test.xls", FileMode.Create);
hssfworkbook.Write(file);
file.Close();
}
static void InitializeWorkbook()
{
hssfworkbook = new HSSFWorkbook();
//create a entry of DocumentSummaryInformation
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "NPOI Team";
hssfworkbook.DocumentSummaryInformation = dsi;
//create a entry of SummaryInformation
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "NPOI SDK Example";
hssfworkbook.SummaryInformation = si;
}
}
}
生成的Excel文件样式如下:
//写标题文本
HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
HSSFCell cellTitle = sheet1.CreateRow(0).CreateCell(0);
cellTitle.SetCellValue("XXX公司2009年10月工资单");
//设置标题行样式
HSSFCellStyle style = hssfworkbook.CreateCellStyle();
style.Alignment = HSSFCellStyle.ALIGN_CENTER;
HSSFFont font = hssfworkbook.CreateFont();
font.FontHeight = 20 * 20;
style.SetFont(font);
cellTitle.CellStyle = style;
//合并标题行
sheet1.AddMergedRegion(new Region(0, 0, 1, 6));
其中用到了我们前面讲的设置单元格样式和合并单元格等内容。接下来我们循环创建公司每个员工的工资单:
DataTable dt=GetData();
HSSFRow row;
HSSFCell cell;
HSSFCellStyle celStyle=getCellStyle();
HSSFPatriarch patriarch = sheet1.CreateDrawingPatriarch();
HSSFClientAnchor anchor;
HSSFSimpleShape line;
int rowIndex;
for (int i = 0; i < dt.Rows.Count; i++)
{
//表头数据
rowIndex = 3 * (i + 1);
row = sheet1.CreateRow(rowIndex);
cell = row.CreateCell(0);
cell.SetCellValue("姓名");
cell.CellStyle = celStyle;
cell = row.CreateCell(1);
cell.SetCellValue("基本工资");
cell.CellStyle = celStyle;
cell = row.CreateCell(2);
cell.SetCellValue("住房公积金");
cell.CellStyle = celStyle;
cell = row.CreateCell(3);
cell.SetCellValue("绩效奖金");
cell.CellStyle = celStyle;
cell = row.CreateCell(4);
cell.SetCellValue("社保扣款");
cell.CellStyle = celStyle;
cell = row.CreateCell(5);
cell.SetCellValue("代扣个税");
cell.CellStyle = celStyle;
cell = row.CreateCell(6);
cell.SetCellValue("实发工资");
cell.CellStyle = celStyle;
DataRow dr = dt.Rows[i];
//设置值和计算公式
row = sheet1.CreateRow(rowIndex + 1);
cell = row.CreateCell(0);
cell.SetCellValue(dr["FName"].ToString());
cell.CellStyle = celStyle;
cell = row.CreateCell(1);
cell.SetCellValue((double)dr["FBasicSalary"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(2);
cell.SetCellValue((double)dr["FAccumulationFund"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(3);
cell.SetCellValue((double)dr["FBonus"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(4);
cell.SetCellFormula(String.Format("$B{0}*0.08",rowIndex+2));
cell.CellStyle = celStyle;
cell = row.CreateCell(5);
cell.SetCellFormula(String.Format("SUM($B{0}:$D{0})*0.1",rowIndex+2));
cell.CellStyle = celStyle;
cell = row.CreateCell(6);
cell.SetCellFormula(String.Format("SUM($B{0}:$D{0})-SUM($E{0}:$F{0})",rowIndex+2));
cell.CellStyle = celStyle;
//绘制分隔线
sheet1.AddMergedRegion(new Region(rowIndex+2, 0, rowIndex+2, 6));
anchor = new HSSFClientAnchor(0, 125, 1023, 125, 0, rowIndex + 2, 6, rowIndex + 2);
line = patriarch.CreateSimpleShape(anchor);
line.ShapeType = HSSFSimpleShape.OBJECT_TYPE_LINE;
line.LineStyle = HSSFShape.LINESTYLE_DASHGEL;
}
其中为了文件打印为单元格增加了黑色边框的样式(如果不设置边框样式,打印出来后是没有边框的)。另外,注意循环过程中excel中的行号随数据源中的行号变化处理。完整代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.HPSF;
using NPOI.HSSF.Util;
using System.Data;
namespace Payroll
{
public class Program
{
static HSSFWorkbook hssfworkbook;
static void Main(string[] args)
{
InitializeWorkbook();
//写标题文本
HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
HSSFCell cellTitle = sheet1.CreateRow(0).CreateCell(0);
cellTitle.SetCellValue("XXX公司2009年10月工资单");
//设置标题行样式
HSSFCellStyle style = hssfworkbook.CreateCellStyle();
style.Alignment = HSSFCellStyle.ALIGN_CENTER;
HSSFFont font = hssfworkbook.CreateFont();
font.FontHeight = 20 * 20;
style.SetFont(font);
cellTitle.CellStyle = style;
//合并标题行
sheet1.AddMergedRegion(new Region(0, 0, 1, 6));
DataTable dt=GetData();
HSSFRow row;
HSSFCell cell;
HSSFCellStyle celStyle=getCellStyle();
HSSFPatriarch patriarch = sheet1.CreateDrawingPatriarch();
HSSFClientAnchor anchor;
HSSFSimpleShape line;
int rowIndex;
for (int i = 0; i < dt.Rows.Count; i++)
{
//表头数据
rowIndex = 3 * (i + 1);
row = sheet1.CreateRow(rowIndex);
cell = row.CreateCell(0);
cell.SetCellValue("姓名");
cell.CellStyle = celStyle;
cell = row.CreateCell(1);
cell.SetCellValue("基本工资");
cell.CellStyle = celStyle;
cell = row.CreateCell(2);
cell.SetCellValue("住房公积金");
cell.CellStyle = celStyle;
cell = row.CreateCell(3);
cell.SetCellValue("绩效奖金");
cell.CellStyle = celStyle;
cell = row.CreateCell(4);
cell.SetCellValue("社保扣款");
cell.CellStyle = celStyle;
cell = row.CreateCell(5);
cell.SetCellValue("代扣个税");
cell.CellStyle = celStyle;
cell = row.CreateCell(6);
cell.SetCellValue("实发工资");
cell.CellStyle = celStyle;
DataRow dr = dt.Rows[i];
//设置值和计算公式
row = sheet1.CreateRow(rowIndex + 1);
cell = row.CreateCell(0);
cell.SetCellValue(dr["FName"].ToString());
cell.CellStyle = celStyle;
cell = row.CreateCell(1);
cell.SetCellValue((double)dr["FBasicSalary"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(2);
cell.SetCellValue((double)dr["FAccumulationFund"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(3);
cell.SetCellValue((double)dr["FBonus"]);
cell.CellStyle = celStyle;
cell = row.CreateCell(4);
cell.SetCellFormula(String.Format("$B{0}*0.08",rowIndex+2));
cell.CellStyle = celStyle;
cell = row.CreateCell(5);
cell.SetCellFormula(String.Format("SUM($B{0}:$D{0})*0.1",rowIndex+2));
cell.CellStyle = celStyle;
cell = row.CreateCell(6);
cell.SetCellFormula(String.Format("SUM($B{0}:$D{0})-SUM($E{0}:$F{0})",rowIndex+2));
cell.CellStyle = celStyle;
//绘制分隔线
sheet1.AddMergedRegion(new Region(rowIndex+2, 0, rowIndex+2, 6));
anchor = new HSSFClientAnchor(0, 125, 1023, 125, 0, rowIndex + 2, 6, rowIndex + 2);
line = patriarch.CreateSimpleShape(anchor);
line.ShapeType = HSSFSimpleShape.OBJECT_TYPE_LINE;
line.LineStyle = HSSFShape.LINESTYLE_DASHGEL;
}
WriteToFile();
}
static DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("FName",typeof(System.String));
dt.Columns.Add("FBasicSalary",typeof(System.Double));
dt.Columns.Add("FAccumulationFund", typeof(System.Double));
dt.Columns.Add("FBonus", typeof(System.Double));
dt.Rows.Add("令狐冲", 6000, 1000, 2000);
dt.Rows.Add("任盈盈", 7000, 1000, 2500);
dt.Rows.Add("林平之", 5000, 1000, 1500);
dt.Rows.Add("岳灵珊", 4000, 1000, 900);
dt.Rows.Add("任我行", 4000, 1000, 800);
dt.Rows.Add("风清扬", 9000, 5000, 3000);
return dt;
}
static HSSFCellStyle getCellStyle()
{
HSSFCellStyle cellStyle = hssfworkbook.CreateCellStyle();
cellStyle.BorderBottom = HSSFCellStyle.BORDER_THIN;
cellStyle.BorderLeft = HSSFCellStyle.BORDER_THIN;
cellStyle.BorderRight = HSSFCellStyle.BORDER_THIN;
cellStyle.BorderTop = HSSFCellStyle.BORDER_THIN;
return cellStyle;
}
static void WriteToFile()
{
//Write the stream data of workbook to the root directory
FileStream file = new FileStream(@"test.xls", FileMode.Create);
hssfworkbook.Write(file);
file.Close();
}
static void InitializeWorkbook()
{
hssfworkbook = new HSSFWorkbook();
//create a entry of DocumentSummaryInformation
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "NPOI Team";
hssfworkbook.DocumentSummaryInformation = dsi;
//create a entry of SummaryInformation
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "NPOI SDK Example";
hssfworkbook.SummaryInformation = si;
}
}
}
生成的Excel文件样式如下:
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式