public void PrintRpt(List <CLDC_DataCore.Model.DnbModel.DnbInfo.MeterBasicInfo> Items, int ReportTao, int ReportType, string Jdyj, string zzbz) { //创建数据的XML文档 XmlDocument dom = CreateXmlDom(Items); // string rootPath = getFilePath("Res"); string xmlDataFile = rootPath + @"\Data.xml"; string xsltFile = rootPath + @"\template.xslt"; string outputDocument = rootPath + @"\report.docx"; //保存为XML文件 dom.Save(xmlDataFile); StringWriter stringWriter = new StringWriter(); XmlWriter xmlWriter = XmlWriter.Create(stringWriter); XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(xsltFile); transform.Transform(xmlDataFile, xmlWriter); XmlDocument newWordContent = new XmlDocument(); newWordContent.LoadXml(stringWriter.ToString()); WordTemplateParser.WordProcess process = new WordTemplateParser.WordProcess(); process.Create(outputDocument); if (process.UpdateBody(newWordContent)) { System.Diagnostics.Process.Start(outputDocument); } }
/// <summary> /// 设置书签的值 /// </summary> /// <param name="bookMark">书签节点</param> private void SetBookMarkValue(WordTemplateParser.Structs.BookMark bookMark, List <Dictionary <PrintDataType, object> > Items, int desc) { Dictionary <PrintDataType, object> currentMeter = Items[desc]; //获取当前表对象 if (bookMark == null || string.IsNullOrEmpty(bookMark.Text.ToLower())) { return; } string[] arrMarkValue = bookMark.Text.ToLower().Split(':'); //标签每个元素间用:隔开 if (arrMarkValue.Length == 6 && arrMarkValue[0] == "loop") { WordTemplateParser.WordProcess loopMarkParser = null; if (arrMarkValue[1] == "meter") { loopMarkParser = ParserLoopMarkMeter(Items, arrMarkValue); } else { loopMarkParser = ParserLoopMarkMeterData(currentMeter, arrMarkValue); } //回填解析后的文档 if (loopMarkParser != null) { bookMark.SetContentValue(loopMarkParser); } else { bookMark.SetValue("Invalid BookMark:" + bookMark.Text); } return; } //TODO:处理非对象标签 SetBookMarkValue(bookMark, currentMeter); }
//解析一只表数据标签 private WordTemplateParser.WordProcess ProcessOneDocument(string templatePath, Dictionary <PrintDataType, object> Item) { //建立一个文档处理对象 WordTemplateParser.WordProcess process = new WordTemplateParser.WordProcess(); //加载模板文件 process.Open(templatePath, true); //解析所有模板 process.ParserBookMarks(); //获取书签 WordTemplateParser.Structs.BookMark bookMark = process.Get(); while (bookMark != null) { //设置书签的值 SetBookMarkValue(bookMark, Item); //获取下一个书签 bookMark = process.Get(); } return(process); }
/// <summary> /// 打印报表 /// </summary> /// <param name="Items">要打印的电能表数据</param> /// <param name="ReportTao">报表样式名称</param> /// <param name="ReportType">报表类型</param> /// <param name="Jdyj">检定依据</param> /// <param name="zzbz">制造标准</param> public void PrintRpt(List <CLDC_DataCore.Model.DnbModel.DnbInfo.MeterBasicInfo> Items , int ReportTao , int ReportType , string Jdyj , string zzbz) { //获取打印类型名称 PrintTypeName = clsMain.getIniString("Type_" + ReportTao.ToString(), "TypeName", "", System.Windows.Forms.Application.StartupPath + @"\Res\Templet.ini").Split(',')[ReportType]; CLDC_DataCore.Function.TopWaiting.ShowWaiting("正在解析数据,请稍后....."); //整理数据 List <Dictionary <PrintDataType, object> > DicItems = new List <Dictionary <PrintDataType, object> >(); for (int i = 0; i < Items.Count; i++) { Dictionary <PrintDataType, object> DataItem = new Dictionary <PrintDataType, object>(); DataItem.Add(PrintDataType.基本信息, DicBasicInfo(Items[i])); //基本信息 DataItem.Add(PrintDataType.误差数据, Items[i].MeterErrors); //误差数据 DataItem.Add(PrintDataType.多功能数据, DicDgnInfo(Items[i])); //多功能数据 DataItem.Add(PrintDataType.走字数据, Items[i].MeterZZErrors); //走字数据 DataItem.Add(PrintDataType.影响量数据, Items[i].MeterSpecialErrs); //特殊检定数据 this.AddDWSetting((Dictionary <string, string>)DataItem[PrintDataType.基本信息]); //加入其它相关信息 DicItems.Add(DataItem); } this.chrJdyj = Jdyj; this.chrZzbz = zzbz; CLDC_DataCore.Function.TopWaiting.HideWaiting(); CLDC_DataCore.Function.TopWaiting.ShowWaiting("正在生成报表,请稍后....."); WordTemplateParser.DocumentManager documentManager = null; for (int i = 0; i < Items.Count; i++) { if (blnColIsAll) { break; //如果是打印一页多条,就不再需要进行下面的过程,直接跳出 } string Report_List = this.getReportTemplet(Items[i].Mb_intClfs == (int)CLDC_Comm.Enum.Cus_Clfs.单相 ? true : false, Items[i], ReportTao, ReportType); if (Report_List == string.Empty) { System.Windows.Forms.MessageBox.Show("没有对应的报表模板,无法完成打印操作...", "打印失败", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); return; } string[] Templet_Arr = Report_List.Split(','); if (Templet_Arr.Length < 2) { System.Windows.Forms.MessageBox.Show("模板配置错误,无法完成打印操作...", "打印失败", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); return; } if (!System.IO.File.Exists(string.Format(@"{0}\{1}", clsMain.getFilePath("Res"), Templet_Arr[0]))) { System.Windows.Forms.MessageBox.Show(string.Format(@"打开模板出现一个致命错误,没有找到对应 {0}\Res\{1}的模板文件...", ResRootPath, Templet_Arr[0]), "打印失败", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); return; } //第一步:加载dot文件 documentManager = new WordTemplateParser.DocumentManager(); string templatePata = string.Format(@"{0}\{1}", clsMain.getFilePath("Res"), Templet_Arr[0]); bool loadRet = documentManager.LoadTemplate(templatePata); if (!loadRet) { System.Windows.Forms.MessageBox.Show("加载报表模板文件[Dotx]文件失败,请确认模板是07格式", "打印失败", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); return; } CLDC_DataCore.Function.TopWaiting.HideWaiting(); CLDC_DataCore.Function.TopWaiting.ShowWaiting("正在准备生成报表....."); for (int Int_Mb = 1; Int_Mb < Templet_Arr.Length; Int_Mb++) { WordTemplateParser.WordProcess templateProcess = ProcessOneDocument(string.Format("{0}\\{1}", clsMain.getFilePath("Res"), Templet_Arr[Int_Mb]) , DicItems , i); //把解析好的模板文档添加到文档管理器中 documentManager.AddContent(templateProcess); } // documentManager.SaveDocument(); if (PrintInfo.Saving) //如果需要存盘 { DateTime date = DateTime.Now; DateTime.TryParse(Items[i].Mb_DatJdrq, out date); string savePath = string.Format(@"{0}\{1}.docx", this.SavePath(date), Items[i].Mb_ChrTxm + "_" + PrintTypeName); documentManager.SaveAs(savePath); } if (PrintInfo.Preview) { documentManager.ShowFileContent(); } } CLDC_DataCore.Function.TopWaiting.HideWaiting(); }
/// <summary> /// 解析循环标签【单表数据】 /// </summary> /// <param name="Item">当前电能表数据对象</param> /// <param name="arrMarkValue">标签文本</param> /// <returns>解析后的文档对象</returns> private WordTemplateParser.WordProcess ParserLoopMarkMeterData(Dictionary <PrintDataType, object> Item, string[] arrMarkValue) { if (arrMarkValue.Length != 6) { return(null); } int loopNum = 0; //每页数量 int parserNum = 0; //已经解析的数量 int.TryParse(arrMarkValue[2], out loopNum); string bodyFilePath = string.Format("{0}\\Res\\{1}.docx", ResRootPath, arrMarkValue[4]); if (string.IsNullOrEmpty(arrMarkValue[4])) { return(null); //没有主体,直接退出 } //开始循环处理 WordTemplateParser.WordProcess meterLoopParser = new WordTemplateParser.WordProcess(); // new WordTemplateParser.WordProcess(); if (!meterLoopParser.Create()) { return(null); } WordTemplateParser.WordProcess headerParser = null; // WordTemplateParser.WordProcess bodyParser = null; // WordTemplateParser.WordProcess footerParser = null; // if (!string.IsNullOrEmpty(arrMarkValue[3])) { //解析头 headerParser = new WordTemplateParser.WordProcess(); string headerFielPath = string.Format("{0}\\Res\\{1}.docx", ResRootPath, arrMarkValue[3]); headerParser.Open(headerFielPath, true);//循环头部分不进行解析。确认循环头部分不应该包括标签 } if (!string.IsNullOrEmpty(arrMarkValue[5])) { //解析脚 footerParser = new WordTemplateParser.WordProcess(); string footerFilePath = string.Format("{0}\\Res\\{1}.docx", ResRootPath, arrMarkValue[5]); footerParser.Open(footerFilePath, true);//循环头部分不进行解析。确认循环头部分不应该包括标签 } while (parserNum < 1) { if (parserNum % loopNum == 0) { if (parserNum == 0) { //处理分页 if (footerParser != null) { meterLoopParser.AddContent(footerParser); } } //加第二页表头 if (headerParser != null) { meterLoopParser.AddContent(headerParser); } } else { //解析循环体 bodyParser = ProcessOneDocument(bodyFilePath, Item); meterLoopParser.AddContent(bodyParser); } parserNum++; } if (footerParser != null) { meterLoopParser.AddContent(footerParser); } meterLoopParser.MegerDocument(string.Empty); return(meterLoopParser); }
/// <summary> /// 解析循环标签[表循环] /// </summary> /// <param name="Items"></param> /// <param name="arrMarkValue"></param> /// <returns></returns> private WordTemplateParser.WordProcess ParserLoopMarkMeter(List <Dictionary <PrintDataType, object> > Items, string[] arrMarkValue) { //arrMarkValue.Length=6 loop:meter:num;header:body:footer if (arrMarkValue.Length != 6) { return(null); } int loopNum = 0; //每页数量 int parserNum = 0; //已经解析的数量 int.TryParse(arrMarkValue[2], out loopNum); string bodyFilePath = string.Format("{0}\\Res\\{1}", ResRootPath, arrMarkValue[4]); if (!string.IsNullOrEmpty(arrMarkValue[4])) { return(null); //没有主体,直接退出 } //开始循环处理每一块表 WordTemplateParser.WordProcess meterLoopParser = null; // new WordTemplateParser.WordProcess(); WordTemplateParser.WordProcess headerParser = null; // WordTemplateParser.WordProcess bodyParser = null; // WordTemplateParser.WordProcess footerParser = null; // if (!string.IsNullOrEmpty(arrMarkValue[3])) { //解析头 headerParser = new WordTemplateParser.WordProcess(); string headerFielPath = string.Format("{0}\\Res\\{1}", ResRootPath, arrMarkValue[3]); headerParser.Open(headerFielPath, true);//循环头部分不进行解析。确认循环头部分不应该包括标签 // meterLoopParser = new WordTemplateParser.WordProcess(); meterLoopParser.Open(headerFielPath, true); } if (!string.IsNullOrEmpty(arrMarkValue[5])) { //解析脚 headerParser = new WordTemplateParser.WordProcess(); string footerFilePath = string.Format("{0}\\Res\\{1}", ResRootPath, arrMarkValue[5]); headerParser.Open(footerFilePath, true);//循环头部分不进行解析。确认循环头部分不应该包括标签 } //检测母板有没有加载 if (meterLoopParser == null) { //无头,直接加载第一块表的循环数据 meterLoopParser = ProcessOneDocument(bodyFilePath, Items, parserNum); parserNum++; } while (parserNum < Items.Count) { if (parserNum % loopNum == 0 && parserNum > 0) { //处理分页 if (footerParser != null) { meterLoopParser.AddContent(footerParser); } //加第二页表头 if (headerParser != null) { meterLoopParser.AddContent(headerParser); } } else { //解析循环体 bodyParser = ProcessOneDocument(bodyFilePath, Items, parserNum); meterLoopParser.AddContent(bodyParser); } parserNum++; } meterLoopParser.MegerDocument(string.Empty); return(meterLoopParser); }