主頁 > 知識庫 > asp.net 按指定模板導出word,pdf實例代碼

asp.net 按指定模板導出word,pdf實例代碼

熱門標簽:世紀佳緣地圖標注怎么去掉 巫師3為什么地圖標注的財寶沒有 怎么給超市做地圖標注入駐店 辦理400電話證件 外呼系統費用一年 外呼系統代理品牌 十堰正規(guī)電銷機器人系統 寧波自動外呼系統代理 手機地圖標注如何刪除

復制代碼 代碼如下:

/// summary>
        /// 導出word文件
        /// /summary>
        /// param name="templateFile">模板路徑/param>
        /// param name="fileNameWord">導出文件名稱/param>
        /// param name="fileNamePdf">pdf文件名稱/param>
        /// param name="bookmarks">模板內書簽集合/param>
        /// param name="invoiceline">發(fā)票條目列表/param>
        public static void GenerateWord(string templateFile, string fileNameWord, string fileNamePdf, Dictionarystring, string> bookmarks, ListInvoiceLineView> invoiceline)
        {
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
            File.Copy(templateFile, fileNameWord, true);
            Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
            object Obj_FileName = fileNameWord;
            object Visible = false;
            object ReadOnly = false;
            object missing = System.Reflection.Missing.Value;
            doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref Visible, ref missing, ref missing, ref missing, ref missing);
            doc.Activate();
            foreach (string bookmarkName in bookmarks.Keys)
            {

                object BookMarkName = bookmarkName;//獲得書簽名                   
                Range range = doc.Bookmarks.get_Item(ref BookMarkName).Range;//表格插入位置
                range.Text = bookmarks[bookmarkName];
            }        
          object IsSave = true;
            object FileName = fileNamePdf;
            object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
            object LockComments = false;
            object AddToRecentFiles = true;
            object ReadOnlyRecommended = false;
            object EmbedTrueTypeFonts = false;
            object SaveNativePictureFormat = true;
            object SaveFormsData = false;
            object SaveAsAOCELetter = false;
            object Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingSimplifiedChineseGB18030;
            object InsertLineBreaks = false;
            object AllowSubstitutions = false;
            object LineEnding = Microsoft.Office.Interop.Word.WdLineEndingType.wdCRLF;
            object AddBiDiMarks = false;
            doc.SaveAs(ref FileName, ref FileFormat, ref LockComments,
                    ref missing, ref AddToRecentFiles, ref missing,
                    ref ReadOnlyRecommended, ref EmbedTrueTypeFonts,
                    ref SaveNativePictureFormat, ref SaveFormsData,
                    ref SaveAsAOCELetter, ref Encoding, ref InsertLineBreaks,
                    ref AllowSubstitutions, ref LineEnding, ref AddBiDiMarks);
            doc.Close(ref IsSave, ref missing, ref missing);
        }

調用

復制代碼 代碼如下:

Dictionarystring, string> bookmarks = new Dictionarystring, string>();
bookmarks.Add("ContractDueDateTime", invoice.InvoiceTime.AddDays(invoice.ContractDueDate).ToString("D"));
bookmarks.Add("CustomContactEmail", invoice.CustomContactEmail);
bookmarks.Add("CustomContactName", invoice.CustomContactName);
bookmarks.Add("ContractDueDate", invoice.ContractDueDate.ToString());
bookmarks.Add("CustomContactTel", invoice.CustomContactTel);
bookmarks.Add("CustomAddress", invoice.CustomAddress);
bookmarks.Add("InvoiceTime", invoice.InvoiceTime.ToString());
bookmarks.Add("InvoiceID", invoice.InvoiceID);
bookmarks.Add("CustomName", invoice.CustomName);
bookmarks.Add("CustomName2", invoice.CustomName);
bookmarks.Add("total", invoice.TotalPrice.ToString("C"));
bookmarks.Add("total1", invoice.TotalPrice.ToString("C"));
bookmarks.Add("totalTax", invoice.TotalTax.ToString("C"));
bookmarks.Add("totalPrice", (invoice.TotalPrice + invoice.TotalTax).ToString("C"));
bookmarks.Add("totalPrice1", (invoice.TotalPrice + invoice.TotalTax).ToString("C"));
bookmarks.Add("totalPrice2", (invoice.TotalPrice + invoice.TotalTax).ToString("C"));
bookmarks.Add("totalPrice3", (invoice.TotalPrice + invoice.TotalTax).ToString("C"));
bookmarks.Add("totalPrice4", (invoice.TotalPrice + invoice.TotalTax).ToString("C"));
Utility.GenerateWord(templateFile, fileNameWord, fileNamePdf, bookmarks, invoiceline);


新建一個word,在需要替換的位置插入書簽,使用以上方法即可將書簽處替換為指定內容,并且另存為pdf

您可能感興趣的文章:
  • 如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF詳解
  • ASP.NET Core中快速構建PDF文檔的步驟分享
  • ASP.NET Core 5中如何生成PDF文檔

標簽:牡丹江 天門 通遼 嘉興 景德鎮(zhèn) 平涼 山西 泰州

巨人網絡通訊聲明:本文標題《asp.net 按指定模板導出word,pdf實例代碼》,本文關鍵詞  asp.net,按,指定,模板,導出,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net 按指定模板導出word,pdf實例代碼》相關的同類信息!
  • 本頁收集關于asp.net 按指定模板導出word,pdf實例代碼的相關信息資訊供網民參考!
  • 推薦文章