NPOI 生成Excel
private void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txt_FilePath.Text) || txt_FilePath.Text.Trim().Substring(txt_FilePath.Text.Trim().Length - ) != ".xls")
{
MessageBox.Show("请选择要导出文件的路径和文件名,例如d:\\file.xls!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
} try
{
HSSFWorkbook book = new HSSFWorkbook(); //表格样式
//header
HSSFCellStyle headercs = (HSSFCellStyle)book.CreateCellStyle();
headercs.VerticalAlignment = VerticalAlignment.Center;
headercs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
headercs.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
headercs.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
headercs.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
headercs.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
headercs.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
headercs.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
headercs.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
headercs.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; HSSFFont headerFont = (HSSFFont)book.CreateFont();
headerFont.FontHeightInPoints = ;
headerFont.Color = HSSFColor.Black.Index;
headerFont.FontName = "宋体"; headercs.SetFont(headerFont); //文本 Style
HSSFCellStyle stringcs = (HSSFCellStyle)book.CreateCellStyle();
stringcs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
stringcs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
stringcs.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
stringcs.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
stringcs.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stringcs.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
stringcs.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
stringcs.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
stringcs.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
stringcs.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; //数字style
HSSFCellStyle decimalcs = (HSSFCellStyle)book.CreateCellStyle();
decimalcs.DataFormat = book.CreateDataFormat().GetFormat("#0");
decimalcs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
decimalcs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
decimalcs.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
decimalcs.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
decimalcs.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
decimalcs.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
decimalcs.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
decimalcs.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
decimalcs.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
decimalcs.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; //数字style
HSSFCellStyle decimalcs1 = (HSSFCellStyle)book.CreateCellStyle();
decimalcs1.DataFormat = book.CreateDataFormat().GetFormat("#0.00");
decimalcs1.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
decimalcs1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
decimalcs1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
decimalcs1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
decimalcs1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
decimalcs1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
decimalcs1.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
decimalcs1.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
decimalcs1.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
decimalcs1.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; //百分数style
HSSFCellStyle percentagecs = (HSSFCellStyle)book.CreateCellStyle();
//percentagecs.DataFormat = book.CreateDataFormat().GetFormat("##%");
percentagecs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
percentagecs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right;
percentagecs.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
percentagecs.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
percentagecs.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
percentagecs.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
percentagecs.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
percentagecs.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
percentagecs.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
percentagecs.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; HSSFSheet sheet2 = (HSSFSheet)book.CreateSheet("病区排程明细"); //列名
string[] headerString = { "类", "病区", "床号", "病历号", "姓名", "表单号", "检查科室", "检查内容", "排程日期", "开单日期", "状态" }; HSSFRow row0_2 = (HSSFRow)sheet2.CreateRow();
row0_2.CreateCell().SetCellValue("病区排程明细");
row0_2.GetCell().CellStyle = headercs; sheet2.AddMergedRegion(new CellRangeAddress(, , , headerString.Length - )); HSSFRow row1_2 = (HSSFRow)sheet2.CreateRow();
for (int i = ; i < headerString.Length; i++)
{
row1_2.CreateCell(i).SetCellValue(headerString[i]);
row1_2.GetCell(i).CellStyle = headercs;
}
for (int i = ; i < dtInfo.Rows.Count; i++)
{
HSSFRow row = (HSSFRow)sheet2.CreateRow(i + );
//类
row.CreateCell().SetCellValue(dtInfo.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//病区,
row.CreateCell().SetCellValue(dtInfo.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//床号,
row.CreateCell().SetCellValue(dtInfo.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//病历号,
row.CreateCell().SetCellValue(dtInfo.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//姓名,
row.CreateCell().SetCellValue(dtInfo.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//表单号
row.CreateCell().SetCellValue(dtInfo.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//检查科室,
row.CreateCell().SetCellValue(dtInfo.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//检查内容,
row.CreateCell().SetCellValue(dtInfo.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//排程日期,
row.CreateCell().SetCellValue(dtInfo.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//申请单号,
row.CreateCell().SetCellValue(dtInfo.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//状态,
row.CreateCell().SetCellValue(dtInfo.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
}
sheet2.SetColumnWidth(, * );
sheet2.SetColumnWidth(, * );
sheet2.SetColumnWidth(, * );
sheet2.SetColumnWidth(, * );
sheet2.SetColumnWidth(, * );
sheet2.SetColumnWidth(, * );
sheet2.SetColumnWidth(, * );
sheet2.SetColumnWidth(, * );
sheet2.SetColumnWidth(, * );
sheet2.SetColumnWidth(, * );
sheet2.SetColumnWidth(, * ); HSSFSheet sheetC = (HSSFSheet)book.CreateSheet("取消排程明细");
string[] headerStringC = { "排程日期", "取消日期", "类", "病区", "床号", "病历号", "姓名", "检查科室", "检查内容", "备注" }; HSSFRow row0_2C = (HSSFRow)sheetC.CreateRow();
row0_2C.CreateCell().SetCellValue("取消排程明细");
row0_2C.GetCell().CellStyle = headercs; sheetC.AddMergedRegion(new CellRangeAddress(, , , headerStringC.Length - ));
HSSFRow row1_2C = (HSSFRow)sheetC.CreateRow();
for (int i = ; i < headerStringC.Length; i++)
{
row1_2C.CreateCell(i).SetCellValue(headerStringC[i]);
row1_2C.GetCell(i).CellStyle = headercs;
}
for (int i = ; i < dtInfoC.Rows.Count; i++)
{
HSSFRow row = (HSSFRow)sheetC.CreateRow(i + );
//排程日期
row.CreateCell().SetCellValue(dtInfoC.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//取消日期,
row.CreateCell().SetCellValue(dtInfoC.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//类,
row.CreateCell().SetCellValue(dtInfoC.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//病区,
row.CreateCell().SetCellValue(dtInfoC.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//床号,
row.CreateCell().SetCellValue(dtInfoC.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//病历号
row.CreateCell().SetCellValue(dtInfoC.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//姓名,
row.CreateCell().SetCellValue(dtInfoC.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//检查科室,
row.CreateCell().SetCellValue(dtInfoC.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//检查内容,
row.CreateCell().SetCellValue(dtInfoC.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs;
//备注,
row.CreateCell().SetCellValue(dtInfoC.Rows[i].Cells[].Value.ToString());
row.GetCell().CellStyle = stringcs; }
sheetC.SetColumnWidth(, * );
sheetC.SetColumnWidth(, * );
sheetC.SetColumnWidth(, * );
sheetC.SetColumnWidth(, * );
sheetC.SetColumnWidth(, * );
sheetC.SetColumnWidth(, * );
sheetC.SetColumnWidth(, * );
sheetC.SetColumnWidth(, * );
sheetC.SetColumnWidth(, * );
sheetC.SetColumnWidth(, * ); //判断是否安装EXCEL,若是已安装,则呼叫EXCEL打开
Type officeType = Type.GetTypeFromProgID("Excel.Application");
if (officeType == null)
{
SaveFileDialog Sfd = new SaveFileDialog();
Sfd.Filter = "Excel文件(*.xls)|*.xls";
Sfd.FileName = "SCH_UCF_0090" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
if (Sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FileStream file3 = new FileStream(Sfd.FileName, FileMode.Create);
book.Write(file3);
file3.Close();
book = null;
}
}
else
{
String tempPath = txt_FilePath.Text;
FileStream tmpxls = new FileStream(tempPath, FileMode.Create);
book.Write(tmpxls);
tmpxls.Close();
book = null;
Process.Start(tempPath);
}
//MessageHandling.ShowInfoMsg("CMMCMMB300", new String[] { "资料导出成功!" });
//MessageBox.Show("资料导出成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
catch (Exception ex)
{
//MessageHandling.ShowWarnMsg("CMMCMMB300", new String[] { ex.Message });
}
}
NPOI 生成Excel的更多相关文章
- 使用NPOI生成Excel级联列表
		
目录 1 概要 1 2 磨刀不误砍柴工——先学会Excel中的操作 2 3 利用NPOI生成导入模板 7 3.1 设置workbook&sheet ...
 - .net利用NPOI生成excel文件
		
整理代码,这个是生成excel文件,用的是HSSF的方式,只能生成65535行,256列的数据,如果要看office07之后的生成,之前的随笔里提过.这个是一个完整的过程. 首先是已经查找好的数据,这 ...
 - Aspose.Cell和NPOI生成Excel文件
		
1.使用Aspose.Cell生成Excel文件,Aspose.Cell是.NET组件控件,不依赖COM组件 1首先一点需要使用新建好的空Excel文件做模板,否则容易产生一个多出的警告Sheet 1 ...
 - NPOI 生成 Excel
		
前言  在 c# 中可以使用对应的com组件生成或操作excel,但前提是必须安装了Office Excel , 但服务器端不一定会安装Excel,而且它操作起来并不简单.但是,使用NPOI这个第三 ...
 - NPOI 生成Excel (单元格合并、设置单元格样式:字段,颜色、设置单元格为下拉框并限制输入值、设置单元格只能输入数字等)
		
NPIO源码地址:https://github.com/tonyqus/npoi NPIO使用参考:源码中的 NPOITest项目 下面代码包括: 1.包含多个Sheet的Excel 2.单元格合并 ...
 - NPOI 生成 excel基本设置
		
//设置页眉页脚 tempSheet.Header.Center = "2017-04-27"; tempSheet.Footer.Center = "√" + ...
 - npoi生成excel流并在客户端下载(html+后台 )
		
//前端页面 <body> <input type="button" value="导出Excel" class="button&q ...
 - NPOI生成excel并下载
		
NPOI文件下载地址:http://npoi.codeplex.com/ 将文件直接引用至项目中即可,,,,, 虽然网上资料很多,但有可能并找不到自己想要的功能,今天闲的没事,所以就稍微整理了一个简单 ...
 - 通过 NPOI  生成 Excel
		
HSSFWorkbook hssfworkbook; ISheet sheet1; public void BuildExcel() { hssfworkbook = new HSSFWorkbook ...
 
随机推荐
- MongoDB 复本集搭建
			
复制集的特点 数据一致性 主是唯一的,但不是固定的 没有MySQL那样的双主结构 大多数原则,集群存活节点小于等于二分之一时集群不可写,只可读. 是否能选举出新的主节点,是由当前复制集成员存活量 ...
 - 连接数据库的方法---ODBC
			
2012-12-10 11:50 (分类:计算机程序) 技术博客,对抗遗忘…… 1.ODBC Open Database Connectivity 1.1 简介:提供了一组对数据库访问的标准 ...
 - [CentOS7]sed 指定字符前后添加内容
			
指定字符前面添加 sed -i 's/指定的字符/需要添加的字符&/' 文件名称 修改Linux启动配置 将 GRUB_CMDLINE_LINUX="rd.lvm.lv=centos ...
 - [javascript] test() 方法进行正则验证
			
test() 方法用于检测一个字符串是否匹配某个模式 最近遇到的某业务中进行发票抬头的正则验证如下: console.log(/^[a-zA-Z\u4e00-\u9fa5\s()()<>& ...
 - redis两种持久化方式RDB和AOF
			
目录 前言 1. Redis 数据库结构 2. RDB 持久化 2.1. RDB 的创建和载入 2.1.1. 手动触发保存 SAVE 命令 BGSAVE 命令 SAVE 和 BGSAVE 的比较 2. ...
 - 如何优雅的封装requests
			
搭建接口自动化测试框架,一般都要对post/get请求做封装. 一般的封装过程是, class MyRequest: def my_post(): """do somet ...
 - 剑指offer-面试题58_2-左旋转字符串-字符串
			
/* 题目: 将字符串的前sep个字符转移到字符串尾部. */ /* 思路: 更好的方法: 先翻转前sep个字符,再翻转后面的字符,最后全体翻转. */ #include<iostream> ...
 - 《Adaptive Density Map Generation for Crowd Counting》密集人群检测论文笔记
			
背景 密度图\(D_g\)的生成对于最终网络预测结果\(D_e\)至关重要,但是密度图\(D_g\)生成的过程中,高斯核的大小常常是手动设定的,并且对于不同的数据集,核大小和形状通常不一样.这些手动选 ...
 - 1-2.Kubectl命令行工具
			
1.kubectl用法 $~: kubectl [command] [TYPE] [NAME] [flags] [command] 子命令.用于操作Kubernetes集群资源对象. 可取值:[cre ...
 - Android实战项目——家庭记账本(二)
			
今天主要是对昨天做的添加账单信息的功能做了完善,实现了数据库的相关操作,如图是对已添加的账单信息的总结显示. 目前实现了通过日期进行汇总的功能,如上图中的各项item就是通过对所有账单信息进行按日期汇 ...