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 ...
随机推荐
- istio-ServiceMesh解决方案
istio-ServiceMesh解决方案 istio(1):ServiceMesh解决方案-k8s安装istio istio(2):流量管理-基于不同版本访问规则控制 istio(3):流量管理-基 ...
- 大数相加-----杭电acm1002
#include<stdio.h> #include<string.h> int main() { ], ch2[]; ], num2[]; ; scanf("%d& ...
- 《python可以这样学》第二章
Python序列 列表与列表推导式 列表创建与删除 创建列表对象 >>> a_list = list((3, 5, 7, 9, 11)) >>> a_list = ...
- Go语言基础之面向对象编程中
1 Golang面向对象编程基本介绍 Golang仍然有面向对象编程的继承,封装和多态的特性,只是实现的方式和其它OPP语言不一样,随后分别介绍Golang对面向对象编程的三大特性是如何实现的. 2 ...
- Android之ScrollView嵌套ListView冲突 (listView只显示一行)
在ScrollView中嵌套使用ListView,ListView只会显示一行多一点.两者进行嵌套,即会发生冲突.由于ListView本身都继承于ScrollView,一旦在ScrollView中嵌套 ...
- android手机拍照旋转的问题
android开发中,遇到过手机拍照,明明是竖着拍的,显示的结果却是横这的,困扰了很久,找了很久找了一种解决方法: ExifInterface exifInterface = new ExifInte ...
- 联合索引在B+树上的存储结构及数据查找方式
能坚持别人不能坚持的,才能拥有别人未曾拥有的.关注编程大道公众号,让我们一同坚持心中所想,一起成长!! 引言 上一篇文章<MySQL索引那些事>主要讲了MySQL索引的底层原理,且对比了B ...
- win10上使用linux命令
(1)可以用windows自带的powershell,但是 ll,vim等命令不能使用 (2)Windows更新==>针对开发人员==>开启开发人员模式,然后在控制面板==>程序与功 ...
- 进阶之路 | 奇妙的Drawable之旅
前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 学习清单: Drawable简介 Drawable分类 自定义Drawable 一.为什么要学习Drawabl ...
- jquery带下拉列表的购物车组件封装
按照国际惯例先放效果图 安静的时候它长这样 等待加载时它长这样(功能是设置的按需加载,网速慢或者加载数据大时会出现) 加载之后购物车没有商品时这样 有商品长这样 接下来放代码: cart.html & ...