private void ToExcel(string id)
{
//模板文件
string TempletFileName = Server.MapPath("template.xls");
//导出文件
string ReportFileName = Server.MapPath("out.xls");
FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
HSSFSheet ws = hssfworkbook.GetSheet("Sheet1");
//添加或修改WorkSheet里的数据
System.Data.DataTable dt = new System.Data.DataTable();
dt = DbHelperMySQLnew.Query("select * from t_jb_info where id='" + id + "'").Tables[0];
#region
if (dt.Rows.Count > 0)
{
if (!string.IsNullOrEmpty(dt.Rows[0]["blrq"].ToString()))
{
DateTime blrq = DateTime.Parse(dt.Rows[0]["blrq"].ToString());
ws.GetRow(7).GetCell(4).SetCellValue("56565");
ws.GetRow(7).GetCell(6).SetCellValue(blrq.Month.ToString());
ws.GetRow(7).GetCell(8).SetCellValue(blrq.Day.ToString());
ws.GetRow(1).GetCell(2).SetCellValue(blrq.Year.ToString());
ws.GetRow(1).GetCell(4).SetCellValue(blrq.Month.ToString());
ws.GetRow(1).GetCell(6).SetCellValue(blrq.Day.ToString());

//strnian = strblnian = blrq.Year.ToString();
//stryue = strblyue = blrq.Month.ToString();
//strri = strblri = blrq.Day.ToString();

}
ws.GetRow(1).GetCell(10).SetCellValue(id);

// strbh = id;
ws.GetRow(2).GetCell(1).SetCellValue(dt.Rows[0]["fyr"].ToString());
ws.GetRow(2).GetCell(4).SetCellValue(dt.Rows[0]["dizhi"].ToString());

ws.GetRow(2).GetCell(10).SetCellValue(dt.Rows[0]["lxdh"].ToString());
ws.GetRow(3).GetCell(1).SetCellValue(dt.Rows[0]["bfyr"].ToString());
ws.GetRow(5).GetCell(0).SetCellValue(dt.Rows[0]["wtnr"].ToString());
if (!string.IsNullOrEmpty(dt.Rows[0]["chuliyijian"].ToString()))
{
ws.GetRow(6).GetCell(1).SetCellValue(dt.Rows[0]["chuliyijian"].ToString());

}
if (!string.IsNullOrEmpty(dt.Rows[0]["ldps"].ToString()))
{
ws.GetRow(8).GetCell(1).SetCellValue(dt.Rows[0]["ldps"].ToString());

}
if (!string.IsNullOrEmpty(dt.Rows[0]["psrq"].ToString()))
{
DateTime blrq = DateTime.Parse(dt.Rows[0]["psrq"].ToString());
ws.GetRow(10).GetCell(4).SetCellValue(blrq.Year.ToString());
ws.GetRow(10).GetCell(6).SetCellValue(blrq.Month.ToString());
ws.GetRow(10).GetCell(8).SetCellValue(blrq.Day.ToString());

}
if (!string.IsNullOrEmpty(dt.Rows[0]["jgrq"].ToString()))
{
DateTime blrq = DateTime.Parse(dt.Rows[0]["jgrq"].ToString());
ws.GetRow(12).GetCell(4).SetCellValue(blrq.Year.ToString());
ws.GetRow(12).GetCell(6).SetCellValue(blrq.Month.ToString());
ws.GetRow(12).GetCell(8).SetCellValue(blrq.Day.ToString());

}
if (!string.IsNullOrEmpty(dt.Rows[0]["jieguozt"].ToString()))
{
ws.GetRow(11).GetCell(1).SetCellValue(dt.Rows[0]["jieguozt"].ToString());

}
}
#endregion
ws.ForceFormulaRecalculation = true;

using (FileStream filess = File.OpenWrite(ReportFileName))
{
hssfworkbook.Write(filess);
}

//filess.Close();

System.IO.FileInfo filet = new System.IO.FileInfo(ReportFileName);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("55.xls"));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", filet.Length.ToString());

// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";

// 把文件流发送到客户端
Response.WriteFile(filet.FullName);
// 停止页面的执行

Response.End();
}

NPOI 通过excel模板写入数据并导出的更多相关文章

  1. java poi 从服务器下载模板写入数据再导出

    最近写了一个,Excel 的 写入和导出.   需求是这样的.   在新建合同的时候,会有导出合同的数据,    导出的模板是固定的,,需要在模板里面写入合同的信息. first   :  下载模板 ...

  2. NPOI操作excel之写入数据到excel表

    在上一篇<NPOI操作excel之读取excel数据>我们把excel数据写入了datatable中,本篇就讲如何把datatable数据写入excel中. using System; u ...

  3. ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据

    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案   ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...

  4. 用NPOI从DataTable到Excel,向Excel模板填充数据

    DataTable---->Excel,填充数据 private IWorkbook workbook = null; private ISheet sheet = null; private ...

  5. 在服务端C#如何利用NPOI构建Excel模板

    目前本人接触过两种模板导出的方式:(1)C#利用NPOI接口制作Excel模板,在服务端用数据渲染模板(2)在前端利用前人搭建好的框架,利用office编写xml制作模板,在客户端进行数据的渲染,导出 ...

  6. POI向Excel中写入数据及追加数据

    import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...

  7. .Net NPOI 根据excel模板导出excel、直接生成excel

    一.根据Excel模板导出excel 1.导入NPOI.dll  2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...

  8. tablib把数据导出为Excel、JSON、CSV等格式的Py库(写入数据并导出exl)

    #tablib把数据导出为Excel.JSON.CSV等格式的Py库 #python 3 import tablib #定义列标题 headers = ('1列', '2列', '3列', '4列', ...

  9. NPOI从数据库中调取数据直接导出到EXCEL中

    关于NPOI NPOI是POI项目的.NET版本,是由@Tony Qu(http://tonyqus.cnblogs.com/)等大侠基于POI开发的,可以从http://npoi.codeplex. ...

随机推荐

  1. (转)Flex 布局教程:

    这个博客的内容比较新,多看看 http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html [语法篇] http://www.ruanyifeng. ...

  2. Hibernate配置文件的书写

    Hibernate主要配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibern ...

  3. noip第19课资料

  4. 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图

    [源码下载] 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 获 ...

  5. redis复习

    一起学习...

  6. debian安装tftp服务

    步骤1:安装 apt-get install tftp-hpa tftpd-hpa tftp-hpa是client,tftpd-hpaserver 步骤2:建立tftp目录 mkdir /tftpbo ...

  7. python中实现排序list

    作为一个非常实用的一种数据结构,排序链表用在很多方面,下面是它的python代码实现: from Node import * class OrderedList: def __init__(self) ...

  8. PyTorch(一)Basics

    PyTorch Basics import torch import torchvision import torch.nn as nn import numpy as np import torch ...

  9. redis pipeline 独占链接

    pipeline期间将“独占”链接,此期间将不能进行非“管道”类型的其他操作,直到pipeline关闭:如果你的pipeline的指令集很庞大,为了不干扰链接中的其他操作,你可以为pipeline操作 ...

  10. jquery选择器和属性

    jQuery的选择器 <html lang="en"> <head> <meta charset="UTF-8"> < ...