Public bool CreateWordFile (string _ filename, "data List or your C # data to be written ") { # Region starts generating Word Try { String strtitle = "task export "; Object oEndOfDoc = "// endofdoc "; Object Nothing = System. Reflection. Missing. Value; Object filename = _ filename; // Create a Word document Microsoft. Office. Interop. Word. Application WordApp = new Microsoft. Office. Interop. Word. ApplicationClass (); Microsoft. Office. Interop. Word. Document WordDoc = WordApp. Documents. Add (ref Nothing, ref Nothing ); // Set the header WordApp. ActiveWindow. View. Type = Microsoft. Office. Interop. Word. WdViewType. wdOutlineView; WordApp. ActiveWindow. View. SeekView = Microsoft. Office. Interop. Word. WdSeekView. wdSeekPrimaryHeader; WordApp. ActiveWindow. ActivePane. Selection. InsertAfter ("WPFOA task export "); WordApp. Selection. ParagraphFormat. Alignment = Microsoft. Office. Interop. Word. WdParagraphAlignment. wdAlignParagraphRight; // set the right Alignment WordApp. ActiveWindow. View. SeekView = Microsoft. Office. Interop. Word. WdSeekView. wdSeekMainDocument; // jump out of the header settings // Task export ------ name Word. Paragraph oPara1; OPara1 = WordDoc. Content. Paragraphs. Add (ref Nothing ); OPara1.Range. Text = strtitle; OPara1.Range. Font. Bold = 1; OPara1.Range. Font. Name = ""; OPara1.Range. Font. Size = 20; OPara1.Range. ParagraphFormat. Alignment = Word. WdParagraphAlignment. wdAlignParagraphCenter; OPara1.Format. SpaceAfter = 5; // 24 pt spacing after paragraph. OPara1.Range. InsertParagraphAfter (); # Region loop each table Foreach (var v in lst_task) { # Region loops through each column to generate a row of data // Description Word. Paragraph oPara3; Object oRng = WordDoc. Bookmarks. get_Item (ref oEndOfDoc). Range; OPara3 = WordDoc. Content. Paragraphs. Add (ref oRng ); OPara3.Range. Text = ""; OPara3.Range. Font. Bold = 0; OPara3.Range. Font. Name = ""; OPara3.Range. Font. Size = 9; OPara3.Range. ParagraphFormat. Alignment = Word. WdParagraphAlignment. wdAlignParagraphCenter; OPara3.Format. SpaceBefore = 1; OPara3.Format. SpaceAfter = 1; OPara3.Range. InsertParagraphAfter (); // Insert table Word. Table newTable; Word. Range wrdRng = WordDoc. Bookmarks. get_Item (ref oEndOfDoc). Range; NewTable = WordDoc. Tables. Add (wrdRng, 5, 4, ref Nothing, ref Nothing ); NewTable. Columns [1]. Width = 60; NewTable. Columns [2]. Width = 145; NewTable. Columns [3]. Width = 80; NewTable. Columns [4]. Width = 145; NewTable. Borders. OutsideLineStyle = Microsoft. Office. Interop. Word. WdLineStyle. wdLineStyleSingle; NewTable. Borders. InsideLineStyle = Microsoft. Office. Interop. Word. WdLineStyle. wdLineStyleSingle; // Fill in the table content NewTable. Cell (1, 1). Range. Text = "project name "; NewTable. Cell (1, 2). Range. Text = "1 "; NewTable. Cell (1, 3). Range. Text = "module name "; NewTable. Cell (1, 4). Range. Text = "2 "; NewTable. Cell (2, 1). Range. Text = "owner name "; NewTable. Cell (2, 2). Range. Text = "3 "; NewTable. Cell (2, 3). Range. Text = "status "; NewTable. Cell (2, 4). Range. Text = "4 "; NewTable. Cell (3, 1). Range. Text = "published on "; NewTable. Cell (3, 2). Range. Text = "5 "; NewTable. Cell (3, 3). Range. Text = "required completion time "; NewTable. Cell (3, 4). Range. Text = "6 "; NewTable. Cell (4, 1). Range. Text = "completion time "; NewTable. Cell (4, 2). Range. Text = "7 "; NewTable. Cell (4, 3). Range. Text = ""; NewTable. Cell (4, 4). Range. Text = "8 "; NewTable. Cell (5, 1). Range. Text = "task description "; NewTable. Cell (5, 2). Merge (newTable. Cell (5, 4 )); NewTable. Cell (5, 2). Range. ParagraphFormat. Alignment = Word. WdParagraphAlignment. wdAlignParagraphLeft; NewTable. Cell (5, 2). Range. Text = "12313213123213 "; # Endregion } WordDoc. SaveAs (ref filename, ref Nothing, Ref Nothing, Ref Nothing, Ref Nothing, ref Nothing ); WordDoc. Close (ref Nothing, ref Nothing, ref Nothing ); WordApp. Quit (ref Nothing, ref Nothing, ref Nothing ); # Endregion // Close the WinWord Process System. Diagnostics. Process [] MyProcess = System. Diagnostics. Process. GetProcessesByName ("WINWORD "); MyProcess [0]. Kill (); Return true; } Catch { System. Diagnostics. Process [] MyProcess = System. Diagnostics. Process. GetProcessesByName ("WINWORD "); MyProcess [0]. Kill (); Return false; } # Endregion } |