asp.net 按指定模板导出word,pdf实例代码_实用技巧

复制代码 代码如下:

/// <summary>
        /// 导出word文件
        /// </summary>
        /// <param name="templateFile">模板路径</param>
        /// <param name="fileNameWord">导出文件名称</param>
        /// <param name="fileNamePdf">pdf文件名称</param>
        /// <param name="bookmarks">模板内书签集合</param>
        /// <param name="invoiceline">发票条目列表</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;//获得书签名                   
                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);
        }

调用

复制代码 代码如下:

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);

新建一个word,在需要替换的位置插入书签,使用以上方法即可将书签处替换为指定内容,并且另存为pdf

时间: 2024-09-17 20:44:31

asp.net 按指定模板导出word,pdf实例代码_实用技巧的相关文章

asp.net Linq把数据导出到Excel的代码_实用技巧

前些时间有朋友为了完成此功能,就硬把数据导入DataGrid再导出到Excel.这实在是多此一举. 解决办法: 通过Linq将数据读出,并直接写入数据流中 代码如下: 复制代码 代码如下: public partial class DataToExcel : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataAccess.DataClassesDataContext db = new D

ASP.NET实现TreeView的XML数据源绑定实例代码_实用技巧

TreeView控件可以使用XML文档作为数据源,根据XML文档的层次结构显示节点.而XML文档的访问由XmlDataSource控件来完成,从XmlDataSource控件的DataFile属性中指定XML文档路径,然后在TreeView控件中设置与XML文档中的节点的对应关系.本示例将演示如何把TreeView控件绑定到XML数据源. 技术要点把TreeView控件绑定到XML数据源的技术要点如下. 使用XmlDataSource控件提供对XML文档的访问. 在TreeView控件的Data

asp.net中生成缩略图并添加版权实例代码_实用技巧

复制代码 代码如下: //定义image类的对象Drawing.Image image,newimage;//图片路径protected string imagePath;//图片类型protected string imageType;//图片名称protected string imageName;//提供一个回调方法,用于确定Image对象在执行生成缩略图操作时何时提前取消执行//如果此方法确定 GetThumbnailImage 方法应提前停止执行,则返回 true:否则返回 false

ASP.NET中下载文件的几种实例代码_实用技巧

复制代码 代码如下:   //TransmitFile实现下载     protected void Button1_Click(object sender, EventArgs e)    {        /*         微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite         下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题.         代码如下:        

asp.net 图标提取以及图标转换的实例代码_实用技巧

复制代码 代码如下: using System;using System.Collections.Generic;using System.Drawing;using System.Windows.Forms;using System.IO;using System.Resources;using System.Reflection;using System.Runtime.InteropServices; namespace newExtIcon{    public partial clas

asp.net导出EXCEL的功能代码_实用技巧

复制代码 代码如下: //由gridviw导出为Excel public static void ToExcel(System.Web.UI.Control ctl) { HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls"); HttpContext.Current.Response.Charset = "UTF-8

asp.net上传图片并作处理水印与缩略图的实例代码_实用技巧

方法类: 复制代码 代码如下: upFileClass.cs using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlC

ASP.NET实现级联下拉框效果实例讲解_实用技巧

用ASP.NET控件实现部门和员工的联动,参考过程如下效果图:  Default.aspx代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/199

Asp.net中安全退出时清空Session或Cookie的实例代码_实用技巧

概览: 网站中点击退出,如果仅仅是重定向到登录/出页面,此时在浏览器地址栏中输入登录后的某个页面地址如主页,你会发现不用登录就能访问.这种所谓的退出并不是安全的. 那么怎样做到安全退出呢? 那就是点击退出后清空相应的Session或Cookie. 清空Session的代码: Session.Clear(); Session.Abandon(); 清除Cookie的正确代码(假设Cookie名称为UserInfo): if (Request.Cookies["UserInfo"] !=