高分提问:.net操作Word:我做了一个Word模板,我想实现这样的功能,在数据库去数据然后,替换word摸班的内容,保存为临时文件,在IE中打 .net操作Word:我做了一个Word模板,我想实现这样的功能,在数据库去数据然后,替换word摸班的内容,保存为临时文件,在IE中打开????怎么做呀,。我找了一个原先的帖子,有是有关于Word的操作,但是没有这里的替换功能,怎么做呀 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用vbscript,我这里有个例子,你参考一下,我是在页面上直接生成的word。你换成打开现有word文档就可以了。如果再不清楚可以上msdn看一下,里面讲的内容比较多。Dim myWord,ActDoc,CurRange,CurTable Sub PrintChange() ' 取页面控件值 Dim FieldValue() Dim CurCell,tbWidth,FieldCount Set myWord = createobject("word.application") wordVer=myWord.version myWord.Documents.Add myWord.DisplayAlerts = 0'wdAlertsNone Set ActDoc = myWord.ActiveDocument ' 边框 With ActDoc.PageSetup .LineNumbering.Active = False .Orientation = 1'wdOrientLandscape .TopMargin = myWord.CentimetersToPoints(2.2) .BottomMargin = myWord.CentimetersToPoints(1) .LeftMargin = myWord.CentimetersToPoints(2) .RightMargin = myWord.CentimetersToPoints(2) End With With ActDoc.PageSetup.TextColumns .SetCount(2) .EvenlySpaced = True .LineBetween = False .Width = myWord.CentimetersToPoints(7) .Spacing = myWord.CentimetersToPoints(3.5) End With Set CurRange = ActDoc.Range(0, 0) With CurRange .Text = "变更事项" .Paragraphs.Alignment = 1'wdAlignParagraphCenter .Bold = True .Font.Size = 16 End With CurRange.Collapse 0 DrawTable CurRange.InsertParagraphAfter CurRange.Collapse 0 DrawTable CurRange.InsertParagraphAfter CurRange.Collapse 0 DrawTable CurRange.InsertParagraphAfter CurRange.Collapse 0 DrawTable CurRange.Collapse 0 CurRange.InsertParagraphAfter myWord.visible=true ActDoc.FormFields.Shaded = True myWord.activedocument.saved = True End Sub Sub DrawTable() Set CurTable = ActDoc.Tables.Add(CurRange, 4, 1) With CurTable.Rows(1).Range .Text = "经审定," .Paragraphs.Alignment = 0 'wdAlignParagraphLeft End With Set CurRange = CurTable.Rows(2).Range CurRange.FormFields.Add CurRange, 70 'wdFieldFormTextInput CurTable.Rows(2).Height = myWord.CentimetersToPoints(2) With CurTable.Rows(3).Range .Text = "同意变更。" + " " .Paragraphs.Alignment = 2 'wdAlignParagraphRight End With '先把整行分为两列 CurTable.Cell(4, 1).Split 1, 2 '再拆分第1列为两列处理审批人信息 CurTable.Cell(4, 1).Split 1, 2 With CurTable.Cell(4, 1).Range .Text = Space(5) + "审批人" .Paragraphs.Alignment = 0 'wdAlignParagraphLeft End With ' 域,审批人 Set CurRange = CurTable.Cell(4,2).Range CurRange.FormFields.Add CurRange, 70 'wdFieldFormTextInput CurTable.Cell(4, 3).Split 1, 6 CurTable.Cell(4, 4).Range.Text = "年" CurTable.Cell(4, 6).Range.Text = "月" CurTable.Cell(4, 8).Range.Text = "日" For i=1 To 3 Set CurRange = CurTable.Cell(4,2*i+1).Range CurRange.FormFields.Add CurRange, 70 'wdFieldFormTextInput Next Set CurRange = CurTable.Range CurRange.Collapse 0 End Sub /// <summary> /// 向Word写入数据 /// </summary> private void ToWord(string[] pStr_XTITLE,string[] pStr_CCONTENT) { object missing = System.Reflection.Missing.Value; object readOnly = false; object isVisible = true; object oTemplate="C:\\Inetpub\\wwwroot\\Evaluation\\file\\word.dot"; object file="c:\\myfile4.doc"; ApplicationClass oWordApp = new ApplicationClass(); oWordApp.Visible=true; // this.Response.Write(this.XTITLE.Text); Document oWordDoc = oWordApp.Documents.Open(ref oTemplate, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing,ref missing,ref missing); for (int i=0;i<pStr_XTITLE.Length;i++) { for (int j = 1; j <= oWordDoc.Books.Count; j++) { object objI = j; if(oWordDoc.Books.Item(ref objI).Name==pStr_XTITLE[i].Trim()) oWordDoc.Books.Item(ref objI).Range.Text =pStr_CCONTENT[i].Trim(); } /*object oBookMark =pStr_XTITLE[i].Trim(); oWordDoc.Books.Item(ref oBookMark).Range.Text =pStr_CCONTENT[i].Trim(); this.Response.Write(pStr_XTITLE[i]);*/ } oWordDoc.SaveAs(ref file,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); oWordApp.Application.Quit(ref missing, ref missing, ref missing); oWordApp=null; }不知是不是你所要的 jquery显示与隐藏啊,,急救啊!!! webservice操作session问题 qq农场外挂,登陆怎么返回...系统繁忙,是怎么回事,少参数?? asp.net 2个textbox赋值 一个比较简单又郁闷的问题 RegisterStartupScript (Type,string,string)使用 奇怪的数据库连接的问题. 如果更新单条记录? 大家在开发中是不是在webform中的form_load事件中加入身份验证的方法? 有关安装.net框架时的问题,请大家帮忙呀,为什么总是安装失败?不知为何原因,请指教!!!! 刷新页面的问题,急! 大型社区网项目外包,欢迎有能力人士接单
Dim myWord,ActDoc,CurRange,CurTable
Sub PrintChange()
' 取页面控件值
Dim FieldValue()
Dim CurCell,tbWidth,FieldCount
Set myWord = createobject("word.application")
wordVer=myWord.version
myWord.Documents.Add
myWord.DisplayAlerts = 0'wdAlertsNone
Set ActDoc = myWord.ActiveDocument
' 边框
With ActDoc.PageSetup
.LineNumbering.Active = False
.Orientation = 1'wdOrientLandscape
.TopMargin = myWord.CentimetersToPoints(2.2)
.BottomMargin = myWord.CentimetersToPoints(1)
.LeftMargin = myWord.CentimetersToPoints(2)
.RightMargin = myWord.CentimetersToPoints(2)
End With
With ActDoc.PageSetup.TextColumns
.SetCount(2)
.EvenlySpaced = True
.LineBetween = False
.Width = myWord.CentimetersToPoints(7)
.Spacing = myWord.CentimetersToPoints(3.5)
End With
Set CurRange = ActDoc.Range(0, 0)
With CurRange
.Text = "变更事项"
.Paragraphs.Alignment = 1'wdAlignParagraphCenter
.Bold = True
.Font.Size = 16
End With
CurRange.Collapse 0
DrawTable
CurRange.InsertParagraphAfter
CurRange.Collapse 0
DrawTable
CurRange.InsertParagraphAfter
CurRange.Collapse 0
DrawTable
CurRange.InsertParagraphAfter
CurRange.Collapse 0
DrawTable
CurRange.Collapse 0
CurRange.InsertParagraphAfter
myWord.visible=true
ActDoc.FormFields.Shaded = True
myWord.activedocument.saved = True End Sub Sub DrawTable()
Set CurTable = ActDoc.Tables.Add(CurRange, 4, 1) With CurTable.Rows(1).Range
.Text = "经审定,"
.Paragraphs.Alignment = 0 'wdAlignParagraphLeft
End With Set CurRange = CurTable.Rows(2).Range
CurRange.FormFields.Add CurRange, 70 'wdFieldFormTextInput
CurTable.Rows(2).Height = myWord.CentimetersToPoints(2) With CurTable.Rows(3).Range
.Text = "同意变更。" + " "
.Paragraphs.Alignment = 2 'wdAlignParagraphRight
End With '先把整行分为两列
CurTable.Cell(4, 1).Split 1, 2
'再拆分第1列为两列处理审批人信息
CurTable.Cell(4, 1).Split 1, 2 With CurTable.Cell(4, 1).Range
.Text = Space(5) + "审批人"
.Paragraphs.Alignment = 0 'wdAlignParagraphLeft
End With
' 域,审批人
Set CurRange = CurTable.Cell(4,2).Range
CurRange.FormFields.Add CurRange, 70 'wdFieldFormTextInput CurTable.Cell(4, 3).Split 1, 6 CurTable.Cell(4, 4).Range.Text = "年"
CurTable.Cell(4, 6).Range.Text = "月"
CurTable.Cell(4, 8).Range.Text = "日"
For i=1 To 3
Set CurRange = CurTable.Cell(4,2*i+1).Range
CurRange.FormFields.Add CurRange, 70 'wdFieldFormTextInput
Next
Set CurRange = CurTable.Range
CurRange.Collapse 0 End Sub
/// 向Word写入数据
/// </summary>
private void ToWord(string[] pStr_XTITLE,string[] pStr_CCONTENT)
{
object missing = System.Reflection.Missing.Value;
object readOnly = false;
object isVisible = true;
object oTemplate="C:\\Inetpub\\wwwroot\\Evaluation\\file\\word.dot";
object file="c:\\myfile4.doc";
ApplicationClass oWordApp = new ApplicationClass();
oWordApp.Visible=true;
// this.Response.Write(this.XTITLE.Text); Document oWordDoc = oWordApp.Documents.Open(ref oTemplate, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing,ref missing,ref missing);
for (int i=0;i<pStr_XTITLE.Length;i++)
{
for (int j = 1; j <= oWordDoc.Books.Count; j++)
{
object objI = j;
if(oWordDoc.Books.Item(ref objI).Name==pStr_XTITLE[i].Trim())
oWordDoc.Books.Item(ref objI).Range.Text =pStr_CCONTENT[i].Trim();
} /*object oBookMark =pStr_XTITLE[i].Trim();
oWordDoc.Books.Item(ref oBookMark).Range.Text =pStr_CCONTENT[i].Trim();
this.Response.Write(pStr_XTITLE[i]);*/
}
oWordDoc.SaveAs(ref file,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); oWordApp.Application.Quit(ref missing, ref missing, ref missing);
oWordApp=null;
}不知是不是你所要的