using Lskj.Business; using Lskj.Business.Impl; using Lskj.Control; using Lskj.Core; using Lskj.Model; using Lskj.Util; using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Linq; using System.Net; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace Lskj.AutoWordFile { public partial class FrmMain : BaseForm { private bool NewInitSuccess = false; private int fileCount; /// /// 基础对象 /// public ModuleModel ModuleModel; /// /// 本地打开word路径 /// public string WordPath; /// /// 调用动态链接库默认传递参数 /// public DynamicAutoCreate Model; // 可替换模板文档 public Document document; // 替换的数据集合 public DataSet ds; /// /// 模块编号 /// public string ModelCode; public FrmMain() { InitializeComponent(); this.Load += OnFrmMainLoad; } /// /// 窗口初始化 /// /// /// private void OnFrmMainLoad(object sender, EventArgs e) { try { string mainTempPath = $@"{PubUtil.AbsolutelyPath}\WordFile\{Model.MainTempPath}.doc"; if (!File.Exists(mainTempPath)) { mainTempPath = $@"{PubUtil.AbsolutelyPath}\WordFile\{Model.MainTempPath}.docx"; if (!File.Exists(mainTempPath)) { //throw new Exception($"模板文件{mainTempPath}不存在"); } else { Model.MainTempPath = $"{Model.MainTempPath}.docx"; } } else { Model.MainTempPath = $"{Model.MainTempPath}.doc"; } string menuTempPath = $@"{PubUtil.AbsolutelyPath}\WordFile\{Model.MenuTempPath}.doc"; if (!File.Exists(menuTempPath)) { menuTempPath = $@"{PubUtil.AbsolutelyPath}\WordFile\{Model.MenuTempPath}.docx"; if (!File.Exists(menuTempPath)) { //throw new Exception($"模板文件{menuTempPath}不存在"); } else { Model.MenuTempPath = $"{Model.MenuTempPath}.doc"; } } else { Model.MenuTempPath = $"{Model.MenuTempPath}.doc"; } string fileName = Path.GetFileName(Model.MainTempPath); fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "word文档" + fileName; string saveFilePath = $@"{PubUtil.AbsolutelyPath}\Tempfiles\{fileName}"; string savePdfPath = $@"{Path.GetDirectoryName(saveFilePath)}\{Path.GetFileNameWithoutExtension(saveFilePath)}.doc"; CancellationTokenSource cts = new CancellationTokenSource(); CancellationToken token = cts.Token; Task progressTask = new Task(() => { for (int i = 1; i <= 99; i++) { token.ThrowIfCancellationRequested(); Invoke(new Action(() => { progressBar1.Value = i; })); Thread.Sleep(1000); } }, token); progressTask.Start(); 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"); string[] masterSqls = Model.MasterSql.Trim('^').Split('^'); List dataSets = new List(); foreach (string masterSql in masterSqls) { if (string.IsNullOrEmpty(masterSql)) continue; dataSets.Add(SqlHelper.ExecuteDataSet(masterSql)); } AutoCreatWord.CreateWordUtil.Create(mainTempPath, menuTempPath, Model.MenuTempArray, dataSets, 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(); } } } }