protected void Page_Load(object sender, EventArgs e)
{
var tempath = "~/Templates/科研项目申报汇总表.docx";
// 打开文件
FileStream fileStream = new FileStream(HttpContext.Current.Server.MapPath(tempath), FileMode.Open, FileAccess.Read, FileShare.Read);
// 读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
//创建新的word文档
XWPFDocument doc = new XWPFDocument(stream);
var beginDate = Convert.ToDateTime(string.Format("{0}-01-01 00:00:00", DateTime.Now.Year));
var endDate = Convert.ToDateTime(string.Format("{0}-12-31 23:59:59", DateTime.Now.Year));
var data = OA.Logic.PrjInfoBLL.GetPrjInfoCreateDate(beginDate, endDate);
var numYear1 = 0f;
var numYear2 = 0f;
var numReportFund = 0f;
for (int r = 0; r < data.Rows.Count; r++)
{
var trow = doc.Tables[0].CreateRow();
var row = data.Rows[r];
var cr = r + 1;
//项目编号
var C_Prj_Code = Vf(row["C_Prj_Code"]);
trow.GetTableCells()[0].SetText(C_Prj_Code);
//项目名称
var C_Prj_Name = Vf(row["C_Prj_Name"]);
trow.GetTableCells()[1].SetText(C_Prj_Name);
//项目研究内容摘要
var C_Expected_results = HelperTool.ReplaceHtmlTag(Vf(row["C_Expected_results"]));
trow.GetTableCells()[2].SetText(C_Expected_results);
//预期成果(考核指标) 近期研究成果
var c_study = HelperTool.ReplaceHtmlTag(Vf(row["c_study"]));
trow.GetTableCells()[3].SetText(c_study);
//项目总经费(万元)
var C_Report_Fund = Vf<float>(row["C_Report_Fund"], 0f);
trow.GetTableCells()[4].SetText(C_Report_Fund.ToString());
numReportFund += C_Report_Fund;
//2019年(万元)
var year1 = Vf<float>(row["第一年"], 0f);
trow.GetTableCells()[5].SetText(year1.ToString());
numYear1 += year1;
//2020年(万元)
var year2 = Vf<float>(row["第二年"], 0f);
trow.GetTableCells()[6].SetText(year2.ToString());
numYear2 += year2;
//承担单位
var C_Respone_Unit = Vf(row["C_Respone_Unit"]);
trow.GetTableCells()[7].SetText(C_Respone_Unit);
//项目负责人
var C_PrjContacter = Vf(row["C_PrjContacter"]);
trow.GetTableCells()[8].SetText(C_PrjContacter);
//起止时间
if (row["C_Plan_Begin"] != null && row["C_Plan_Begin"] != DBNull.Value && row["C_Plan_End"] != null && row["C_Plan_End"] != DBNull.Value)
{
var begDa = Convert.ToDateTime(row["C_Plan_Begin"]);
var endDa = Convert.ToDateTime(row["C_Plan_End"]);
var qzDate = string.Format("{0}—{1}", begDa.ToString("yyyy-MM"), endDa.ToString("yyyy-MM"));
trow.GetTableCells()[9].SetText(qzDate);
}
}
var rw = doc.Tables[0].CreateRow();
XWPFRun rIO2 = rw.GetTableCells()[2].AddParagraph().CreateRun();
rIO2.FontFamily = "黑体";
rIO2.FontSize = 15;
rIO2.IsBold = true;
rIO2.SetText("2019年" + data.Rows.Count + "个新立项目经费合计");
//项目总经费(万元)
XWPFRun rIO4 = rw.GetTableCells()[4].AddParagraph().CreateRun();
rIO4.FontFamily = "黑体";
rIO4.FontSize = 15;
rIO4.IsBold = true;
rIO4.SetText(numReportFund.ToString());
//2019年(万元)
XWPFRun rIO5 = rw.GetTableCells()[5].AddParagraph().CreateRun();
rIO5.FontFamily = "黑体";
rIO5.FontSize = 15;
rIO5.IsBold = true;
rIO5.SetText(numYear1.ToString());
//2020年(万元)
XWPFRun rIO6 = rw.GetTableCells()[6].AddParagraph().CreateRun();
rIO6.FontFamily = "黑体";
rIO6.FontSize = 15;
rIO6.IsBold = true;
rIO6.SetText(numYear2.ToString());
string name = "科研项目申报汇总表.docx";
string ReportFileName = Server.MapPath(name);
FileStream sw = File.Create(ReportFileName);
doc.Write(sw);
sw.Close();
FileInfo file = new FileInfo(ReportFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush();
File.Delete(ReportFileName);
}
使用NPOI操作WORD
猜你喜欢
转载自blog.csdn.net/qq_32109957/article/details/86577938
今日推荐
周排行