为了实现对word模板修改,并批量 保存下载到客户端的问题,网上查了很多资料,总是有这样那样的问题不能解决,好在终于解决了
要注意以下问题
1.访问目录权限的问题
对模板所在目录 和保存文件的目录权限进行设定 添加“everyone” 属性“完全控制” ;
2. 调用 Word.Application thisApplication = new Word.ApplicationClass(); 和Word.Document wordDoc = thisApplication.Documents.Add(ref Template, ref NewTemplate, ref DocumentType, ref Visible) 能正确new 和add
需要对“组件服务”中 DCOM程序 microsoft word程序 访问权限、启动和激活权限 设置需添加 “everyone” “本地访问”权限和“本地激活” “本地启动”权限,设置权限中也加入"everyone"完全控制
如果实在不行也把“network service” 用户也加进来
对于64位服务器的问题
(在64bit系统中的DCOM管理中添加32Bit的Excel、Word等的管理)
1).开始--〉运行--〉cmd
2)命令提示符下面,输入“mmc -32”回车,打开32的控制台
3).文件菜单中,添加删除管理单元--〉“组件服务”(在最下面),确认后,关闭即可。
因为是在64位系统上面操作,组件服务中DOCOM中默认是没有的,因为Microsoft Excel Application是32的DCOM配置,所以通过如下方式解决
3.下载问题
目前遇到一个问题,超过13条记录 使用 Response.Redirect("DownloadWord.aspx?id=" + "a.doc",false); 调转页面,跳不过去,查了问题所在,但目前还没解决,目前导出13条以内是没问题的,还有待大家交流,帮忙解决哦!
以下我的部分代码,从网上搜集整理的
1.读写word标签模板,并保存到服务器指定目录
//对标签进行赋值,并另存word到本地
for (int i = 0; i < idList.Count; i++)
{
WordHelper helper = new WordHelper();
helper.CreateNewWordDocument(path + "template.doc");
helper.Replace("sm", smList[i].ToString());
helper.Replace("zgbm", zgbmList[i].ToString());
helper.SaveAs(directory + xmList[i].ToString() + "呈批件.doc");
helper.Close();
}
try
{
Response.Redirect("DownloadWord.aspx",false);
}catch (Exception ex)
{
ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "alert", "alert('" + ex.ToString() + "!')", true);
}
WordHelper类 也是从网上找的哦,没自己写
不过这个姐姐写的不错 地址是 http://blog.sina.com.cn/s/blog_50f1c15d0100kbn6.html#
2. DownloadWord.cs 将目录打包成压缩文件,并下载,删除保存文件的目录,并创建新的目录,给新建目录添加everyone 完全控制权限
RARsave(directory, directory, "doc");
ResponseFile(directory + @"\doc.rar");
public void RARsave(string patch, string rarPatch, string rarName)
{
String the_rar;
RegistryKey the_Reg;
Object the_Obj;
String the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process;
try
{
the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
the_rar = the_rar.Substring(1, the_rar.Length - 7);
Directory.CreateDirectory(patch);
//命令参数
//the_Info = " a " + rarName + " " + @"C:Test?70821.txt"; //文件压缩
the_Info = " a " + rarName + " " + patch + " -r";
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = "WinRar";//the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//打包文件存放目录
the_StartInfo.WorkingDirectory = rarPatch;
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
the_Process.Close();
}
catch (Exception ex)
{
throw ex;
}
}
protected void ResponseFile(string fileName)
{
FileInfo fileInfo = new FileInfo(fileName);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "p_w_upload;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();
string tempPath = fileName.Substring(0, fileName.LastIndexOf("\\"));
// DelDir(tempPath);
Directory.Delete(tempPath,true);
Directory.CreateDirectory(tempPath);
addpathqx(tempPath,"Everyone","完全控制");
Response.End();
}
//给目录添加用户
public void addpathqx(string pathname, string username, string qx)
{
DirectoryInfo dirinfo = new DirectoryInfo(pathname);
if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)
{
dirinfo.Attributes = FileAttributes.Normal;
}
//取得访问控制列表
DirectorySecurity dirsecurity = dirinfo.GetAccessControl();
// string strDomain = Dns.GetHostName();
switch (qx)
{
case "完全控制":
dirsecurity.AddAcce***ule(new FileSystemAcce***ule("User_" + username, FileSystemRights.FullControl, AccessControlType.Allow));
break;
case "只读":
dirsecurity.AddAcce***ule(new FileSystemAcce***ule("User_" + username, FileSystemRights.Read, AccessControlType.Allow));
break;
case "写入":
dirsecurity.AddAcce***ule(new FileSystemAcce***ule("User_" + username, FileSystemRights.Write, AccessControlType.Allow));
break;
default:
dirsecurity.AddAcce***ule(new FileSystemAcce***ule("User_" + username, FileSystemRights.FullControl, AccessControlType.Deny));
break;
}
dirinfo.SetAccessControl(dirsecurity);
//AccessControlType.Allow允许访问受保护对象//Deny拒绝访问受保护对象
//FullControl、Read 和 Write 完全控制,读,写
//FileSystemRights.Write写入//Delete删除 //DeleteSubdirectoriesAndFiles删除文件夹和文件//ListDirectory读取
//Modify读写删除-修改//只读打开文件和复制//
}
转载于:https://blog.51cto.com/yaya123/1114940