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. 【Python3爬虫】一次应对JS反调试的记录

    一.前言简介 在前面已经写过关于 JS 反调试的博客了,地址为:https://www.cnblogs.com/TM0831/p/12154815.html.但这次碰到的网站就不一样了,这个网站并不是 ...

  2. C# checked unchecked

    static void CheckedUnCheckedDemo() { int i = int.MaxValue; try { //checked //{ // Console.WriteLine( ...

  3. Ajax0002: 省市县三级联动案例

  4. 菜鸟linux

    //查看系统中文件的使用情况 df -h //查看当前目录下各个文件及目录占用空间大小 du -sh *//查看当期端口使用情况netstat -tlpn //find命令详见--https://ww ...

  5. php压缩文件zip格式并打包(单个或多个文件压缩)

    最近接到一个需求,就是选择多个文件下载时,不要一个一个下载,直接把多个文件打包成一个文件进行下载.我们项目是前后端分离,所以我写了个接口,让前端传参数,后台下载. 废话不多说,直接上代码: 先是压缩单 ...

  6. jQuery---五角星评分案例

    五角星评分案例 1. 鼠标经过li的时候,当前的位置是实心五角星,前面的是实心.当前位置后面的是空心.注意此处不能完全用链式编程写到底 2. 鼠标离开,comment的所有孩子变为空心五角星.额外,找 ...

  7. Android8.0自定义广播接收不到问题

    https://blog.csdn.net/kongqwesd12/article/details/78998151

  8. ArchLinux下electronssr无法启动的解决措施

    ArchLinux下electronssr无法启动的解决措施 今天重新配置electron-ssr时发现闪退(无法启动). 于是开始查错 首先是找到了目录位置 /usr/electron-ssr/el ...

  9. LNMP环境配置(2)

    php-fpm配置,Nginx配置 Nginx配置 默认虚拟主机 修改主配置文件 # vi  /usr/local/nginx/conf/nginx.conf 在最后 } 符号上面写入  includ ...

  10. 【python基础语法】运算符&if条件语句&while循环&for循环(第5天课堂笔记)

    """ if语法规则: if 条件比较语句1: # 条件成立的时候 会执行的代码 elif 条件比较语句2: # 条件2成立的时候 会执行的代码 else: # 条件不成 ...