Aspose. Words: the problem of exporting dt to word, aspose. wordsdt

Source: Internet
Author: User

Aspose. Words: the problem of exporting dt to word, aspose. wordsdt

Solution: import the extracted data to a word document. The exported text and image are successfully exported. Run Main () to export dt to word.

To be resolved: Import multiple pieces of data (text and image into one word document) From dt to word; error messages discovered"An item with the same key has been added"

Dll library used: Aspose. Words and template file 2.docx

 

The complete code is as follows. Execute Main () to export.

Private void Main () {var dic = new Dictionary <string, string> (); Document doc = new Document (Server. mapPath (@ "2.doc"); // template file path string StrID = Request. form ["YX_CH"]; // obtain the value of the foreground GridView project. The following dt is used as one of the query conditions, in addition, nodeid is the node number if (StrID = null) {WebMsgBox. show ("You have not selected any data! ");} DataTable dt = bArticle. getList ("nodeid = '" + Request. queryString ["nodeid"] + "'and ArticleID in (" + StrID + ")"). tables [0]; // if (dt. rows. count> 0) // {// for (int I = 0; I <dt. rows. count; I ++ )//{
// The above four lines comment on the for Loop (start)
Dic. add ("title", dt. rows [0] ["art_title"]. toString (); // The first line starting with "An item with the same key already added", indicated by the browser in dic. add ("date", dt. rows [0] ["art_pubdate"]. toString (); dic. add ("image", Server. mapPath (@ "/UploadFiles/Images/") + dt. rows [0] ["art_img"]. toString (); // replace doc = new Document (Server. mapPath (@ "2.doc"); foreach (var key in dic. keys) {if (key! = "Image") {var repStr = string. format ("& {0} &", key); doc. range. replace (repStr, dic [key], false, false);} else {Regex reg = new Regex ("& Image &"); doc. range. replace (reg, new ReplaceAndInsertImage (Server. mapPath (@ "/UploadFiles/Images/") + dt. rows [I] ["art_img"]. toString (), false );}}//}//}
// The above four lines comment on the for Loop (end)
Doc. Save (Server. MapPath (@ "2ks.doc"); // Save the exported file Response. End ();}
// Public class ReplaceAndInsertImage: IReplacingCallback {// <summary> // path of the image to be inserted /// </summary> public string url {get; set;} public ReplaceAndInsertImage (string url) {this. url = url;} public ReplaceAction Replacing (ReplacingArgs e) {// get the current node var node = e. matchNode; // get the current Document doc = node. document as Document; DocumentBuilder builder = new DocumentBuilder (doc); // move the cursor to the builder of the specified node. moveTo (node); // insert image builder. insertImage (url); return ReplaceAction. replace ;}}

The simplified Main method, but only outputs a piece of data.

Private void Main () {Document doc = new Document (Server. mapPath (@ "2.doc"); DataTable dt = bArticle. getList ("nodeid = '4 '"). tables [0]; if (dt. rows. count> 0) {for (int I = 0; I <dt. rows. count; I ++) {doc. range. replace ("& Title &", dt. rows [2] ["art_title"]. toString (), false, false); doc. range. replace ("& Date &", dt. rows [I] ["art_pubdate"]. toString (), false, false );
Regex reg = new Regex ("& Image &"); doc. range. replace (reg, new ReplaceAndInsertImage (Server. mapPath (@ "/UploadFiles/Images/") + dt. rows [I] ["art_img"]. toString (), false);} doc. save (Server. mapPath (@ "2ks.doc"); Response. end ();}}

  


How does asposewords call a Word document and display it on the page?

Reference answer: read books and write them down.

Image Display problems after converting Word documents into webpage formats using the asposewords Control

Baidu
 

Related Article

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.