ASP.NET(C#):如何利用word模板dot文件生成报表。 100
1个回答
展开全部
ASP.NET利用dot模板生成Word文档代码,假定有一个模板文件位于网站Mywebsite/templates/a.dot,现在需要利用Word.ApplicationClass和Word.Document类来生成一个word文档。关键代码如下:
//初始化生成word文档需要的信息
Word.Application appWord =null;
Word.Document doc = null;
try
{
appWord = new Word.ApplicationClass();
appWord.Visible = false;
object objTrue = true;
object objFalse = false;
object objTemplate = Server.MapPath(@"templates\a.dot");//模板路径
object objDocType = Word.WdDocumentType.wdTypeDocument;
doc = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
//第一步生成word文档
//定义书签变量
object obDD_Name = "DD_Name";//姓 名
object obDD_Sex = "DD_Sex";//性 别
object obDD_Age = "DD_Age";//年龄
object obDD_Birthday = "DD_Birthday"; //出生年月
object obDD_Nation = "DD_Nation"; //民 族
object obDD_Native = "DD_Native"; //籍 贯
//第二步 读取数据,填充数据集
SqlDataReader dr = XXXXX;//读取出来的数据集
//第三步 给书签赋值
//给书签赋值
doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = dr["appliername"].ToString(); //姓 名
doc.Bookmarks.get_Item(ref obDD_Sex).Range.Text = getCodeFullNameStd("性别", dr["Sex"].ToString());//性 别
doc.Bookmarks.get_Item(ref obDD_Age).Range.Text = Convert.ToString(DateTime.Now.Year - ((DateTime)dr["Birthday"]).Year);//年龄
.
//第四步 生成word
object filename = Server.MapPath("file") + "\\" + "XXXXXXXXXX情况表" +".doc";
object miss = System.Reflection.Missing.Value;
doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object missingValue = Type.Missing;
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
catch (System.Exception e)
{
//捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
string aa = e.ToString();
object miss = System.Reflection.Missing.Value;
object missingValue = Type.Missing;
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
//初始化生成word文档需要的信息
Word.Application appWord =null;
Word.Document doc = null;
try
{
appWord = new Word.ApplicationClass();
appWord.Visible = false;
object objTrue = true;
object objFalse = false;
object objTemplate = Server.MapPath(@"templates\a.dot");//模板路径
object objDocType = Word.WdDocumentType.wdTypeDocument;
doc = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
//第一步生成word文档
//定义书签变量
object obDD_Name = "DD_Name";//姓 名
object obDD_Sex = "DD_Sex";//性 别
object obDD_Age = "DD_Age";//年龄
object obDD_Birthday = "DD_Birthday"; //出生年月
object obDD_Nation = "DD_Nation"; //民 族
object obDD_Native = "DD_Native"; //籍 贯
//第二步 读取数据,填充数据集
SqlDataReader dr = XXXXX;//读取出来的数据集
//第三步 给书签赋值
//给书签赋值
doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = dr["appliername"].ToString(); //姓 名
doc.Bookmarks.get_Item(ref obDD_Sex).Range.Text = getCodeFullNameStd("性别", dr["Sex"].ToString());//性 别
doc.Bookmarks.get_Item(ref obDD_Age).Range.Text = Convert.ToString(DateTime.Now.Year - ((DateTime)dr["Birthday"]).Year);//年龄
.
//第四步 生成word
object filename = Server.MapPath("file") + "\\" + "XXXXXXXXXX情况表" +".doc";
object miss = System.Reflection.Missing.Value;
doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object missingValue = Type.Missing;
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
catch (System.Exception e)
{
//捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
string aa = e.ToString();
object miss = System.Reflection.Missing.Value;
object missingValue = Type.Missing;
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
追问
word 这部分我已经做好了的。我的问题是:利用word模板dot文件生成报表。就是设定一个标签,在那插入报表。
不过还是谢谢。
追答
一样的啊 直接写入word就可以了啊,你已经会写如word了还 在乎写什么进去吗?至于格式可以慢慢的着磨,只是生成报表应该先有数据啊。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询