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 ...
随机推荐
- 开源堡垒机jumpserver
开源堡垒机jumpserver 开源堡垒机jumpserver的安装 开源堡垒机jumpserver的配置和使用
- windows运行shell脚本
1. 环境变量的理解:快速找到程序并执行,配置在path的目录下有系统环境和用户环境,配置在此的只要目录路径就好,在cmd输入名字就会去此路径找匹配程序执行 2. 将git安装目录下的....\Git ...
- 杭电-------2047阿牛的eof牛肉串(C语言写)
/* 主要看最后一个是否为O,若为O,则倒数第二个不能为O,则为a[n-2]*1*2; 若不为O,则最后一个有两个选择则为a[n-1]*2 */ #include<stdio.h> ] = ...
- img 标签上的src 链接图片不存在时 怎么处理
// .html <img [src]="nzSrc" *ngIf="nzSrc && hasSrc" (error)="img ...
- width、height为auto或者100%的区别
一.规则 1. 某div不设置宽度,那么width默认为auto. 2. 某子元素div的width为100%(或者设置为等于父元素宽度的具体值,比如父元素width为100px,子元素width也设 ...
- 题解【[USACO05NOV]奶牛玩杂技】
\[ \texttt{Description} \] 有 \(n\) 头牛,每头牛都有自己的体重 \(W_i\) 和力量 \(S_i\) . 将这 \(n\) 头牛摞在一起,每头牛的压扁指数定义为:压 ...
- 使用PropTypes进行类型检查
原文地址 1.组件特殊属性——propTypes 对Component设置propTypes属性,可以为Component的props属性进行类型检查. import PropTypes from ' ...
- Jenkins集成jacoco收集单元测试覆盖率
Jenkins集成jacoco收集单元测试覆盖率 2020-02-28 目录 0 整体思路1 Jenkins创建JacocoIntegrateTestDemo项目2 配置源码管理3 配置Build4 ...
- Node中使用MongoDB
简介 MongoDB 中文文档 MongoDB是一个介于关系数据库和非关系数据库(nosql)之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. Mongoose 在Node中可以使用 Mo ...
- P2214 [USACO14MAR]哞哞哞Mooo Moo
链接:Miku ---------------------- 这道题还是个背包 --------------------- 首先看一下声音的组成,对于每一个农场的声音,它是由两部分组成的 :上一个农场 ...