- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Microsoft.Office.Interop.Word;
- namespace MvcApplication1.Controllers
- {
- public class HomeController : Controller
- {
- //
- // GET: /Home/
- public ActionResult Index(string type, int id = 0)
- {
- ViewBag.ID = id;
- ViewBag.Type = type;
- return View();
- }
- [HttpPost]
- public ActionResult Index(FormCollection collection)
- {
- Toprint();
- return View();
- }
- public void Toprint()
- {
- WriteIntoWord wiw = new WriteIntoWord();
- string FilePath = Server.MapPath("jkxy01.dot"); //模板路径
- var filename = "qjjf" + DateTime.Now.ToString("yyyyMMddHHmmss");
- string Bookmarkjkqx = "jkqx";
- string Filljkqx = "400";
- string SaveDocPath = Server.MapPath(filename+".doc");
- wiw.OpenDocument(FilePath);
- //写入书签
- wiw.WriteIntoDocument("IdCard", "123");
- wiw.WriteIntoDocument("chujieren", "借款人");
- wiw.WriteIntoDocument(Bookmarkjkqx, Filljkqx);
- wiw.Save_CloseDocument(SaveDocPath);
- //转成pdf
- string SavePdfPath = Server.MapPath("contract/"+filename+".pdf");
- var flag= wiw.WordToPdf(SaveDocPath, SavePdfPath);
- if (flag)
- {
- //删除临时word,
- System.IO.File.Delete(SaveDocPath);
- }
- }
- }
- public class WriteIntoWord
- {
- private ApplicationClass app = null; //定义应用程序对象
- private Document doc = null; //定义 word 文档对象
- private Object missing = System.Reflection.Missing.Value; //定义空变量
- private Object isReadOnly = false;
- // 向 word 文档写入数据
- public void OpenDocument(string FilePath)
- {
- object filePath = FilePath; //文档路径
- app = new ApplicationClass(); //打开文档
- doc = app.Documents.Open(ref filePath, ref missing, ref missing, ref missing,
- ref missing, ref missing, ref missing, ref missing);
- doc.Activate(); //激活文档
- }
- /// <summary>
- /// 打开word,将对应数据写入word里对应书签域
- /// </summary>
- ///<param name="parLableName">域标签</param>
- /// <param name="parFillName">写入域中的内容</param>
-
- public void WriteIntoDocument(string BookmarkName, string FillName)
- {
- object bookmarkName = BookmarkName;
- Bookmark bm = doc.Bookmarks.get_Item(ref bookmarkName); //返回书签
- bm.Range.Text = FillName; //设置书签域的内容
- }
- /// <summary>
- /// 保存并关闭
- /// </summary>
- /// <param name="parSaveDocPath">文档另存为的路径</param>
- ///
- public void Save_CloseDocument(string SaveDocPath)
- {
- object savePath = SaveDocPath; //文档另存为的路径
- Object saveChanges = app.Options.BackgroundSave; //文档另存为
- doc.SaveAs(ref savePath, ref missing, ref missing, ref missing, ref missing,
- ref missing, ref missing, ref missing);
- doc.Close(ref saveChanges, ref missing, ref missing); //关闭文档
- app.Quit(ref missing, ref missing, ref missing); //关闭应用程序
- }
- /// <summary>
- /// 把Word文件转换成pdf文件
- /// </summary>
- /// <param name="sourcePath">需要转换的文件路径和文件名称</param>
- /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
- /// <returns>成功返回true,失败返回false</returns>
- public bool WordToPdf(object sourcePath, string targetPath)
- {
- bool result = false;
- WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;
- object missing = Type.Missing;
- Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
- Document document = null;
- try
- {
- applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
- document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
- if (document != null)
- {
- document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);
- }
- result = true;
- }
- catch
- {
- result = false;
- }
- finally
- {
- if (document != null)
- {
- document.Close(ref missing, ref missing, ref missing);
- document = null;
- }
- if (applicationClass != null)
- {
- applicationClass.Quit(ref missing, ref missing, ref missing);
- applicationClass = null;
- }
- }
- return result;
- }
- }
- }
复制代码 需要引用using Microsoft.Office.Interop.Word;
Microsoft.Office.Interop.Word.rar
(381.81 KB, 下载次数: 0)
|