• NPOI 通过excel模板写入数据并导出


    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();
    }

  • 相关阅读:
    Resolving multicopy duplications de novo using polyploid phasing 用多倍体相位法解决多拷贝复制的新问题
    Efficient algorithms for polyploid haplotype phasing 多倍体单体型分型的有效算法
    PolyCluster: Minimum Fragment Disagreement Clustering for Polyploid Phasing 多聚类:用于多倍体的最小碎片不一致聚类
    MicroRNA in Control of Gene Expression: An Overview of Nuclear Functions 微RNA控制基因表达:核功能概述
    点9图 Android设计中如何切图.9.png
    【Android开发经验】android:windowSoftInputMode属性具体解释
    Android存储路径你了解多少?
    Android 各种路径详细说明
    自定义Dialog的详细步骤(实现自定义样式一般原理)
    Android:图解四种启动模式 及 实际应用场景解说
  • 原文地址:https://www.cnblogs.com/raincedar/p/10341772.html
Copyright © 2020-2023  润新知