https://www.cnblogs.com/weixing/p/7771332.html
https://www.cnblogs.com/weixing/category/844563.html
和Word相关
第一个文档的最后
第二个文档的最后
生成之后的文档
protected void Grid1ExportClick(object sender, EventArgs e)
{
//MainAID=1049
FileName.Text = DateTime.Now.ToString("yyyyMMddhhmmss") + ".docx";
//第一个模板
string SaveFilesPath = Server.MapPath(("~\UploadFiles\") + FileName.Text);
string DotFileName = Server.MapPath("~\dot\") + "检测报告模板.docx";
//第二个模板
string SavaFilesPath2 = Server.MapPath(("~\UploadFiles\") + FileName.Text);
string DotFileName2 = Server.MapPath("~\dot\") + "EquipTestItem.docx";
Novacode.DocX wordDoc2 = null;
Novacode.DocX wordDoc = null;//之前degug,wordDoc 是null的原因是因为没有初始化
int i = 0;
wordDoc = WordReportDocX.LoadTemplateDocument(DotFileName);//此句话是对wordDoc初始化
wordDoc2 = WordReportDocX.LoadTemplateDocument(DotFileName2);//第二个Word模板
//int i = 0;
List<dYWZCMainbyAID> list0 = Govaze.SQLServerDAL.Factory.getCHINADADAL().d_YWZC_dis_Main_ByAID(int.Parse(AID.Text), getUserName());
WordReportDocX.InsertText(wordDoc, "$Text1$", list0[0].SnID);//项目编号
WordReportDocX.InsertText(wordDoc, "$Text4$", Lab.Text);//实验室
WordReportDocX.InsertText(wordDoc, "$Text5$", null);//报告日期目前不知道是哪一个日期,所以赋值为空
WordReportDocX.InsertText(wordDoc, "$Text9$", list0[0].Text6);//样品送样方式
WordReportDocX.InsertText(wordDoc, "$Text10$", list0[0].SnID);//样品状态
//和样品有关的存储过程
XElement xl = new XElement("root", new XElement("Type", "MainSample")
, new XElement("AID", AID.Text)
, new XElement("UserName", getUserName())
);
DataTable dt = Govaze.SQLServerDAL.Factory.getDataXmlDAL().ProcDataTable(xl.ToString(), "d_YWZC_dis_Sample_ByMainAID");
WordReportDocX.InsertText(wordDoc, "$Text2$", dt.Rows[0]["Text1"].ToString());//样品名称
WordReportDocX.InsertText(wordDoc, "$Text3$", dt.Rows[0]["Text101"].ToString());//样品描述就是样品型号
//WordReportDocX.InsertText(wordDoc, "$Text5$", list0[0].SnID);
WordReportDocX.InsertText(wordDoc, "$Text6$", dt.Rows[0]["Num1"].ToString());//样品数量
WordReportDocX.InsertText(wordDoc, "$Text7$", dt.Rows[0]["SampleCode"].ToString()); //样品编号
WordReportDocX.InsertText(wordDoc, "$Text8$", dt.Rows[0]["DateTime1"].ToString());//接收日期
//和检测项目有关的存储过程
//DataTable dt3 = Govaze.SQLServerDAL.Factory.getDataXmlDAL().ProcDataTable("<root><Type>DisMainTestPrice</Type><MainAID>" + AID.Text + "</MainAID></root>", "DataXmlProject");
//WordReportDocX.InsertText(wordDoc2, "$Text11$", dt3.Rows[0]["BeginDate"].ToString() + "-" + dt3.Rows[0]["EndDate"].ToString());//检测日期
//WordReportDocX.InsertText(wordDoc2, "$Text12$", dt3.Rows[0]["TestItem"].ToString());//检测项目
//仪器仪表设备
XElement x1 = new XElement("root", new XElement("Type", "DisMainTestPrice"), new XElement("MainAID", AID.Text));
DataTable dt1 = Govaze.SQLServerDAL.Factory.getDataXmlDAL().ProcDataTable(x1.ToString(), "DataXmlProject");
int rowNumber = 2;
if (dt1.Rows.Count > 0)
{
WordReportDocX.AddRow(wordDoc2,1,dt1.Rows.Count);
}
foreach (DataRow row1 in dt1.Rows)
{
WordReportDocX.InsertCell(wordDoc2, 1, rowNumber, 1, (rowNumber - 1).ToString());
WordReportDocX.InsertCell(wordDoc2, 1, rowNumber, 2, row1["EquipName"].ToString());
WordReportDocX.InsertCell(wordDoc2, 1, rowNumber, 3, row1["Manufacturer"].ToString());//厂家
WordReportDocX.InsertCell(wordDoc2, 1, rowNumber, 4, row1["EquipModel"].ToString());//型号
WordReportDocX.InsertCell(wordDoc2, 1, rowNumber, 5, row1["ManufactureSnID"].ToString());//编号
WordReportDocX.InsertCell(wordDoc2, 1, rowNumber, 6, row1["ManufactureDate"].ToString().ToString());//有效日期
rowNumber++;
}
//先把wordDoc2插入到wordDoc中
wordDoc.Paragraphs[wordDoc.Paragraphs.Count - 1].InsertPageBreakAfterSelf();
wordDoc.InsertDocument(wordDoc2);
// 保存wordDoc
wordDoc.SaveAs(SaveFilesPath);
//临时路径加载 把加粗的部分给删除点击导出按钮就不会报错了
//wordDocTmp = WordReportDocX.LoadTemplateDocument(SaveFilesPath);
//wordDocTmp.Paragraphs[wordDocTmp.Paragraphs.Count - 1].InsertPageBreakAfterSelf();
//wordDocTmp.InsertDocument(wordDoc);
//Document Doc = new Document(SaveFilesPath);
//DocumentBuilder builder = new DocumentBuilder(Doc);
//Doc.Save(SaveFilesPath, SaveFormat.Docx);
WindowWord.Hidden = false;
PageContext.RegisterStartupScript(WindowWord.GetHidePostBackReference());
return;
}