web 实现 导出word

chenyongxin123 2012-08-14 02:01:41

1、给我一个全的例子,可以实现调用模版,把数据库值放到相应的位置上。
2、要求点击导出按钮,直接打开word,不需要点任何东西。
3、打开后,让word显示到最前面
在线等
...全文
196 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenyongxin123 2012-08-15
  • 打赏
  • 举报
回复


protected void BTNdc_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application appWord = null;//应用程序
Microsoft.Office.Interop.Word.DocumentClass doc = null;//文档
try
{
appWord = new Microsoft.Office.Interop.Word.Application();
appWord.Visible = false;
object objTrue = true;
object objFalse = false;
//J:\newsys\MB\ReportForm
object objTemplate = "j:/newsys/MB/ReportForm/打印交办单.docx";//模板路径

object objDocType = WdDocumentType.wdTypeDocument;
doc = (DocumentClass)appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);


//给书签赋值
SetBookMarksValue(doc, "BH", "哈哈");


//第三步 生成word
appWord.Visible = true;
object filename = "j:/newsys/MB/ExcelTempFile/" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".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 = 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 ex)
{
//捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
string aa = ex.ToString();
object miss = System.Reflection.Missing.Value;
object missingValue = Type.Missing;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
}
private void SetBookMarksValue(Microsoft.Office.Interop.Word.DocumentClass doc, object name, string value)
{
doc.Bookmarks.get_Item(ref name).Range.Text = value;
}


这个是直接保存到服务器端,有没有直接打开word把数据放进去。修改哪个地方。

chenyongxin123 2012-08-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
给你后你不结贴怎么办?
[/Quote]
能帮到我的我都给分
chenyongxin123 2012-08-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
看过楼主的发帖记录。。

从8号到今天问的都是同一个问题。。

为什么一个星期了还没得到解决,楼主应该反思一下。

学编程,先学做人。
[/Quote]
我这边有3套方案,早已经解决了,就是想看看还有没有别的方法
hfdsoft 2012-08-14
  • 打赏
  • 举报
回复
看过楼主的发帖记录。。

从8号到今天问的都是同一个问题。。

为什么一个星期了还没得到解决,楼主应该反思一下。

学编程,先学做人。
hfdsoft 2012-08-14
  • 打赏
  • 举报
回复

using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using Aspose.Words;

namespace WJSService
{
/// <summary>
/// WordCreate 的摘要说明
/// </summary>
[WebService(Namespace = "http://www.heibing.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WordCreate : BaseService
{
[SoapHeader("SecHeader"), WebMethod(Description = "生成指定模板的WORD文件")]
public bool Convert(DAL.Doc entity,string templateName,bool isNeedRedHead)
{
entity.Content = entity.Content.Replace("\r\n\r\n", "\r\n");
var blnTemp = false;
try
{
//打开模板文件
var doc = new Document(Config.TemplateRootPath + templateName);
//反射属性
var entityProperties = typeof(DAL.Doc).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
//书签替换
foreach (var info in entityProperties)
{
try
{
var mark = doc.Range.Bookmarks[info.Name];
if (mark != null){mark.Text = info.GetValue(entity, null).ToString();}
//同一个书签名称不能多次使用,哥暂时用了个蠢办法
for (var i = 0; i < 4; i++)
{
var mark2 = doc.Range.Bookmarks[string.Format("{0}Item{1}",info.Name,i)];
if (mark2 != null){mark2.Text = info.GetValue(entity, null).ToString();}
}
}
catch(Exception ex)
{
ModifyFile(string.Format("D:\\ConvertLog{0}.txt", DateTime.Now), ex.Message + ex.Source + ex.Data);
}

}
//插入文件红头
doc.MailMerge.Execute(new[] { "Logo" },
isNeedRedHead
? new object[] { Config.TemplateRootPath + @"RedHead\" + entity.HTText + ".jpg" }
: new object[] { Config.TemplateRootPath + @"RedHead\blank.jpg" });

//保存文件
string filename = string.Format(templateName == "服务提供过程记录.doc" ? "{0}服务提供过程记录.doc" : "{0}.doc", entity.NoModel);


doc.Save(Config.DocumentSaveRootPath + filename, SaveFormat.Doc);
blnTemp = true;
}
catch (Exception ex)
{

ModifyFile(string.Format("D:\\Log{0}.txt", DateTime.Now), ex.Message + ex.Source + ex.Data);
blnTemp = false;
}
return blnTemp;
}
}
}

风一样的大叔 2012-08-14
  • 打赏
  • 举报
回复
给你后你不结贴怎么办?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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