asp.net 向服务器上的word写数据出错,写本地word 没问题,请大神看看

caoyuanlaoshi 2018-05-10 10:24:59
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.Web;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
//string path = "I:\\Filldoc\\download"; //路径
//string path = Server.MapPath("~/") + "";
//string templatePath = "E:\\设计报告\\SH08J7G01.docx";
string templatePath = Server.MapPath("~/DesignRp/") + "SH08J7G01.docx";//该文件存在
WordOp wop = new WordOp();
wop.OpenTempelte(templatePath);
wop.FillLable("D", "A970");
//wop.FillLable("usr_name", "测试");
//wop.SaveAs("E:\\设计报告\\savetest.docx",true);
wop.Quit();
}
public class WordOp
{
public WordOp()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private ApplicationClass WordApp;
private Document WordDoc;
private static bool isOpened = false;//判断word模版是否被占用
public void SaveAs(string strFileName, bool isReplace)
{
if (isReplace && File.Exists(strFileName))
{
File.Delete(strFileName);
}
object missing = Type.Missing;
object fileName = strFileName;
WordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
//定义一个Word.Application 对象
public void activeWordApp()
{
WordApp = new ApplicationClass();
}
public void Quit()
{
object missing = System.Reflection.Missing.Value;
WordApp.Application.Quit(ref missing, ref missing, ref missing);
isOpened = false;
}
//基于模版新建Word文件
public void OpenTempelte(string strTemppath)
{
object Missing = Type.Missing;
//object Missing = System.Reflection.Missing.Value;
activeWordApp();
WordApp.Visible = false;
object oTemplate = (object)strTemppath;
try
{
while (isOpened)
{
System.Threading.Thread.Sleep(500);
}
WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing, ref Missing, ref Missing);
isOpened = true;
WordDoc.Activate();
}
catch (Exception Ex)
{
Quit();
isOpened = false;
throw new Exception(Ex.Message);
}
}
public void FillLable(string LabelId, string Content)
{
//打开Word模版
// OpenTempelte(tempName); //对LabelId的标签进行填充内容Content,即函件题目项
object bkmC = LabelId;
if (WordApp.ActiveDocument.Bookmarks.Exists(LabelId) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
}
WordApp.Selection.TypeText(Content);
//SaveAs(saveAsFileName);
//Quit();
}
}
class BR : Control
{
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<br />");
}
}
}


以上代码将数据写入word标签内部,写入本地word 没问题,写入服务器出错误,提示:
*****************************************************
“/”应用程序中的服务器错误。
Word 无法读取此文档,文档可能已损坏。
请尝试下列方法:
* 打开并修复文件。
* 用文本恢复转换器打开文件。
*******************************************************
请大神给看看是问题出在哪里》????
...全文
991 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
caoyuanlaoshi 2018-06-12
  • 打赏
  • 举报
回复
后来解决了,是我们公司的电脑本身有加密的设置,还是谢谢
还想懒够 2018-05-13
  • 打赏
  • 举报
回复
权限问题,配置一下应用程序池的权限,改成LocalSystem或指定账号。如果是指定账号,你再分配给相应的权限即可。
caoyuanlaoshi 2018-05-10
  • 打赏
  • 举报
回复
运行这个程序的时候出错:wop.OpenTempelte(templatePath);
caoyuanlaoshi 2018-05-10
  • 打赏
  • 举报
回复
是不是这句话有问题,还是本地服务器设置的问题? string templatePath = Server.MapPath("~/DesignRp/") + "SH08J7G01.docx";
caoyuanlaoshi 2018-05-10
  • 打赏
  • 举报
回复
这个问题一直没有解决,程序可以读到该文件,提示文件错误,如果把文件移除,则提示: *************************************************************** “/”应用程序中的服务器错误。 抱歉,找不到您的文件。是否可能被移动、重命名或删除? ******************************************************************** 电脑用的是word2013

62,041

社区成员

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

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

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

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