diff --git a/插件库/Lskj.AutoCreatWord/CreateWordUtil.cs b/插件库/Lskj.AutoCreatWord/CreateWordUtil.cs index e071004..9ce81b9 100644 --- a/插件库/Lskj.AutoCreatWord/CreateWordUtil.cs +++ b/插件库/Lskj.AutoCreatWord/CreateWordUtil.cs @@ -20,32 +20,36 @@ namespace Lskj.AutoCreatWord /// 数据查询对象 /// public static Data.ADO.Abstract.AdsOperater DbOperator; - public static bool Create(string mainTempPath, string menuTempPath, string menuTempArray, DataSet dataSet, string savePath, string OAUrl, out string msg, bool createPdf = false) + public static bool Create(string mainTempPath, string menuTempPath, string menuTempArray, List dataSets, string savePath, string OAUrl, out string msg, bool createPdf = false) { msg = ""; bool result; try { - Document document = CreateWordUtil.InitTempDocuemnt(mainTempPath, menuTempPath, menuTempArray, dataSet); - CreateWordUtil.InitCreatBaseSql(document, dataSet, OAUrl); - CreateWordUtil.InitCreatBaseSqlDetail(document, dataSet, OAUrl); - CreateWordUtil.InitCreatTabSql(document, dataSet, OAUrl); - CreateWordUtil.InitCreatTabSqlDetail(document, dataSet, OAUrl); - CreateWordUtil.RemoveTableRow(document); + Document saveDocument = new Document(); + foreach (DataSet dataSet in dataSets) + { + CreateWordUtil.InitTempDocuemnt(saveDocument, mainTempPath, menuTempPath, menuTempArray, dataSet); + CreateWordUtil.InitCreatBaseSql(saveDocument, dataSet, OAUrl); + CreateWordUtil.InitCreatBaseSqlDetail(saveDocument, dataSet, OAUrl); + CreateWordUtil.InitCreatTabSql(saveDocument, dataSet, OAUrl); + CreateWordUtil.InitCreatTabSqlDetail(saveDocument, dataSet, OAUrl); + CreateWordUtil.RemoveTableRow(saveDocument); + } bool flag = !Directory.Exists(Path.GetDirectoryName(savePath)); if (flag) { Directory.CreateDirectory(Path.GetDirectoryName(savePath)); } string fileName = Path.GetDirectoryName(savePath) + "\\" + Path.GetFileNameWithoutExtension(savePath) + ".pdf"; - document.SaveToFile(savePath, FileFormat.Doc); + saveDocument.SaveToFile(savePath, FileFormat.Doc); if (createPdf) { - document.SaveToFile(fileName, FileFormat.PDF); + saveDocument.SaveToFile(fileName, FileFormat.PDF); } result = true; - document.Close(); - document.Dispose(); + saveDocument.Close(); + saveDocument.Dispose(); msg += "生成成功"; } catch (Exception ex) @@ -55,22 +59,26 @@ namespace Lskj.AutoCreatWord } return result; } - private static Document InitTempDocuemnt(string mainTempPath, string menuTempPath, string menuTempArray, DataSet dataSet) + private static Document InitTempDocuemnt(Document document, string mainTempPath, string menuTempPath, string menuTempArray, DataSet dataSet) { - Document document = new Document(); - Section mainSection = new Section(document); - mainSection.PageSetup.DifferentFirstPageHeaderFooter = false; + if (document == null) + { + document = new Document(); + } bool hasMainTemp = false; - document.Sections.Add(mainSection); try { if (!string.IsNullOrEmpty(mainTempPath) && File.Exists(mainTempPath)) { + Section mainSection = new Section(document); + mainSection.PageSetup.DifferentFirstPageHeaderFooter = false; + document.Sections.Add(mainSection); hasMainTemp = true; Document document2 = new Document(); document2.LoadFromFile(mainTempPath); foreach (Section section in document2.Sections) { + mainSection.PageSetup.Orientation = section.PageSetup.Orientation; mainSection.PageSetup.PageSize = section.PageSetup.PageSize; mainSection.PageSetup.Margins = section.PageSetup.Margins; CpoyHeaderFooter(section, mainSection, HeaderFooterType.FooterEven); @@ -85,11 +93,17 @@ namespace Lskj.AutoCreatWord } } } + else + { + Section mainSection = new Section(document); + mainSection.PageSetup.DifferentFirstPageHeaderFooter = false; + document.Sections.Add(mainSection); + } if (!string.IsNullOrEmpty(menuTempPath) && File.Exists(menuTempPath)) { if (hasMainTemp) { - mainSection.PageSetup.DifferentFirstPageHeaderFooter = true; + document.LastSection.PageSetup.DifferentFirstPageHeaderFooter = true; } Document document3 = new Document(); document3.LoadFromFile(menuTempPath); @@ -134,7 +148,7 @@ namespace Lskj.AutoCreatWord { Paragraph lastBreakParagraph = null; DocumentObject lastBreak = null; - foreach (Paragraph paragraph in mainSection.Paragraphs) + foreach (Paragraph paragraph in document.LastSection.Paragraphs) { foreach (DocumentObject docObject in paragraph.ChildObjects) { @@ -154,15 +168,26 @@ namespace Lskj.AutoCreatWord lastBreakParagraph.ChildObjects.Remove(lastBreak); } } - if (i == 0 && mainSection.Paragraphs.Count > 0) + if (i == 0 && document.LastSection.Paragraphs.Count > 0) { - mainSection.Paragraphs[mainSection.Paragraphs.Count - 1].AppendBreak(BreakType.PageBreak); + //document.LastSection.Paragraphs[document.LastSection.Paragraphs.Count - 1].AppendBreak(BreakType.PageBreak); } Section section = startSelection.GetAsOneRange().OwnerParagraph.OwnerTextBody.Owner as Section; - CpoyHeaderFooter(section, mainSection, HeaderFooterType.FooterEven); - CpoyHeaderFooter(section, mainSection, HeaderFooterType.FooterOdd); - CpoyHeaderFooter(section, mainSection, HeaderFooterType.HeaderEven); - CpoyHeaderFooter(section, mainSection, HeaderFooterType.HeaderOdd); + if (section.PageSetup.Orientation != document.LastSection.PageSetup.Orientation) + { + document.LastSection.Paragraphs[document.LastSection.Paragraphs.Count - 1].InsertSectionBreak(SectionBreakType.NoBreak); + document.LastSection.PageSetup.Orientation = section.PageSetup.Orientation; + document.LastSection.PageSetup.PageSize = section.PageSetup.PageSize; + document.LastSection.PageSetup.Margins = section.PageSetup.Margins; + } + Section mainSection = document.LastSection; + if (!moduleId.Equals("yw", StringComparison.OrdinalIgnoreCase)) + { + CpoyHeaderFooter(section, mainSection, HeaderFooterType.FooterEven); + CpoyHeaderFooter(section, mainSection, HeaderFooterType.FooterOdd); + CpoyHeaderFooter(section, mainSection, HeaderFooterType.HeaderEven); + CpoyHeaderFooter(section, mainSection, HeaderFooterType.HeaderOdd); + } bool isCopying = false; foreach (DocumentObject obj in section.Body.ChildObjects) { @@ -172,7 +197,9 @@ namespace Lskj.AutoCreatWord continue; //跳过开始标记 } if (obj == endSelection.GetAsOneRange().OwnerParagraph) + { break; //停止复制 + } if (isCopying) { //将内容添加到可替换模板 @@ -225,10 +252,29 @@ namespace Lskj.AutoCreatWord { if (dataColumn.ColumnName.Contains("img_"))//单行替换图片 { - Image image = InitCreatImage(dataRow[dataColumn] + "", OAUrl); - if (image == null) + Image image = null; + Size size = new Size(200, 200); + string picUrl = dataRow[dataColumn] + ""; + if (picUrl.StartsWith("@") && DbOperator != null) { - image = Base64ToImage(dataRow[dataColumn] + ""); + DataTable picTable = DbOperator.ExecuteDataTable(picUrl.TrimStart('@')); + if (picTable != null && picTable.Rows.Count > 0) + { + DataRow picRow = picTable.Rows[0]; + string path = picRow.Table.Columns.Contains("value") ? picRow["value"] + "" : ""; + int width = picRow.Table.Columns.Contains("width") && int.TryParse(picRow["width"] + "", out width) ? width : 200; + int height = picRow.Table.Columns.Contains("height") && int.TryParse(picRow["height"] + "", out height) ? height : 200; + size = new Size(width, height); + image = InitCreatImage(path, OAUrl); + } + } + else + { + image = InitCreatImage(picUrl, OAUrl); + if (image == null) + { + image = Base64ToImage(dataRow[dataColumn] + ""); + } } // 定位到占位符的位置并替换为图片 TextSelection[] selections = document.FindAllString(item, false, true); @@ -238,8 +284,8 @@ namespace Lskj.AutoCreatWord { DocPicture picture = new DocPicture(document); picture.LoadImage(image); - picture.Width = 200; - picture.Height = 200; + picture.Width = size.Width; + picture.Height = size.Height; // 用图片替换占位符 TextRange range = selection.GetAsOneRange(); Paragraph paragraph = range.OwnerParagraph; @@ -347,10 +393,29 @@ namespace Lskj.AutoCreatWord { if (dataColumn.ColumnName.Contains("img_"))//单行替换图片 { - Image image = InitCreatImage(dataRow[dataColumn] + "", OAUrl); - if (image == null) + Image image = null; + Size size = new Size(200, 200); + string picUrl = dataRow[dataColumn] + ""; + if (picUrl.StartsWith("@") && DbOperator != null) { - image = Base64ToImage(dataRow[dataColumn] + ""); + DataTable picTable = DbOperator.ExecuteDataTable(picUrl.TrimStart('@')); + if (picTable != null && picTable.Rows.Count > 0) + { + DataRow picRow = picTable.Rows[0]; + string path = picRow.Table.Columns.Contains("value") ? picRow["value"] + "" : ""; + int width = picRow.Table.Columns.Contains("width") && int.TryParse(picRow["width"] + "", out width) ? width : 200; + int height = picRow.Table.Columns.Contains("height") && int.TryParse(picRow["height"] + "", out height) ? height : 200; + size = new Size(width, height); + image = InitCreatImage(path, OAUrl); + } + } + else + { + image = InitCreatImage(picUrl, OAUrl); + if (image == null) + { + image = Base64ToImage(dataRow[dataColumn] + ""); + } } // 定位到占位符的位置并替换为图片 TextSelection[] selections = document.FindAllString(item, false, true); @@ -360,8 +425,8 @@ namespace Lskj.AutoCreatWord { DocPicture picture = new DocPicture(document); picture.LoadImage(image); - picture.Width = 200; - picture.Height = 200; + picture.Width = size.Width; + picture.Height = size.Height; // 用图片替换占位符 TextRange range = selection.GetAsOneRange(); Paragraph paragraph = range.OwnerParagraph; @@ -432,6 +497,26 @@ namespace Lskj.AutoCreatWord { if (tableCell.Paragraphs.Count == 0) { + if (disMergeDic.ContainsKey(table)) + { + disMergeDic[table].Add(tableRow.Cells.IndexOf(tableCell)); + } + else + { + disMergeDic.Add(table, new List() { tableRow.Cells.IndexOf(tableCell) }); + } + continue; + } + if (string.IsNullOrEmpty(tableCell.Paragraphs[0].Text)) + { + if (disMergeDic.ContainsKey(table)) + { + disMergeDic[table].Add(tableRow.Cells.IndexOf(tableCell)); + } + else + { + disMergeDic.Add(table, new List() { tableRow.Cells.IndexOf(tableCell) }); + } continue; } if (Regex.IsMatch(tableCell.Paragraphs[0].Text, pattern, RegexOptions.IgnoreCase) || Regex.IsMatch(tableCell.Paragraphs[0].Text, pattern1, RegexOptions.IgnoreCase)) @@ -747,8 +832,8 @@ namespace Lskj.AutoCreatWord DocPicture picture = paragraph.AppendPicture(image); picture.HorizontalAlignment = ShapeHorizontalAlignment.Center; picture.VerticalAlignment = ShapeVerticalAlignment.Center; - picture.Width = 200; - picture.Height = 200; + picture.Width = imageSize.Width; + picture.Height = imageSize.Height; } else if (addRowIndex > 0) { @@ -849,6 +934,26 @@ namespace Lskj.AutoCreatWord { if (tableCell.Paragraphs.Count == 0) { + if (disMergeDic.ContainsKey(table)) + { + disMergeDic[table].Add(tableRow.Cells.IndexOf(tableCell)); + } + else + { + disMergeDic.Add(table, new List() { tableRow.Cells.IndexOf(tableCell) }); + } + continue; + } + if (string.IsNullOrEmpty(tableCell.Paragraphs[0].Text)) + { + if (disMergeDic.ContainsKey(table)) + { + disMergeDic[table].Add(tableRow.Cells.IndexOf(tableCell)); + } + else + { + disMergeDic.Add(table, new List() { tableRow.Cells.IndexOf(tableCell) }); + } continue; } if (Regex.IsMatch(tableCell.Paragraphs[0].Text, pattern, RegexOptions.IgnoreCase) || Regex.IsMatch(tableCell.Paragraphs[0].Text, pattern1, RegexOptions.IgnoreCase)) @@ -1169,8 +1274,8 @@ namespace Lskj.AutoCreatWord DocPicture picture = paragraph.AppendPicture(image); picture.HorizontalAlignment = ShapeHorizontalAlignment.Center; picture.VerticalAlignment = ShapeVerticalAlignment.Center; - picture.Width = 200; - picture.Height = 200; + picture.Width = imageSize.Width; + picture.Height = imageSize.Height; } else if (addRowIndex > 0) { @@ -1506,34 +1611,4 @@ namespace Lskj.AutoCreatWord private static Image Base64ToImage(string base64String) { Image image = null; - try - { - if (!string.IsNullOrEmpty(base64String)) - { - string pattern = @".*;base64,"; - base64String = Regex.Replace(base64String, pattern, ""); - byte[] imageBytes = Convert.FromBase64String(base64String); - using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) - { - ms.Write(imageBytes, 0, imageBytes.Length); - image = Image.FromStream(ms, true); - } - } - } - catch (Exception) - { - } - return image; - } - /// - ///word表格数据配置的集合 - /// - protected class GridTarGet - { - public string number; - public Section targetSection; - public Table targetTable; - public int rowIndex; - } - } -} + \ No newline at end of file diff --git a/插件库/Lskj.AutoCreatWord/FrmMain.cs b/插件库/Lskj.AutoCreatWord/FrmMain.cs index c8d9b96..ccaf46c 100644 --- a/插件库/Lskj.AutoCreatWord/FrmMain.cs +++ b/插件库/Lskj.AutoCreatWord/FrmMain.cs @@ -119,25 +119,6 @@ namespace Lskj.AutoWordFile Task crateTask = new Task(() => { AutoCreatWord.CreateWordUtil.DbOperator = new Lskj.Data.ADO.Template.DbTemplate(string.Format("server={0};database={1};persist security info=true;user id=lserpAdmin;password=lserp110;max pool size = 512;", DBConfig.Instance.ServerName, DBConfig.Instance.DataBase), "system.data.sqlclient"); - AutoCreatWord.CreateWordUtil.Create(mainTempPath, menuTempPath, Model.MenuTempArray, SqlHelper.ExecuteDataSet(Model.MasterSql), savePdfPath, SystemInfo.Instance.OAUrl, out string msg); - cts.Cancel(); - if (this.InvokeRequired) - { - this.Invoke(new Action(() => - { - progressBar1.Value = 100; - Process.Start(savePdfPath); - Close(); - })); - } - }); - crateTask.Start(); - } - catch (Exception ex) - { - MessageUtil.Show(ex.Message); - Close(); - } - } - } -} + string[] masterSqls = Model.MasterSql.Trim('^').Split('^'); + List dataSets = new List(); + for \ No newline at end of file diff --git a/插件库/Lskj.AutoCreatWord/Lskj.AutoCreatWord.csproj b/插件库/Lskj.AutoCreatWord/Lskj.AutoCreatWord.csproj index 4f2418f..1a0401e 100644 --- a/插件库/Lskj.AutoCreatWord/Lskj.AutoCreatWord.csproj +++ b/插件库/Lskj.AutoCreatWord/Lskj.AutoCreatWord.csproj @@ -70,9 +70,6 @@ ..\..\Release\Lskj.Util.dll - - False - ..\..\引用DLL\Spire.Common.dll