Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use interop library and write directly to Word document as well. Especially if document structure is quite simple. Disadvantages: Word must be installed on machine which will be generating report. Example below, remember to add assemblies: Microsoft.Office.Interop.Word (...\Office12\Microsoft.Office.Interop.Word.dll), Office (...\Office12\Office.dll).</p> <pre><code>using System; using System.Collections.Generic; using System.Text; using Microsoft.Office.Interop.Word; using Microsoft.Office.Core; using System.Runtime.InteropServices; using System.IO; using System.Diagnostics; namespace WriteToWordSO { class Program { static void Main(string[] args) { _Application app = null; Documents docs = null; Document doc = null; Range range = null; Tables tables = null; Table table = null; object oMissing = System.Reflection.Missing.Value; object oFalse = false; object oTrue = true; object fileName = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "tmp.doc"); object fileFormat = WdSaveFormat.wdFormatDocument; FileInfo fi = new FileInfo(fileName.ToString()); if (fi.Exists) fi.Delete(); int rows = 4, cols = 5; try { app = new ApplicationClass(); app.Visible = false; app.DisplayAlerts = WdAlertLevel.wdAlertsNone; app.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable; docs = app.Documents; doc = docs.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); range = doc.Range(ref oMissing, ref oMissing); tables = doc.Tables; object oAutoFitCell = WdDefaultTableBehavior.wdWord9TableBehavior; object oAutoFitWindow = WdAutoFitBehavior.wdAutoFitWindow; table = tables.Add(range, rows, cols, ref oAutoFitCell, ref oAutoFitWindow); Cell c = null; Range cellRange = null; // header row for (int i = 1; i &lt; cols + 1; i++) { c = table.Cell(1, i); cellRange = c.Range; cellRange.Text = "Header " + i.ToString(); cellRange.Bold = 1; Marshal.ReleaseComObject(c); Marshal.ReleaseComObject(cellRange); } // data rows for (int i = 1; i &lt; cols + 1; i++) { for (int j = 1; j &lt; rows; j++) { c = table.Cell(j + 1, i); cellRange = c.Range; cellRange.Text = "Cell " + i.ToString() + j.ToString(); Marshal.ReleaseComObject(c); Marshal.ReleaseComObject(cellRange); } } doc.SaveAs(ref fileName, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); ((_Document)doc).Close(ref oFalse, ref oMissing, ref oMissing); ((_Application)app).Quit(ref oFalse, ref oMissing, ref oMissing); Process.Start(fileName.ToString()); } finally { // frees memory GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); // its very important to release all used object, // otherwise some complications may appear if (table != null) Marshal.ReleaseComObject(table); table = null; if (tables != null) Marshal.ReleaseComObject(tables); tables = null; if (range == null) Marshal.ReleaseComObject(range); range = null; if (doc != null) Marshal.ReleaseComObject(doc); doc = null; if (docs != null) Marshal.ReleaseComObject(docs); docs = null; if (app != null) Marshal.ReleaseComObject(app); app = null; } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload