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的更多相关文章

  1. 使用NPOI生成Excel级联列表

    目录 1    概要    1 2    磨刀不误砍柴工——先学会Excel中的操作    2 3    利用NPOI生成导入模板    7 3.1    设置workbook&sheet   ...

  2. .net利用NPOI生成excel文件

    整理代码,这个是生成excel文件,用的是HSSF的方式,只能生成65535行,256列的数据,如果要看office07之后的生成,之前的随笔里提过.这个是一个完整的过程. 首先是已经查找好的数据,这 ...

  3. Aspose.Cell和NPOI生成Excel文件

    1.使用Aspose.Cell生成Excel文件,Aspose.Cell是.NET组件控件,不依赖COM组件 1首先一点需要使用新建好的空Excel文件做模板,否则容易产生一个多出的警告Sheet 1 ...

  4. NPOI 生成 Excel

    前言 ​ 在 c# 中可以使用对应的com组件生成或操作excel,但前提是必须安装了Office Excel , 但服务器端不一定会安装Excel,而且它操作起来并不简单.但是,使用NPOI这个第三 ...

  5. NPOI 生成Excel (单元格合并、设置单元格样式:字段,颜色、设置单元格为下拉框并限制输入值、设置单元格只能输入数字等)

    NPIO源码地址:https://github.com/tonyqus/npoi NPIO使用参考:源码中的 NPOITest项目 下面代码包括: 1.包含多个Sheet的Excel 2.单元格合并 ...

  6. NPOI 生成 excel基本设置

    //设置页眉页脚 tempSheet.Header.Center = "2017-04-27"; tempSheet.Footer.Center = "√" + ...

  7. npoi生成excel流并在客户端下载(html+后台 )

    //前端页面 <body> <input type="button" value="导出Excel" class="button&q ...

  8. NPOI生成excel并下载

    NPOI文件下载地址:http://npoi.codeplex.com/ 将文件直接引用至项目中即可,,,,, 虽然网上资料很多,但有可能并找不到自己想要的功能,今天闲的没事,所以就稍微整理了一个简单 ...

  9. 通过 NPOI 生成 Excel

    HSSFWorkbook hssfworkbook; ISheet sheet1; public void BuildExcel() { hssfworkbook = new HSSFWorkbook ...

随机推荐

  1. Memcache 安装配置使用

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.                                                     ...

  2. make: *** No targets specified and no makefile found. Stop.错误

    # make make: *** No targets specified and no makefile found. Stop. # yum install gcc gcc-c++ gcc-g77 ...

  3. Windows下CMD常用命令

    清理DNS的缓存 ipconfig /flushdns 查看dns nslookup 重置socket服务 netsh winsock reset 重置tcp/ip协议栈 netsh int ip r ...

  4. solr常用操作及集成分词器或cdh集群部署说明

    首先,如果是从http://lucene.apache.org/solr/下载的solr,基本都是自带集成的jetty服务,不需要单独搭建tomcat环境,但是要注意jdk版本,直接解压通过cmd命令 ...

  5. smartforms设置表格脚标在最后一页显示

    用户打印采购订单时,末尾计算一个合计金额,但是有多页时,合计显示在了每一页,现在希望合计项只显示在表格最后一行就可以. smartforms调整表格,将总计放在脚标内,设置脚标输出打印[在表结束处]即 ...

  6. Python中autoescape标签使用详解

    1.spaceless标签:移除html标签中的空白字符.包括空格.tab键.换行符,示例代码如下: {% spaceless %}具体内容{% endspaceless %} 2.autoescap ...

  7. Bilibili手机端下载的Download文件批量转换为MP4软件【Bilibili_DownVideoToMp4】原创发布

    Bilibili手机端下载的Download文件批量转换为MP4软件[Bilibili_DownVideoToMp4]原创发布 起因 Bilibili手机端的视频下载下来只能在手机上看,手机屏幕太小看 ...

  8. Wix 快速开发安装包程序 (二)安装行为

    上一小节,主要介绍了构建最小级别的安装包,这个安装包所做的事情很简单,主要是打包好一些文件,然后放到用户机器的某个位置下面. 这个小节,主要是总结安装过程的各种行为如何使用Wix编写. 一.写注册表 ...

  9. 【算法】——递归:小白正在上楼梯,楼梯有n阶台阶,小白一次可以上1阶,2阶或者3阶,实现一个方法,计算小白有多少种走完楼梯的方式。

    分析:从最后一步分析,能有的情况有三种情况构成,写出如图所示的方程 //和斐波拉契相似 int void f(int n) { //考虑出口 ) ;//正常思路是返回0 ) ;//通过自己想可以得出只 ...

  10. JUnit套件测试(共通类测试)

    @RunWith(Suite.class)@Suite.SuiteClasses({ TestClass1.class, TestClass2.class })public class SuiteTe ...