admin管理员组文章数量:1645534
在做网页开发的时候,经常会遇到下载需求,以下这种方式是保存为原生的word格式。非直接将HTML另存为。
1. 首先是下载为word 的代码。
引用:
using MSWord = Microsoft.Office.Interop.Word;
using Microsoft.Ajax.Utilities;
using Microsoft.Office.Interop.Word;
相关代码:
var str = "全文内容XXXX";
string titleName="标题";
//判断服务器上生成的下载文件保存目录,没有则生成
string uploadPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "download/";
string path2 = uploadPath + filename;
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
//先判断之前是否生成了同名的文件
if (!File.Exists(path2))
{
_Application WordApp;
try
{//创建word.exe
WordApp = (Microsoft.Office.Interop.Word.Application)
Microsoft.VisualBasic.Interaction.GetObject("", "Word.Application");
}
catch
{
//如果报错、则关闭所有word进程,重新打开。
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
{
p.CloseMainWindow();
}
WordApp = (Microsoft.Office.Interop.Word.Application)
Microsoft.VisualBasic.Interaction.GetObject("", "Word.Application");
}
//}
//生成word并保存。
object path; //声明文件路径变量
string wordstr; //声明word文档内容
object Nothing = System.Reflection.Missing.Value; //COM调用时用于占位
Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); // MSWord.Document worddoc; //声明word文档变量
//初始化变量
object format = MSWord.WdSaveFormat.wdFormatDocument; //Word文档的保存格式
//向文档中写入内容
wordstr = str;
WordDoc.Paragraphs.Last.Range.Text = wordstr;
path = uploadPath + filename; //设置文件保存路径
WordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing);
object visible = WdSaveOptions.wdDoNotSaveChanges;
WordDoc.Close(ref visible, ref Nothing, ref Nothing);
WordApp.Quit(ref visible, ref Nothing, ref Nothing);
}
//下载
path2 = uploadPath + filename;
var Response = HttpContext.Current.Response;
FileInfo fileInfo = new FileInfo(path2);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
2.Microsoft.Office.Interop.Word组件的配置相关
首先是主要安装Office,不激活也行,主要是需要用到 Microsoft Word 97 - 2003 文档 这个组件。 然后就是权限等设置,网上能找到一堆类似以下的说明。方法1:配置Web.Config文件,在每次请求时模拟本地系统的账户。
具体操作:在Web.Config文件中添加如下节点:
<identity impersonate="true" userName="accountname" password="password" />
其中:userName是要模拟的本地账号,password是该账号的密码。
方法2:在“DCOM配置”中,为IIS账号配置操作Word(其他Office对象也一样)的权限。
具体操作:“组件服务(Component Service)”->计算机(Computers)->我的电脑(My Computer)->DCOM配置(DCOM Config)->Microsoft Office Word 97 - 2003 文档,右击“Microsoft Office Word 97 - 2003 文档”,选择“属性”进行一下两步操作:
(1)在【标识(Identity)】选项卡中选中“交互式用户(The interactive user)”.
(2)在【安全(Security)】选项卡中,分别给前两个组(启动和激活权限,访问权限)选择“自定义(customer)”,然后点“编辑”,在弹出的界面中添加IIS账号(Server版的操作系统一般为NETWORK SERVICES,其他系统(XP)可能会是ASP.NET),并在下面的权限框中,给该用户分配所有权限。
方法3:为ASP.NET站点应用池分配本地账号
具体操作:在IIS中,为ASP.NET站点创建新的应用程序池,再改应用程序池属性的【标识(identity)】选项卡中,为“预定义账户”选择“本地系统(LocalSystem)”。
如果是IIS7.0中,则按以下步骤操作:为ASP.NET站点创建新的应用程序池。选中该应用程序池,高级设置->进程模式—>标识:选择localSystem。
以上三种方法中,个人测试需要第一和第二同时使用才正常。第三种暂未尝试,据说第三种更好。因为在操作完Word对象后,还需要关闭Word对象的进程,而第三种方法可以不要其他设置就能让ASP.NET应用程序有权限去结束这个word进程。
以上都设置完后,可能还是不行,还会报出无法创建、命令错误等。
重点来了,到以下目录新建2个文件夹,然后就正常了。
C:\Windows\SysWOW64\config\systemprofile\Desktop
C:\Windows\System32\config\systemprofile\Desktop
32位系统只需要建第二个即可。
版权声明:本文标题:C#,使用office组件Microsoft.Office.Interop.Word,将网页内容下载为word的demo及权限配置要点。 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1729394113a1199686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论