C# 读写Excle Word - JackHu88/Comm GitHub Wiki

读写Excel有NPOI,读写Word有DocX

NPOI Excel

https://github.com/tonyqus/npoi

https://blog.csdn.net/chuntian1983/article/details/18399359

https://blog.csdn.net/qq_21425067/article/details/65635037

https://blog.csdn.net/xcymorningsun/article/details/77170512

https://blog.csdn.net/weixin_37585701/article/details/79135965

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 Word

https://blog.csdn.net/WuLex/article/details/81413096

DocX

https://github.com/xceedsoftware/DocX

http://developer.51cto.com/art/201302/380664.htm