. Net exports data to word

Source: Internet
Author: User

1. First import the COM file Microsoft Word 11.0 Object Library.
2. Declare using system. Text. regularexpressions;
3. Add the InterOP. Word. dll file.
Http://files.cnblogs.com/ghostljj/Interop.Word.rar
-----------------------------------------------------------------
4. perform the following steps:
Object nothing = system. reflection. Missing. value;
// Obtain the path for saving the Word file
Object filename = system. Web. httpruntime. appdomainapppath + "file: // xmlfiles // educewordfiles //" + this. Context. User. Identity. Name + ". Doc ";
// Create a component object named wordapp
Word. Application wordapp = new word. applicationclass ();
// Create a document object named worddoc
Word. Document worddoc = wordapp. Documents. Add (ref nothing, ref nothing );
// Add a table
// Word. Table = worddoc. Tables. Add (wordapp. selection. Range, 1, 1, ref nothing, ref nothing );
// Add custom text content to the first cell of the table
// Table. Cell (1, 1). range. Text = "add custom text content to the first cell of the table ";
// Add text content in the blank area of the document
// Worddoc. Paragraphs. Last. range. Bold = 72;
// Wordapp. Visible = true;
// Worddoc. Activate ();

# Region title
Wordapp. selection. Font. size = 15;
Wordapp. selection. paragraphformat. Alignment = word. wdparagraphalignment. wdalignparagraphcenter; // center
Wordapp. selection. Font. Bold = 1; //
Wordapp. selection. typetext (saveshowinfo. Title );
# Endregion

# Region time and Source
Wordapp. selection. typeparagraph ();
Wordapp. selection. Font. size = 10;
Wordapp. selection. Font. Bold = 0; // cancel the blacklist
Wordapp. selection. typetext ("Release Date:" + saveshowinfo. savetime + "Source:" + saveshowinfo. websitename );
# Endregion

# Region Summary
Wordapp. selection. typeparagraph ();
Wordapp. selection. typeparagraph ();
Wordapp. selection. paragraphformat. Alignment = word. wdparagraphalignment. wdalignparagraphleft; // left
Wordapp. selection. typetext ("Abstract :");
Wordapp. selection. typeparagraph ();
// Wordapp. selection. paragraphformat. characterunitfirstlineindent = 2.0f; // indent the first line with 2 Characters
Wordapp. selection. typetext ("" + saveshowinfo. Summary );
# Endregion

# Region content

Wordapp. selection. typeparagraph ();
Wordapp. selection. typeparagraph ();
Wordapp. selection. typetext ("content :");

String strpagecontent = saveshowinfo. content. tostring ();
// Set
Into two

// Strpagecontent = RegEx. Replace (strpagecontent ,"(
[\ S] *) + ","

");
// Remove all labels, with only \ r \ n left
Strpagecontent = RegEx. Replace (strpagecontent, @ "<[^>] +/?> |] +> "," ", Regexoptions. ignorecase );

String [] strcontents = strpagecontent. Split (New String [] {"\ r \ n"}, stringsplitoptions. removeemptyentries );

Foreach (string strcontent in strcontents)
{
Wordapp. selection. typeparagraph ();
Wordapp. selection. typetext ("" + strcontent );
}

# Endregion

# Region image Export

String [] strpictureurls = saveshowinfo. pictureurl. Split (New String [] {"
"}, Stringsplitoptions. removeemptyentries );
If (strpictureurls. length! = 0 & strpictureurls [0]! = "")
{
Wordapp. selection. typeparagraph ();
Wordapp. selection. typeparagraph ();
Wordapp. selection. typetext ("Image :");
Wordapp. selection. typeparagraph ();
Wordapp. selection. paragraphformat. Alignment = word. wdparagraphalignment. wdalignparagraphcenter; // center
Foreach (string strpictureurl in strpictureurls)
{
If (strpictureurl. length> 10)
{
String strurl = getpictureonlyurl (strpictureurl );

Wordapp. selection. inlineshapes. addpicture (strurl, ref nothing );
Wordapp. selection. typeparagraph ();

}
}
}
# Endregion

// Worddoc. Paragraphs. Last. range. Text + = saveshowinfo. pictureurl;

// Save the content of the worddoc object as a doc object
Worddoc. saveas (ref filename, ref nothing, ref nothing, ref nothing, ref nothing );
// Close the worddoc Document Object
Worddoc. Close (ref nothing, ref nothing, ref nothing );
// Close the wordapp Component Object
Wordapp. Quit (ref nothing, ref nothing, ref nothing );
// Return results
// Lblmsg. Text = "file path: C: \ 111.doc( click the link to view details)
Result generated: Successful! ";

// Clear special characters from the exported file
String outfilename = Si. Title. Replace ("/","");
Outfilename = outfilename. Replace ("\\","");
Outfilename = outfilename. Replace (":","");
Outfilename = outfilename. Replace ("*","");
Outfilename = outfilename. Replace ("? ","");
Outfilename = outfilename. Replace ("\"","");
Outfilename = outfilename. Replace ("<","");
Outfilename = outfilename. Replace ("> ","");
Outfilename = outfilename. Replace ("| ","");
// Download the file from the server (refer to my other article)
// Reference URL http://www.cnblogs.com/ghostljj/archive/2007/01/24/629293.html
Responsefile (page. Request, page. Response, outfilename + ". Doc"
, System. Web. httpruntime. appdomainapppath + "file: // xmlfiles // educewordfiles //" + this. Context. User. Identity. Name + ". Doc", 1024000 );
//--------------------------------------------------------
5. If it is placed in IIS, it cannot be output now. configure it.
Solution 1: Add in Web. config



Solution 2:
(1) In the Run-> dcomcnfg open the component service
(2) choose console root directory> component service> Computer> my computer> DCOM configuration> Microsoft Word document> Properties> Security
(3) Start and activate permissions-> use custom-> Add an ASPnet user, and enable local startup and Local activation.
Access permission-> use custom-> Add an ASPnet user, and enable local access and remote access

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.