效果图:

代码: 

/// <summary>
/// 导出Excel
/// </summary>
/// <param name="DeptId"></param>
[HttpPost]
public void ExportToExcel(int DeptId,List<FM_CostApply> CostApplyList, int beginYear, int beginMonth, int endYear, int endMonth)
{
foreach (var Item in CostApplyList)
{
CostApplyItem.AddRange(Item.FM_CostApplyItem);
}
var Project = CostApplyItem.GroupBy(a => a.FM_Project.ProjectName).ToList();
//创建工作簿
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
string[] headName = { "年度", "月", "日", "申请类型", "新科目名称", "部门名称", "项目名称", "凭证号", "摘要", "金额" };
string[] ColumnName = { "Year", "Month", "Day", "Type", "SubJectName", "DeptName", "ProjectName", "CardNum", "Summary", "Cost" };
//创建Sheet页
if (Project.Count > )
{
foreach (var proc in Project)
{
//该项目下申请的所有的科目
var SubjectName = CostApplyItem.Where(a => a.FM_Project.ProjectName == proc.Key).GroupBy(a => a.FM_SecondSubject.SubjectName).ToList();
try
{
//创建Sheet页
ISheet sheet = hssfworkbook.CreateSheet(proc.Key); //获取项目下的费用明细
List<CostApplyExcel> model = GetCostApply(proc.Key, CostApplyList); var Dic = model.GroupBy(a => a.SubJectName).ToDictionary(w => w.Key, r => r.ToList()); //集合转换为DataTable
DataTable dt = ConvtToDataTable.ToDataTable<CostApplyExcel>(model); int RowIndex = ; #region 如果为第一行
IRow IRow = sheet.CreateRow();
for (int h = ; h < ; h++)
{
ICell Icell = IRow.CreateCell(h);
Icell.SetCellValue(BeginDate.ToString("yyyy.MM") + "-" + EndDate.ToString("yyyy.MM") + " " + proc.Key + "项目汇总表"); ICellStyle style = hssfworkbook.CreateCellStyle();
//设置单元格的样式:水平对齐居中
style.Alignment = HorizontalAlignment.CENTER;
//新建一个字体样式对象
IFont font = hssfworkbook.CreateFont();
font.FontName = "宋体";
font.FontHeightInPoints = ;
//设置字体加粗样式
font.Boldweight = (short)FontBoldWeight.BOLD;
//使用SetFont方法将字体样式添加到单元格样式中
style.SetFont(font);
//将新的样式赋给单元格
Icell.CellStyle = style;
//合并单元格
sheet.AddMergedRegion(new CellRangeAddress(, , , ));
}
#endregion #region 表头
IRow Irows2 = sheet.CreateRow();
for (int j = ; j < ; j++)
{
ICell Icell2 = Irows2.CreateCell(j);
ICellStyle Istyle2 = hssfworkbook.CreateCellStyle();
//设置边框
Istyle2.BorderTop = BorderStyle.THIN;
Istyle2.BorderBottom = BorderStyle.THIN;
Istyle2.BorderLeft = BorderStyle.THIN;
Istyle2.BorderRight = BorderStyle.THIN;
//设置单元格的样式:水平对齐居中
Istyle2.Alignment = HorizontalAlignment.CENTER;
//新建一个字体样式对象
IFont Ifont2 = hssfworkbook.CreateFont();
Ifont2.FontName = "宋体";
Ifont2.FontHeightInPoints = ;
//设置字体加粗样式
Ifont2.Boldweight = (short)FontBoldWeight.BOLD;
//使用SetFont方法将字体样式添加到单元格样式中
Istyle2.SetFont(Ifont2);
//将新的样式赋给单元格
Icell2.CellStyle = Istyle2;
Icell2.SetCellValue(headName[j]);
}
#endregion foreach (var DicItem in Dic)
{
int SumStartRows = RowIndex + ; //求和的开始行
//集合转换为DataTable
DataTable table = ConvtToDataTable.ToDataTable<CostApplyExcel>(DicItem.Value);
for (int i = ; i <= DicItem.Value.Count; i++)
{
IRow row = sheet.CreateRow(RowIndex); if (i == DicItem.Value.Count)
{
for (int j = ; j < ; j++)
{
if (j == )
{
#region 汇总求和文字
ICell cell = row.CreateCell(j); DataRow TableRow = table.Rows[i - ];
string subName = TableRow[].ToString(); ICellStyle style = hssfworkbook.CreateCellStyle();
//设置边框
style.BorderTop = BorderStyle.THIN;
style.BorderBottom = BorderStyle.THIN;
style.BorderLeft = BorderStyle.THIN;
style.BorderRight = BorderStyle.THIN;
//设置单元格的样式:水平对齐居中
style.Alignment = HorizontalAlignment.CENTER;
//新建一个字体样式对象
IFont font = hssfworkbook.CreateFont();
font.FontName = "宋体";
font.FontHeightInPoints = ;
//设置字体加粗样式
font.Boldweight = (short)FontBoldWeight.BOLD;
//使用SetFont方法将字体样式添加到单元格样式中
style.SetFont(font);
//将新的样式赋给单元格
cell.CellStyle = style;
cell.SetCellValue(subName + " 汇总");
#endregion
}
else if (j == ) //合计
{
#region 汇总求和公式插入
ICell cell = row.CreateCell(j);
ICellStyle style = hssfworkbook.CreateCellStyle();
//设置边框
style.BorderTop = BorderStyle.THIN;
style.BorderBottom = BorderStyle.THIN;
style.BorderLeft = BorderStyle.THIN;
style.BorderRight = BorderStyle.THIN;
//设置单元格的样式:水平对齐居中
style.Alignment = HorizontalAlignment.CENTER;
//新建一个字体样式对象
IFont font = hssfworkbook.CreateFont();
font.FontName = "宋体";
font.FontHeightInPoints = ;
//使用SetFont方法将字体样式添加到单元格样式中
style.SetFont(font);
//将新的样式赋给单元格
cell.CellStyle = style; string format = "sum(";
for (int s = SumStartRows; s < (DicItem.Value.Count + SumStartRows); s++)
{
format += ("J" + s + ",");
}
format += ")"; cell.SetCellFormula(format); #endregion 汇总求和
}
else
{
#region 汇总求和-普通单元格
ICell cell = row.CreateCell(j);
ICellStyle style = hssfworkbook.CreateCellStyle();
//设置边框
style.BorderTop = BorderStyle.THIN;
style.BorderBottom = BorderStyle.THIN;
style.BorderLeft = BorderStyle.THIN;
style.BorderRight = BorderStyle.THIN;
//设置单元格的样式:水平对齐居中
style.Alignment = HorizontalAlignment.CENTER;
//新建一个字体样式对象
IFont font = hssfworkbook.CreateFont();
font.FontName = "宋体";
font.FontHeightInPoints = ;
//使用SetFont方法将字体样式添加到单元格样式中
style.SetFont(font);
//将新的样式赋给单元格
cell.CellStyle = style;
#endregion
}
}
}
else if (i < DicItem.Value.Count)
{
#region 插入值
DataRow TableRow = table.Rows[i];
for (int j = ; j < ; j++)
{
ICell cell = row.CreateCell(j);
ICellStyle style = hssfworkbook.CreateCellStyle();
//设置边框
style.BorderTop = BorderStyle.THIN;
style.BorderBottom = BorderStyle.THIN;
style.BorderLeft = BorderStyle.THIN;
style.BorderRight = BorderStyle.THIN;
//设置单元格的样式:水平对齐居中
style.Alignment = HorizontalAlignment.CENTER;
//设置单元格属性为文本
style.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
//新建一个字体样式对象
IFont font = hssfworkbook.CreateFont();
font.FontName = "宋体";
font.FontHeightInPoints = ;
//使用SetFont方法将字体样式添加到单元格样式中
style.SetFont(font);
//将新的样式赋给单元格
cell.CellStyle = style;
string val = TableRow[ColumnName[j]].ToString();
if (j == )
{
double cost = double.Parse(val);
cell.SetCellValue(cost);
}
else
{
cell.SetCellValue(val);
}
}
#endregion
}
RowIndex++;
}
}
for (int h = ; h < ; h++)
{
sheet.AutoSizeColumn(h);  //会按照值的长短 自动调节列的大小
}
}
catch (Exception ex) { }
}
}
else
{
//创建Sheet页
ISheet sheet = hssfworkbook.CreateSheet();
}
string Path = Server.MapPath("~/upload/财务导出");
if (!System.IO.Directory.Exists(Path))
System.IO.Directory.CreateDirectory(Path);
string fileName = DateTime.Now.ToFileTime() + ".xls";
using (FileStream file = new FileStream(Path + "\\" + fileName, FileMode.Create))
{
hssfworkbook.Write(file);  //创建test.xls文件。
file.Close();
result = ConfigurationManager.AppSettings["Websitet"] + "upload/财务导出/" + fileName;
}
HttpContext context = System.Web.HttpContext.Current;
context.Response.Write(result);
context.Response.End();
}

C# NPOI生成Excel文档(简单样式)的更多相关文章

  1. NPOI 2.1.1 系列(2) 使用NPOI读取List或者datatable数据生成 Excel文档 ;Npoi生成 xlsx 2007以上文档

    结合上一篇文章  NPOI 2.1.1 系列(1) 使用NPOI读取 Excel文档 ;NpoiExcelHelper 导入导出 2003格式 2007格式的 Excel; Npoi 导出 xlsx ...

  2. 后台生成EXCEL文档,自定义列

    后台生成EXCEL文档,自定义列 //response输出流处理 //设置编码.类型.文件名 getResponse().reset(); getResponse().setCharacterEnco ...

  3. php用PHPExcel库生成Excel文档的例子

    <?php require_once '../libs/PHPWord/PHPWord.php'; require_once '../libs/PHPWord/PHPWord/IOFactory ...

  4. POI 生成 word 文档 简单版(包括文字、表格、图片、字体样式设置等)

      POI 生成word 文档 一般有两种方法: ① word模板 生成word 文档 : ② 写代码直接生成 word 文档: 我这里演示的是第二种方法,即写代码生成 word文档,不多说废话,直接 ...

  5. NPOI 2.1.1 系列(1) 使用NPOI读取 Excel文档 ;NpoiExcelHelper 导入导出 2003格式 2007格式的 Excel; Npoi 导出 xlsx 格式

    下载地址 http://npoi.codeplex.com/releases 下面放一个 NPOIHelper 助手类吧,也不是我写的- NpoiExcelHelper 可以生成xlsx格式publi ...

  6. 【转】ExcelHelper类,用npoi读取Excel文档

    //------------------------------------------------------------------------------------- // All Right ...

  7. java生成excel文档

    要做一个后台自动化,要先预先生成一份文档,以下内容生成了文档 首先下载jxl.jar包,下载地址:http://download.csdn.net/detail/prstaxy/4469935 1.生 ...

  8. Python openpyxl : Excel 文档简单操作

    安装方法 使用 pip 或通过专门python IDE(如pyCharm)进行安装 其中pip安装方法,命令行输入:  pip install openpyxl 基本使用 第一步先是要导入 openp ...

  9. java、ruby、python、php等如何生成excel文档?

    excel在我们日常工作生活中会经常用到,通常我们都是用office软件去编写文档.但是对于格式一致的excel文档,如果还是使用人工完成,那绝不是我们软件工程师的姿态了~ 下面我就介绍一种方法,不需 ...

随机推荐

  1. 自定义自己的jQury插件

    对于一个商业插件来说,自定义插件的样式是必不可少的.我们可以通过我们自己输入不同的样式,来改变开发者的默认样式.比如说最常见的 width.height.url.color等等.要是没有这些自定义的东 ...

  2. Python3.6+Scrapy爬取知名技术文章网站

    爬取分析 伯乐在线已经提供了所有文章的接口,还有下一页的接口,所有我们可以直接爬取一页,再翻页爬. 环境搭建 Windows下安装Python: http://www.cnblogs.com/0bug ...

  3. Java Base64编码

    使用commons-codec, 下载地址 http://commons.apache.org/proper/commons-codec/ 下载commons-codec-1.12-bin.zip,解 ...

  4. Java自定义数据验证注解Annotation

    本文转载自:https://www.jianshu.com/p/616924cd07e6 Java注解Annotation用起来很方便,也越来越流行,由于其简单.简练且易于使用等特点,很多开发工具都提 ...

  5. Bootstrap&bxslider

    Bootstrap 规则 一.响应式布局 @media 二.图标.字体 @font-face 三.基本使用 实例: <!DOCTYPE html> <html lang=" ...

  6. vue之文本渲染

    Vue使用了基于HTML的模板语法,允许开发者声明式地将DOM绑定至底层Vue实例的数据.所有Vue的模板都是合法的HTML,所以能被遵循规范的浏览器和HTML解析器解析. 在前面,我们一直使用的是{ ...

  7. offse家族属性

    在JavaScript中,常用offset.scroll和client家族属性来表示元素的位置和大小相关属性,最近在网上找到了一张图来表示三者之间的关系,正好可以在此借鉴一下. 本次主要来看一下off ...

  8. 廖雪峰Java2面向对象编程-5包和classpath-1静态字段和方法

    1.静态字段 1.1定义:用static修饰的字段称为静态字段 普通字段在每个实例中都有自己的一个独立的空间 静态字段只有1个共享空间,所有实例都共享该字段. public class Person{ ...

  9. js代码要不要加分号

    最近写了点node的项目,习惯了go语言的后面不带分号,那么js的项目,要不要带分号呢 首先,我们来了解下javascript的自动填充规则 在说要不要写分号之前,先了解一下javascript自动填 ...

  10. Jmeter(二十九)Jmeter-Question之“Ant集成报告模板优化”

    也是在和朋友探讨的时候,发现一个问题,Jmeter在与Ant集成的时候,通常选用的模板是jmeter自带的两个样式表 该自带的样式,节省了大家搭建框架的时间,不需要自己重新写样式,当然也相对简洁: 做 ...