110,538
社区成员
发帖
与我相关
我的任务
分享
protected void BTNdc_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application appWord = null;//应用程序
Microsoft.Office.Interop.Word.DocumentClass doc = null;//文档
try
{
appWord = new Microsoft.Office.Interop.Word.Application();
appWord.Visible = false;
object objTrue = true;
object objFalse = false;
//J:\newsys\MB\ReportForm
object objTemplate = "j:/newsys/MB/ReportForm/打印交办单.docx";//模板路径
object objDocType = WdDocumentType.wdTypeDocument;
doc = (DocumentClass)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
//给书签赋值
SetBookMarksValue(doc, "BH", "哈哈");
//第三步 生成word
appWord.Visible = true;
object filename = "j:/newsys/MB/ExcelTempFile/" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
object miss = System.Reflection.Missing.Value;
doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object missingValue = Type.Missing;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
catch (System.Exception ex)
{
//捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
string aa = ex.ToString();
object miss = System.Reflection.Missing.Value;
object missingValue = Type.Missing;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
}
private void SetBookMarksValue(Microsoft.Office.Interop.Word.DocumentClass doc, object name, string value)
{
doc.Bookmarks.get_Item(ref name).Range.Text = value;
}
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using Aspose.Words;
namespace WJSService
{
/// <summary>
/// WordCreate 的摘要说明
/// </summary>
[WebService(Namespace = "http://www.heibing.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WordCreate : BaseService
{
[SoapHeader("SecHeader"), WebMethod(Description = "生成指定模板的WORD文件")]
public bool Convert(DAL.Doc entity,string templateName,bool isNeedRedHead)
{
entity.Content = entity.Content.Replace("\r\n\r\n", "\r\n");
var blnTemp = false;
try
{
//打开模板文件
var doc = new Document(Config.TemplateRootPath + templateName);
//反射属性
var entityProperties = typeof(DAL.Doc).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
//书签替换
foreach (var info in entityProperties)
{
try
{
var mark = doc.Range.Bookmarks[info.Name];
if (mark != null){mark.Text = info.GetValue(entity, null).ToString();}
//同一个书签名称不能多次使用,哥暂时用了个蠢办法
for (var i = 0; i < 4; i++)
{
var mark2 = doc.Range.Bookmarks[string.Format("{0}Item{1}",info.Name,i)];
if (mark2 != null){mark2.Text = info.GetValue(entity, null).ToString();}
}
}
catch(Exception ex)
{
ModifyFile(string.Format("D:\\ConvertLog{0}.txt", DateTime.Now), ex.Message + ex.Source + ex.Data);
}
}
//插入文件红头
doc.MailMerge.Execute(new[] { "Logo" },
isNeedRedHead
? new object[] { Config.TemplateRootPath + @"RedHead\" + entity.HTText + ".jpg" }
: new object[] { Config.TemplateRootPath + @"RedHead\blank.jpg" });
//保存文件
string filename = string.Format(templateName == "服务提供过程记录.doc" ? "{0}服务提供过程记录.doc" : "{0}.doc", entity.NoModel);
doc.Save(Config.DocumentSaveRootPath + filename, SaveFormat.Doc);
blnTemp = true;
}
catch (Exception ex)
{
ModifyFile(string.Format("D:\\Log{0}.txt", DateTime.Now), ex.Message + ex.Source + ex.Data);
blnTemp = false;
}
return blnTemp;
}
}
}