Operations on generating Word documents

Source: Internet
Author: User
Using system; using system. collections. generic; using system. web; using system. web. ui; using system. web. UI. webcontrols; using Microsoft. office. interOP. word; using system. io; public partial class word related _ generate WORD: system. web. UI. page {protected void page_load (Object sender, eventargs e) {createwordfile ("AAAAA");} Public String createwordfile (string checkedinfo) {string message = ""; try {object nothing = System. reflection. missing. value; directory. createdirectory ("F:/project code/Public Architecture/publicprojectsolution/publicprojectweb/word related"); // create the file directory string name = "CNSI _" + checkedinfo + ". doc "; object filename =" F: // project code // Public Architecture // publicprojectsolution // publicprojectweb // word related // "+ name; // file storage path // create a Word document Microsoft. office. interOP. word. application wordapp = new Microsoft. office. interOP. word. applicationclass (); m Icrosoft. office. interOP. word. document worddoc = wordapp. documents. add (ref nothing, ref nothing); // Add a header wordapp. activewindow. view. type = wdviewtype. wdoutlineview; wordapp. activewindow. view. seekview = wdseekview. wdseekprimaryheader; wordapp. activewindow. activepane. selection. insertafter ("[header content]"); wordapp. selection. paragraphformat. alignment = Microsoft. office. interOP. word. W Dparagraphalignment. wdalignparagraphright; // set the right-aligned wordapp. activewindow. view. seekview = wdseekview. wdseekmaindocument; // jump out of the header and set wordapp. selection. paragraphformat. linespacing = 15f; // set the line spacing of the document // move the focus and wrap the object COUNT = 14; object wdline = Microsoft. office. interOP. word. wdunits. wdline; // change the line; wordapp. selection. movedown (ref wdline, ref count, ref nothing); // move the focus wordapp. selection. typeparagraph (); // insert a paragraph // text File to create a table Microsoft. office. interOP. word. table newtable = worddoc. tables. add (wordapp. selection. range, 12, 3, ref nothing, ref nothing); // sets the table style newtable. borders. outsidelinestyle = Microsoft. office. interOP. word. wdlinestyle. wdlinestylethickthinlargegap; newtable. borders. insidelinestyle = Microsoft. office. interOP. word. wdlinestyle. wdlinestylesingle; newtable. columns [1]. width = 100f; newtable. columns [2]. W Idth = 220f; newtable. columns [3]. width = 105f; // fill in the table content newtable. cell (1, 1 ). range. TEXT = "product details table"; newtable. cell (1, 1 ). range. bold = 2; // set the font in the cell to bold. // merge the newtable cells. cell (1, 1 ). merge (newtable. cell (1, 3); wordapp. selection. cells. verticalalignment = Microsoft. office. interOP. word. wdcellverticalalignment. wdcellalignverticalcenter; // vertical center wordapp. selection. paragraphformat. alignment = Microsoft. office. I Nterop. word. wdparagraphalignment. wdalignparagraphcenter; // horizontally center // fill in the table content newtable. cell (2, 1 ). range. TEXT = "Basic Product Information"; newtable. cell (2, 1 ). range. font. color = Microsoft. office. interOP. word. wdcolor. wdcolordarkblue; // set the font color in the cell. // merge the newtable cells. cell (2, 1 ). merge (newtable. cell (2, 3); wordapp. selection. cells. verticalalignment = Microsoft. office. interOP. word. wdcellverticalalignment. wdcellalignverticalcenter; // Fill in the table content newtable. cell (3, 1 ). range. TEXT = "brand name:"; newtable. cell (3, 2 ). range. TEXT = "BrandName"; // vertically merge cells newtable. cell (3, 3 ). select (); // select a row of object moveunit = Microsoft. office. interOP. word. wdunits. wdline; object movecount = 5; object moveextend = Microsoft. office. interOP. word. wdmovementtype. wdextend; wordapp. selection. movedown (ref moveunit, ref movecount, ref moveextend); wordapp. selection. cel Ls. merge (); // insert image string filename = @ "C: \ bg-code.jpg"; // path of the image object linktofile = false; object savewithdocument = true; object anchor = worddoc. application. selection. range; worddoc. application. activedocument. inlineshapes. addpicture (filename, ref linktofile, ref savewithdocument, ref anchor); worddoc. application. activedocument. inlineshapes [1]. width = 100f; // The Image Width worddoc. application. activedocumen T. inlineshapes [1]. height = 100f; // Image Height // set the image to a peripheral Microsoft Image. office. interOP. word. shape S = worddoc. application. activedocument. inlineshapes [1]. converttoshape (); S. wrapformat. type = Microsoft. office. interOP. word. wdwraptype. wdwrapsquare; newtable. cell (12, 1 ). range. TEXT = "special product attributes"; newtable. cell (12, 1 ). merge (newtable. cell (12, 3); // Add the worddoc row to the table. content. tables [1]. rows. add (ref nothing); worddoc. pa Ragraphs. last. range. TEXT = "document creation time:" + datetime. now. tostring (); // "paid" worddoc. paragraphs. last. alignment = Microsoft. office. interOP. word. wdparagraphalignment. wdalignparagraphright; // Save the worddoc file. saveas (ref filename, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, re F Nothing); worddoc. close (ref nothing, ref nothing, ref nothing); wordapp. quit (ref nothing, ref nothing, ref nothing); message = Name + "the document is generated successfully and saved to C: CNSI." ;}catch {message = "An error occurred while exporting the file! ";}Return message ;}}
When executing the above Code, sometimes an error occurs: for example:Failed to retrieve components whose CLSID is {000209ff-0000-0000-c000-000000000046} in the com class factory because of the following error: 80070005If so, you still need to perform the following settings:
Solution 1:
Control Panel-> Administrative Tools-> component services-> Computer-> my computer-> DCOM configuration-> Microsoft Word documents
After
Click properties to open the Properties dialog box for this application.
2. Click the ID tab and select an interactive user.
3. Click the "Security" tab, select "Custom" in the "Start and activate Permissions" and "Access Permissions" groups, and then
Custom-> edit-> Add ASP. NET account and iuser _ computer name


* These accounts only exist when IIS is installed on the computer.
13. Make sure that each user is allowed to access the service and click OK.
14. Click OK to disable dcomcnfg.

Solution 2:
If the above method cannot solve the problem, it should be a permission problem. Please try the following method:
Use identity simulation in Web. config and add <identity impersonate = "true" username = "your username" Password = "password"/> In the <system. Web> section.
</System. Web>
Note:It is the path problem in it. The path for saving the Word file and the path of the image to be used in the Word file should be a local path, an error may occur if you place the relative or absolute path of the project!
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.