asp.net 按指定模板導(dǎo)出word,pdf實(shí)例代碼
/// <summary>
/// 導(dǎo)出word文件
/// </summary>
/// <param name="templateFile">模板路徑</param>
/// <param name="fileNameWord">導(dǎo)出文件名稱(chēng)</param>
/// <param name="fileNamePdf">pdf文件名稱(chēng)</param>
/// <param name="bookmarks">模板內(nèi)書(shū)簽集合</param>
/// <param name="invoiceline">發(fā)票條目列表</param>
public static void GenerateWord(string templateFile, string fileNameWord, string fileNamePdf, Dictionary<string, string> bookmarks, List<InvoiceLineView> 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;//獲得書(shū)簽名
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);
}
調(diào)用
Dictionary<string, string> bookmarks = new Dictionary<string, 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);
新建一個(gè)word,在需要替換的位置插入書(shū)簽,使用以上方法即可將書(shū)簽處替換為指定內(nèi)容,并且另存為pdf
相關(guān)文章
ASP.NET的HtmlForm控件學(xué)習(xí)及Post與Get的區(qū)別概述
HtmlForm 控件用于控制form元素,本文主要介紹下HtmlForm控件的Method/Action方法(要提交數(shù)據(jù)的頁(yè)面,即數(shù)據(jù)要傳送至哪個(gè)網(wǎng)址)及Post與Get的區(qū)別感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)HtmlForm控件有所幫助2013-02-02
asp.net html控件的File控件實(shí)現(xiàn)多文件上傳實(shí)例分享
asp.net中html控件的File控件實(shí)現(xiàn)多文件上傳簡(jiǎn)單實(shí)例,開(kāi)發(fā)工具vs2010使用c#語(yǔ)言,感興趣的朋友可以了解下,必定是多文件上傳值得學(xué)習(xí),或許本文所提供的知識(shí)點(diǎn)對(duì)你有所幫助2013-02-02
使用pdfbox實(shí)現(xiàn)pdf文本提取和合并功能示例
這篇文章主要介紹了使用pdfbox實(shí)現(xiàn)pdf文本提取和合并功能示例,大家參考使用吧2014-01-01
.net6?在中標(biāo)麒麟下的安裝和部署過(guò)程
這篇文章主要介紹了.net6?在中標(biāo)麒麟下的安裝部署,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
如何給asp.net core寫(xiě)個(gè)簡(jiǎn)單的健康檢查
這篇文章主要給大家介紹了關(guān)于如何給asp.net core寫(xiě)個(gè)簡(jiǎn)單的健康檢查的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用asp.net core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
asp.net實(shí)現(xiàn)生成縮略圖及給原始圖加水印的方法示例
這篇文章主要介紹了asp.net實(shí)現(xiàn)生成縮略圖及給原始圖加水印的方法,結(jié)合具體實(shí)例形式分析了asp.net圖片的縮略圖與水印操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10
獲取轉(zhuǎn)向地址的URL的源文件(可自定義REFER)
獲取轉(zhuǎn)向地址的URL的源文件(可自定義REFER)...2006-09-09
ASP.NET State service狀態(tài)服務(wù)的問(wèn)題解決方法
每次重啟機(jī)器以后,在.NET2005上跑Web程序老是遇到這樣的問(wèn)題2008-11-11

