Aspose.Words匯出dt到word的問題

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   os   ar   使用   for   

已解決:單挑資料匯入到一個word文檔,匯出文字和圖片成功,執行Main();方法匯出dt到word

待解決:多條資料(文字,圖片的匯入到一個word文檔裡面)從dt匯入到word;已發現的錯誤提示“已添加了具有相同鍵的項”

所用dll庫:Aspose.Words 和模板檔案2.docx

希望楊老師予以指導!

以下為完整代碼。執行Main()即可匯出。

        private void Main()        {            var dic = new Dictionary<string, string>();            Document doc = new Document(Server.MapPath(@"2.doc"));//模板檔案路徑            string StrID = Request.Form["YX_CH"];//這裡是擷取前台GridView項目的值,穿給下面的dt作為查詢條件之一,另外的nodeid是節點編號            if (StrID == null)            {                WebMsgBox.Show("您還未選擇任何資料!");            }            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++)            //    {
//以上4行注釋的為for迴圈(開始)
dic.Add("標題", dt.Rows[0]["art_title"].ToString()); //“已添加了具有相同鍵的項”開始的第一行,瀏覽器所指位置 dic.Add("日期", dt.Rows[0]["art_pubdate"].ToString()); dic.Add("圖片", Server.MapPath(@"/UploadFiles/Images/") + dt.Rows[0]["art_img"].ToString()); //使用特殊字元串替換 doc = new Document(Server.MapPath(@"2.doc")); foreach (var key in dic.Keys) { if (key != "圖片") { var repStr = string.Format("&{0}&", key); doc.Range.Replace(repStr, dic[key], false, false); } else { Regex reg = new Regex("&圖片&"); doc.Range.Replace(reg, new ReplaceAndInsertImage(Server.MapPath(@"/UploadFiles/Images/") + dt.Rows[i]["art_img"].ToString()), false); } } // } //}
             //以上4行注釋的為for迴圈(結束)
        doc.Save(Server.MapPath(@"2ks.doc"));//儲存匯出的檔案 Response.End(); }
//插入圖片的一個方法
public class ReplaceAndInsertImage : IReplacingCallback { /// <summary> /// 需要插入的圖片路徑 /// </summary> public string url { get; set; } public ReplaceAndInsertImage(string url) { this.url = url; } public ReplaceAction Replacing(ReplacingArgs e) { //擷取當前節點 var node = e.MatchNode; //擷取當前文檔 Document doc = node.Document as Document; DocumentBuilder builder = new DocumentBuilder(doc); //將游標移動到指定節點 builder.MoveTo(node); //插入圖片 builder.InsertImage(url); return ReplaceAction.Replace; } }

 

Aspose.Words匯出dt到word的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.