菜鸟提问:我有一个word模板,希望通过程序来填充数据,保存成wword格式,如何实现?
最好有实例,谢谢~!
最好有实例,谢谢~!
解决方案 »
- Cookie ie6 存取不成功!各位的解决方案是什么?
- .net web页面点击按钮运行时屏蔽所有按钮的问题
- form表单提交怎么对table分页
- 事件执行次序的疑问
- 在C#后台中如何写脚本?关于FLASH的
- 用户上传个图片,怎么在显示的时候怎么控制大小呢?
- 在一个页面中用了frameset里面定义了两个frame 。。。。。。。。。。。
- webHtmlEdit 如何获取上传的文件名字
- 我有A,B两个类在同一个名称空间中,但是在不同的cs文件中,请问B怎么继承A
- 不能访问远程数据库的问题error: 40 - Could not open a connection to SQL Server
- DataList控件如何编写前台代码
- 求这个表分页语句!
Application wordApp = new ApplicationClass();
Document o = this.OpenDoc(‘路径’, ref wordApp, 1);
foreach (Book book in o.get_Books())
{
book .Select();
book .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;
}
{//利用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.Book bm in doc.Books)
//{
// 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>");
}
//取得Word文件保存路径
object [email protected];
//创建一个名为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/>生成结果:成功!";