1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
| using System; using System.Diagnostics; using Microsoft.Office.Interop.Word;
namespace export_doc { public class Report { private _Application _wordApp = null; private _Document _wordDoc = null;
public _Application Application { get => _wordApp; set => _wordApp = value; }
public _Document Document { get => _wordDoc; set => _wordDoc = value; } public void CreateNewDocument(string path) { KillWinWordProcess(); _wordApp = new ApplicationClass() { DisplayAlerts = WdAlertLevel.wdAlertsNone, Visible = false };
object missing = System.Reflection.Missing.Value; object templateName = path; _wordDoc = _wordApp.Documents.Open(ref templateName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); } public void SaveDocument(string path) { object fileName = path; object format = WdSaveFormat.wdFormatDocument; object missing = System.Reflection.Missing.Value; _wordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); object saveChanges = WdSaveOptions.wdSaveChanges; object originalFormat = WdOriginalFormat.wdOriginalDocumentFormat; object routeDocument = false; _wordDoc.Close(ref saveChanges,ref originalFormat,ref routeDocument); _wordApp.Quit(ref saveChanges,ref originalFormat,ref routeDocument); } public bool InsertValue(string bookMark, string value) { object bkObj = bookMark; if (_wordApp.ActiveDocument.Bookmarks.Exists(bookMark)) { _wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select(); _wordApp.Selection.TypeText(value); return true; } return false; } public Table InsertTable(string bookMark, int rows, int columns, float width) { object missing = System.Reflection.Missing.Value; object toStart = bookMark; Range range = _wordDoc.Bookmarks.get_Item(ref toStart).Range; Table newTable = _wordDoc.Tables.Add(range, rows, columns, ref missing, ref missing); newTable.Borders.Enable = 1; newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt; if (width != 0) newTable.PreferredWidth = width; newTable.AllowPageBreaks = false; return newTable; } public void MergeCell(Microsoft.Office.Interop.Word.Table table, int row1, int column1, int row2, int column2) { table.Cell(row1,column1).Merge(table.Cell(row2,column2)); } public void SetParagraph_Table(Table table, int align, int vertical) { switch (align) { case -1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; break; case 0: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; break; case 1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; break; }
switch (vertical) { case -1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalTop; break; case 0: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; break; case 1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom; break; } } public void SetFont_Table(Table table, string fontName, double size) { if (size!=0) { table.Range.Font.Size = Convert.ToSingle(size); }
if (!string.IsNullOrEmpty(fontName)) { table.Range.Font.Name = fontName; } } public void UseBorder(int n, bool use) { _wordDoc.Content.Tables[n].Borders.Enable = use ? 1 : 2; } public void AddRow(int n) { object missing = System.Reflection.Missing.Value; _wordDoc.Content.Tables[n].Rows.Add(ref missing); } public void AddRow(int n, int rows) { object missing = System.Reflection.Missing.Value; Table table = _wordDoc.Content.Tables[n]; for (int i = 0; i < rows; i++) { table.Rows.Add(ref missing); } } public void InsertCell(Table table, int row, int column, string value) { table.Cell(row, column).Range.Text = value; } public void InsertCell(int n, int row, int column, string value) { _wordDoc.Tables[n].Cell(row, column).Range.Text = value; } public void InsertCell(int n, int row, int columns, string[] values) { Table table = _wordDoc.Content.Tables[n]; for (int i = 0; i < columns; i++) { table.Cell(row, columns).Range.Text = values[i]; } } public void InsertPicture(string bookMark, string picturePath, float width, float height) { object missing = System.Reflection.Missing.Value; object toStart = bookMark; object linkToFile = false; object saveWithDocument = true; object range = _wordDoc.Bookmarks.get_Item(ref toStart).Range; _wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocument, ref range); _wordDoc.Application.ActiveDocument.InlineShapes[1].Width = width; _wordDoc.Application.ActiveDocument.InlineShapes[1].Height = height; } public void InsertText(string bookMark, string text) { object toStart = bookMark; object range = _wordDoc.Bookmarks.get_Item(ref toStart).Range; Paragraph wp = _wordDoc.Content.Paragraphs.Add(ref range); wp.Format.SpaceBefore = 6; wp.Range.Text = text; wp.Format.SpaceAfter = 24; wp.Range.InsertParagraphAfter(); _wordDoc.Paragraphs.Last.Range.Text = "\n"; } public void KillWinWordProcess() { System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD"); foreach (Process process in processes) { var b = process.MainWindowTitle == ""; if (b) { process.Kill(); } } } } }
namespace export_doc { internal class Program { public static void Main(string[] args) { Report report = new Report(); report.CreateNewDocument(@"xxxxx.docx"); report.InsertValue("warning_level", "Ⅱ"); report.SaveDocument(@"xxxx.docx"); } } }
|