About c # Generating word .,
Requirement: You need to make the data into such an effect and display a list of r data on a page.
Object filename = ""; Object Nothing = System. reflection. missing. value; Word. application WordApp = new Word. applicationClass (); // instantiate word HY. common. utils. logger. log. info ("WordApp:" + WordApp); Word. _ Application oWord = new Word. application (); // create a Word document Word. _ Document WordDoc = WordApp. documents. add (ref Nothing, ref Nothing); try {SetPage (WordApp, WordDoc, "dashboard", 21, 29.7, 1, 1, 1, 1); HY. common. utils. logger. log. info ("WordDoc:" + WordDoc); string name = "Approval table" + DateTime. now. toString ("yyyyMMdd") + DateTime. now. toLongTimeString (). replace (":", "") + ". docx "; // create the name of the generated file HY. common. utils. logger. log. info ("name" + name); string CreateFilePath = ConfigurationManager. appSettings ["CreateFilePath"]; HY. common. utils. logger. log. info ("CreateFilePath" + CreateFilePath); filename = C ReateFilePath + name; // file storage path HY. common. utils. logger. log. info ("filename" + filename); string [] SerialNumberArr = Context. request ["SerialNumber"]. substring (0, Context. request ["SerialNumber"]. toString (). length-1 ). split (','); string [] ProcInstIDArr = Context. request ["ProcInstID"]. substring (0, Context. request ["ProcInstID"]. toString (). length-1 ). split (','); // account Id string [] PaymentProcInstIDArr = Con Text. request ["PaymentProcInstID"]. substring (0, Context. request ["PaymentProcInstID"]. toString (). length-1 ). split (','); // payment Id for (int I = 0; I <ProcInstIDArr. length; I ++) {# region print InsertText (WordDoc, "Approval table", new System. drawing. font ("", 12, System. drawing. fontStyle. bold), Word. wdParagraphAlignment. wdAlignParagraphLeft, false); InsertText (WordDoc, "", new System. drawing. font ("", 12, Sy Stem. drawing. fontStyle. bold), Word. wdParagraphAlignment. wdAlignParagraphLeft, true); InsertText (WordDoc, "Applicant information", new System. drawing. font ("", 12, System. drawing. fontStyle. bold), Word. wdParagraphAlignment. wdAlignParagraphLeft, false); if (ProcInstIDArr. length-1! = I) {InsertBreak (WordDoc, Nothing) ;}# endregion} WordDoc. saveAs (ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing); WordApp = null; HY. common. utils. logger. log. info ("SaveAs" + filename);} catch (Exception ex) {HY. common. utils. logger. log. info ("error" + e. toString ();} finally {WordDoc. close (ref Nothing, ref Nothing, ref Nothing); oWord. quit (ref Nothing, ref Nothing, ref Nothing); KillWordProcess ();}# Region-insert text-public static bool InsertText (Word. _ Document oDoc, string strText, System. drawing. font font, Word. wdParagraphAlignment wdAlign, bool isAftre) {try {Word. range rng = oDoc. content; int lenght = oDoc. characters. count-1; object start = lenght; object end = lenght; rng = oDoc. range (ref start, ref end); if (isAftre = true) {strText + = "\ r \ n";} rng. text = strText; rng. font. name = font. name; rng. font. size = font. size; if (font. style = System. drawing. fontStyle. bold) {rng. font. bold = 1;} // set the font of the cell to Bold rng. paragraphFormat. alignment = wdAlign; // set the text Alignment format (left Alignment, right Alignment, center Alignment) return true;} catch (Exception) {return false ;}# endregionView Code # region-insert pagination character-public static void InsertBreak (Word. _ Document WordDoc, Object Nothing) {Word. paragraph para; para = WordDoc. content. paragraphs. add (ref Nothing); object pBreak = (int) Word. wdBreakType. wdSectionBreakNextPage; para. range. insertBreak (ref pBreak);} public static void DeleteBreak (Word. application wordApp, Object Nothing) {Word. range range1, range2; // jump type object objWhat = Word. wdGoToItem. wdGoToPage; // jump position object objWhich = Word. wdGoToDirection. wdGoToLast; // switch to the wordApp on the last page. selection. goTo (ref objWhat, ref objWhich, ref Nothing, ref Nothing); // Range to get range1 = wordApp. selection. range; range2 = wordApp. activeDocument. range (ref Nothing, ref Nothing); object start = range1.Start; object end = range2.End; // Delete the last wordApp. activeDocument. range (ref start, ref end ). delete (ref Nothing, ref Nothing);} # endregionView Code # region-page settings-public static void SetPage (Word. application oWord, Word. _ Document oDoc, string orientation, double width, double height, double topMargin, double leftMargin, double rightMargin, double bottomMargin) {Logger. log. info ("Word. name "+ oWord. name); Logger. log. info ("oDoc" + oDoc); oDoc. pageSetup. pageWidth = oWord. centimetersToPoints (float) width); oDoc. pageSetup. pageHeight = oWord. centimetersToPoints (float) height); if (orientation = "horizontal bar") {oDoc. pageSetup. orientation = Microsoft. office. interop. word. wdOrientation. wdOrientLandscape;} oDoc. pageSetup. topMargin = (float) (topMargin * 25); // top margin oDoc. pageSetup. leftMargin = (float) (leftMargin * 25); // left away from oDoc. pageSetup. rightMargin = (float) (rightMargin * 25); // right margin from oDoc. pageSetup. bottomMargin = (float) (bottomMargin * 25); // bottom margin} # endregionView Code # region-Clear the word process-/** // <summary> // clear the word process // </summary> public static void KillWordProcess () {System. diagnostics. process [] myPs; myPs = System. diagnostics. process. getProcesses (); foreach (System. diagnostics. process p in myPs) {if (p. id! = 0) {string myS = "WINWORD. EXE" + p. ProcessName + "ID:" + p. Id. ToString (); try {if (p. Modules! = Null) if (p. modules. count> 0) {System. diagnostics. processModule pm = p. modules [0]; myS + = "/n Modules [0]. fileName: "+ pm. fileName; myS + = "/n Modules [0]. moduleName: "+ pm. moduleName; myS + = "/n Modules [0]. fileVersionInfo:/n "+ pm. fileVersionInfo. toString (); if (pm. moduleName. toLower () = "winword.exe") p. kill () ;}} catch {}finally {}}# endregionView Code
Problems:
1. Because you need to display a list on each page, after each cycle is created based on ProcInstIDArr [], call InsertBreak () to generate a blank page. As a result, a blank page appears at the end,
At that time, I thought about how to delete the last blank page and query it online, saying that the following code can be used to delete the page, and the code can be used to test the result.
// Word. range range1, range2; // jump type // object objWhat = Word. wdGoToItem. wdGoToPage; // jump position // object objWhich = Word. wdGoToDirection. wdGoToLast; // switch to the last page // WordApp. selection. goTo (ref objWhat, ref objWhich, ref Nothing, ref Nothing); // get Range // range1 = WordApp. selection. range; // range2 = WordApp. activeDocument. range (ref Nothing, ref Nothing); // object start = range1.Start; // object end = range2.End; // Delete the last page // WordApp. activeDocument. range (ref start, ref end ). delete (ref Nothing, ref Nothing );View Code
Ask your colleagues at noon about the logic. I found that it was totally complicated, and I only thought about how to delete it. I never thought about generating it. Finally, the problem can be easily solved through an if judgment.
Do not think too complicated when encountering problems.
2.there is also a problem that the winword.exe process has not been killed. This was discovered this morning. In the morning, I opened the server and found that there were N word documents on the server. There are no more than 30 winword.exe processes in the process, click Details