C#如何打印word模板

要在C#中调用Word模板替换掉里面的一些书签该怎么弄,希望能写的详细点,给加分

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.HtmlControls;
using Microsoft.Office;
using Microsoft.Office.Core;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Word;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string[] demostr ={ "测试数据1", "afsdasd", "fasdfdf", "sdfasdfee33", "asdfddeegadg", "fee都受到" };
string url = "http://" + this.Request.Url.Authority + "/demo/全国工业产品生产许可证申请登记表.doc";
string urlreadword = "/demo/Officebak/" + SetDocumentBookmarkData(url, demostr, Guid.NewGuid().ToString());
Page.RegisterStartupScript("msg", "<script>window.location='" + urlreadword + "';</script>");
}

//设定标签的数据
public string SetDocumentBookmarkData(string FileName, string [] demostr, string caseid)
{
//打开文档
Microsoft.Office.Interop.Word.Document wDoc = null;
Microsoft.Office.Interop.Word.Application wApp = null;
this.OpenWordDoc(FileName, ref wDoc, ref wApp);
object oEndOfDoc = "\\endofdoc";
object missing = System.Reflection.Missing.Value;
//设定标签数据
System.Collections.IEnumerator enu = wApp.ActiveDocument.Bookmarks.GetEnumerator();

string[] strbook = new string[demostr.Length ];
int i = 0;
Microsoft.Office.Interop.Word.Bookmark bk = null;
while (enu.MoveNext())
{
bk = (Microsoft.Office.Interop.Word.Bookmark)enu.Current;

if (bk.Name.ToString().Trim() != "Table")
{
strbook[i] = bk.Name.ToString();
i++;
}
}

object tempobject = null;
int length = 0;
for (i = 0; i < strbook.Length; i++)
{
tempobject = strbook[i].ToString();
if (wApp.ActiveDocument.Bookmarks.Exists(strbook[i].ToString()))
{
wApp.ActiveDocument.Bookmarks.get_Item(ref tempobject).Select();
wApp.Selection.Text = demostr[i].ToString ();
}

}
Microsoft.Office.Interop.Word.Table wordTablexSoft = wDoc.Tables[1];
InsertTabletoData(wordTablexSoft, ref wDoc, ref wApp);

//收尾工作
object o = null;

//string guid = System.Guid.NewGuid().ToString();
string guid = caseid;
object sFileName = Server.MapPath("/demo/Officebak/" + guid + ".doc");
if (wDoc.SaveFormat == (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument)
{
wDoc.Application.ActiveDocument.SaveAs(ref sFileName, 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);
}

wDoc.Close(ref missing, ref missing, ref missing);
wApp.Quit(ref missing, ref missing, ref missing);

if (wDoc != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc);
wDoc = null;
}

if (wApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp);
wApp = null;
}

GC.Collect();

return guid + ".doc";
}

#region 打开word模板和word文件

public static void OpenWordDot()
{
//不建议用非标准doc 此处方法 略
}

private void OpenWordDoc(string FileName, ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
if (FileName == "") return;
Microsoft.Office.Interop.Word.Document thisDocument = null;
Microsoft.Office.Interop.Word.FormFields formFields = null;
Microsoft.Office.Interop.Word.Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
thisApplication.Visible = true;
thisApplication.Caption = "";
thisApplication.Options.CheckSpellingAsYouType = false;
thisApplication.Options.CheckGrammarAsYouType = false;

Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = true;
Object AddToRecentFiles = false;

Object PasswordDocument = System.Type.Missing;
Object PasswordTemplate = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePasswordDocument = System.Type.Missing;
Object WritePasswordTemplate = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XMLTransform = System.Type.Missing;

try
{
Microsoft.Office.Interop.Word.Document wordDoc =
thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
ref NoEncodingDialog, ref XMLTransform);

thisDocument = wordDoc;
wDoc = wordDoc;
WApp = thisApplication;
formFields = wordDoc.FormFields;
}
catch (Exception ex)
{

}
}

#endregion
/// <summary>
/// 在objtbale 标签上新增表
/// </summary>
/// <param name="objTable"></param>
/// <param name="wDoc"></param>
/// <param name="WApp"></param>
/// <returns></returns>
private bool InsertTabletoData(Microsoft.Office.Interop.Word.Table wordTable, ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{

object Rownum = 5;
object Columnnum = 1;
wordTable.Cell(18, 2).Split(ref Rownum, ref Columnnum);
wordTable.Cell(18, 3).Split(ref Rownum, ref Columnnum);
wordTable.Cell(18, 4).Split(ref Rownum, ref Columnnum);
wordTable.Cell(18, 5).Split(ref Rownum, ref Columnnum);
for (int i = 0; i < 5; i++)
{
wordTable.Cell(18 + i, 2).Range.Text = "测试1列" + i + "行";
wordTable.Cell(18 + i, 3).Range.Text = "测试2列" + i + "行";
wordTable.Cell(18 + i, 4).Range.Text = "测试3列" + i + "行";
wordTable.Cell(18 + i, 5).Range.Text = "测试4列" + i + "行";
}

return true;
}
/// <summary>
/// 在objtbale 标签上新增表
/// </summary>
/// <param name="objTable"></param>
/// <param name="wDoc"></param>
/// <param name="WApp"></param>
/// <returns></returns>
private bool InsertTabletoBookmark(string objTable, ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
object oEndOfDoc = "\\endofdoc";
object missing = System.Reflection.Missing.Value;

object objBookmark = objTable;

WApp.ActiveDocument.Bookmarks.get_Item(ref objBookmark).Select();

Microsoft.Office.Interop.Word.Table table = wDoc.Tables.Add(WApp.Selection.Range, 3, 4, ref missing, ref missing);
table.Cell(1, 1).Range.Text = "表:" + WApp.ActiveDocument.Bookmarks.get_Item(ref objBookmark).Range.Tables[1].Rows.Count;

return true;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-03-20

第一课熟悉word开始窗及创建word模板

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网