C#如何打印word模板
2013-04-19
展开全部
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;
}
}
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;
}
}
博思aippt
2024-07-20 广告
2024-07-20 广告
作为深圳市博思云创科技有限公司的工作人员,对于Word文档生成PPT的操作,我们有以下建议:1. 使用另存为功能:在Word中编辑完文档后,点击文件->另存为,选择PowerPoint演示文稿(*.pptx)格式,即可将文档内容转换为PPT...
点击进入详情页
本回答由博思aippt提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询