private static void InternalWrodReportExecute(Aspose.Words.Document doc, DataSet dataSource, PrintBySelf printByself, FormatColumnValue formatColumnValue) { foreach (Aspose.Words.Bookmark bookmark in doc.Range.Bookmarks) { } doc.MailMerge.MergeField += delegate (object sender, MergeFieldEventArgs e) { string text = string.Empty; bool flag = false; try { if (printByself != null) { flag = printByself(e.FieldName, ref text); } } catch (Exception exception) { LoggingService.Error(exception); } if (!flag && ((formatColumnValue != null) && (e.FieldValue != null))) { text = formatColumnValue(e.FieldValue, e.FieldValue.GetType(), e.FieldName); flag = true; } if (flag) { e.Text = text; } }; doc.MailMerge.MergeImageField += delegate (object sender, MergeImageFieldEventArgs e) { if (e.FieldValue != null) { MemoryStream stream = new MemoryStream((byte[]) e.FieldValue); e.ImageStream = stream; } }; List<string> list = new List<string>(doc.MailMerge.GetFieldNames()); foreach (DataTable table in dataSource.Tables) { if (table.Rows.Count > 0) { if (list.Contains(string.Format("TableStart:{0}", table.TableName))) { doc.MailMerge.ExecuteWithRegions(table); } else { doc.MailMerge.Execute(table); } } } }
public static void PrintWordEx(string targetFile, byte[] docBytes, DataSet dataSource, bool preview, PrintBySelf printByself, FormatColumnValue formatColumnValue, bool write) { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate (object sender, DoWorkEventArgs e) { WaitDialogHelper.Show(); try { using (MemoryStream stream = new MemoryStream(docBytes)) { if (license == null) { license = new Aspose.Words.License(); license.SetLicense("Aspose.lic"); } Aspose.Words.Document doc = new Aspose.Words.Document(stream); InternalWrodReportExecute(doc, dataSource, printByself, formatColumnValue); if (!write) { doc.Protect(ProtectionType.ReadOnly, "sbdwlfty"); } if (preview) { doc.Save(targetFile); Process.Start(targetFile); } else { new AsposeWordsPrintDocument(doc).Print(); } } } finally { WaitDialogHelper.Close(); } }; worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(PrintHelper.RunPrintWordWorkerCompleted); worker.RunWorkerAsync(); }
public static void PrintWordEx(byte[] docBytes, DataSet dataSource, bool preview, PrintBySelf printByself, FormatColumnValue formatColumnValue, bool write) { PrintWordEx(Path.GetTempFileName() + ".doc", docBytes, dataSource, preview, printByself, formatColumnValue, write); }
public static void PrintWord(string targetFile, byte[] docBytes, DataTable dataSource, bool preview, PrintBySelf printByself, FormatColumnValue formatColumnValue, bool write) { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate (object sender, DoWorkEventArgs e) { WaitDialogHelper.Show(); try { object fileName = targetFile; object readOnly = false; object visible = true; object confirmConversions = Missing.Value; if (LoggingService.IsInfoEnabled) { LoggingService.Info("准备写临时WORD文件"); } File.WriteAllBytes((string) fileName, docBytes); Microsoft.Office.Interop.Word.Application oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document doc = oWordApplic.Documents.Open(ref fileName, ref confirmConversions, ref readOnly, ref confirmConversions, ref confirmConversions, ref confirmConversions, ref confirmConversions, ref confirmConversions, ref confirmConversions, ref confirmConversions, ref confirmConversions, ref visible, ref confirmConversions, ref confirmConversions, ref confirmConversions, ref confirmConversions); Microsoft.Office.Interop.Word.Bookmarks bookmarks = doc.Bookmarks; foreach (Microsoft.Office.Interop.Word.Bookmark bookmark in bookmarks) { string text = string.Empty; if (LoggingService.IsInfoEnabled) { LoggingService.InfoFormatted("Word标签:{0}", new object[] { bookmark.Name }); } bool flag = false; try { if (printByself != null) { flag = printByself(bookmark.Name, ref text); } } catch (Exception exception) { LoggingService.Error(exception); } if (!flag && dataSource.Columns.Contains(bookmark.Name)) { if (formatColumnValue != null) { text = formatColumnValue(dataSource.Rows[0][bookmark.Name], dataSource.Columns[bookmark.Name].DataType, bookmark.Name); } else { text = dataSource.Rows[0][bookmark.Name].ToString(); } flag = true; } if (flag) { bookmark.Range.Text = text; } } doc.Fields.Update(); ShowOrPrintDocumnet(preview, write, ref confirmConversions, ref oWordApplic, ref doc); } finally { WaitDialogHelper.Close(); } }; worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(PrintHelper.RunPrintWordWorkerCompleted); worker.RunWorkerAsync(); }
public static void PrintWord(string targetFile, byte[] docBytes, DataSet dataSource, bool preview, PrintBySelf printByself, FormatColumnValue formatColumnValue, bool write) { PrintWord(targetFile, docBytes, dataSource.Tables[0], preview, printByself, formatColumnValue, write); }
public static void PrintWord(byte[] docBytes, DataSet dataset, bool preview, PrintBySelf printByself, FormatColumnValue formatColumnValue, bool write) { PrintWord(docBytes, dataset.Tables[0], preview, printByself, formatColumnValue, write); }