生成Word文档

理不完的逻辑 2011-04-12 02:28:58
菜鸟提问:我有一个word模板,希望通过程序来填充数据,保存成wword格式,如何实现?
最好有实例,谢谢~!
...全文
78 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
子夜__ 2011-04-13
  • 打赏
  • 举报
回复
Object Nothing=System.Reflection.Missing.value;
//取得Word文件保存路径
object filename=@SaveAs.Text;
//创建一个名为WordApp的组件对象
Word.Application WordApp=new Word.ApplicationClass();
//创建一个名为WordDoc的文档对象
Word.Document WordDoc=WordApp.Documents.Add(ref Nothing,ref Nothing,
ref Nothing,ref Nothing);
//增加一表格
Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,1,1,ref Nothing,ref Nothing);
//在表格第一单元格中添加自定义的文字内容
table.Cell(1,1).Range.Text=wordText.Text;
//在文档空白地方添加文字内容
WordDoc.Paragraphs.Last.Range.Text="Wellcome To Aspxcn.Com";
//将WordDoc文档对象的内容保存为DOC文档
WordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,
ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,
ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
//关闭WordDoc文档对象
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
//返回结果
result.Text="文档路径:<a href="/"+SaveAs.Text+"'>"+SaveAs.Text
+"</a>(点击链接查看)<br/>生成结果:成功!";
ycproc 2011-04-12
  • 打赏
  • 举报
回复
private void TestPrint()
{//利用word模板生成


Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
//模板文件
string TemplateFile = @"E:\work\testPrint.doc";//@"E:\work\testPrint.doc";
TemplateFile = Server.MapPath("~/UpLoad/TestWord/testPrint.doc");
//生成的具有模板样式的新文件
string FileName = @"C:\Documents and Settings\Administrator\桌面\" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc";//@"C:\Documents and Settings\Administrator\桌面\" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc";
string Fname = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".doc";
FileName = Server.MapPath("~/UpLoad/TestWord/" + Fname);
//模板文件拷贝到新文件
File.Copy(TemplateFile, FileName);
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
object Obj_FileName = FileName;
object Visible = false;
object ReadOnly = false;
object missing = System.Reflection.Missing.Value;
//打开文件
doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref Visible,
ref missing, ref missing, ref missing,
ref missing);
doc.Activate();

#region MyRegion
//foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc.Bookmarks)
//{
// if (bm.Name == "a")
// {
// bm.Select();
// bm.Range.Text = this.txtTitle.Value;
// }
// //else if (bm.Name == "class")
// //{
// // bm.Select();
// // bm.Range.Text = ViewState["_class"].ToString();
// //}
// //else if (bm.Name == "caizhi")
// //{
// // bm.Select();
// // bm.Range.Text = ViewState["_caizhi"].ToString();
// //}
// //else if (bm.Name == "guige")
// //{
// // bm.Select();
// // bm.Range.Text = ViewState["_guige"].ToString();
// //}
// //else if (bm.Name == "chicun")
// //{
// // bm.Select();
// // bm.Range.Text = txtSize.Text;
// //}
// //else if (bm.Name == "count")
// //{
// // bm.Select();
// // bm.Range.Text = txtCount.Text;
// //}
// //else if (bm.Name == "sweight")
// //{
// // bm.Select();
// // bm.Range.Text = txtSingleWeight.Text;
// //}
// //else if (bm.Name == "weight")
// //{
// // bm.Select();
// // bm.Range.Text = txtWeight.Text;
// //}

//}
#endregion
object IsSave = true;
doc.Close(ref IsSave, ref missing, ref missing);

//hplTestWord.Visible = true;
//hplTestWord.Target = "_blank";
//hplTestWord.NavigateUrl = "~/UpLoad/TestWord/" + Fname;

Response.Write("<script language='javascript'>alert('生成word成功!');</script>");


}
斯赛 2011-04-12
  • 打赏
  • 举报
回复

Application wordApp = new ApplicationClass();
Document o = this.OpenDoc(‘路径’, ref wordApp, 1);
foreach (Bookmark bookmark in o.get_Bookmarks())
{

bookmark .Select();
bookmark .get_Range().set_Text('文字');

}


private Document OpenDoc(string strDocPath, ref Application WordApp, int flag)
{
Document document2;
if (!File.Exists(strDocPath))
{
return null;
}
object obj2 = strDocPath;
object missing = this.missing;
object obj4 = this.missing;
WordApp.set_Visible(false);
try
{
document2 = WordApp.get_Documents().Open(ref obj2, ref this.missing, ref obj4, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing, ref missing, ref this.missing, ref this.missing, ref this.missing, ref this.missing);
}
catch (Exception exception)
{
throw new Exception(exception.Message);
}
return document2;
}


62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧