3.3 用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(0016));
复制代码

其中用到了我们前面讲的设置单元格样式和合并单元格等内容。接下来我们循环创建公司每个员工的工资单:
复制代码

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+20, rowIndex+26));
    anchor 
= new HSSFClientAnchor(012510231250, rowIndex + 26, rowIndex + 2);
    line 
= patriarch.CreateSimpleShape(anchor);
    line.ShapeType 
= HSSFSimpleShape.OBJECT_TYPE_LINE;
    line.LineStyle 
= HSSFShape.LINESTYLE_DASHGEL;

}
复制代码
其中为了文件打印为单元格增加了黑色边框的样式(如果不设置边框样式,打印出来后是没有边框的)。另外,注意循环过程中excel中的行号随数据源中的行号变化处理。完整代码如下:
Code
生成的Excel文件样式如下:

 

返回目录

 

posted @   atao.xiang  阅读(20590)  评论(11编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
< 2009年10月 >
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7
点击右上角即可分享
微信分享提示