导出word时,项目文件夹里的临时word文件老是为只读的,导致我无法写入

WUSTAR 2008-03-05 03:50:35
我已经把所有的权限都给上了,每次用的时候都把只读的钩去掉,而且昨天还成功导出过,我把临时word新建在项目文件夹里但并没有加到项目中,但今天一开机它就自动加入到项目中了,即使我删了重新用昨天的办法也没用,只要我一运行写入word的代码时,它就变只读的了,估计是程序的问题,我把代码贴出来,大家帮忙看看,谢谢罗


protected void LinkButton1_Click(object sender, EventArgs e)
{
//try
//{
string id = Session["caseinfoId"].ToString();
//从数据库中取值
string sql = @"select a.dsrname as dsr1,a.xb as xb_1,a.sfcode as code1,a.hjdz as hjdz1,a.jzdz as xjd1,
b.dsrname as dsr2,b.xb as xb_2,b.sfcode as code2,b.hjdz as hjdz2,b.jzdz as xjd2,caseinfo.jbr as jbr,
caseinfo.jbrdz as jbrdz,caseinfo.jbrtel as jbrtel,caseinfo.aqms as aqjj,caseinfo.cbnbyj as nbyj,caseinfo.ajly as ajly,caseinfo.ajqtly as ajqtly,
caseinfo.cbry as cbr,caseinfo.Lashyj as shyj,caseinfo.Lashfzr as bmfzr,caseinfo.Jaspyj as spyj,caseinfo.Jaspfzr as jgfzr,caseinfo.Lanf as nf,caseinfo.Labh as bh
from (select * from casedsr where dsrbh=1 ) as a inner join caseinfo on caseinfo.id=a.ajbh inner join (select * from casedsr where dsrbh=2 )as b on caseinfo.id=b.ajbh where caseinfo.id=" + id;
DataSet ds = sqlHelper.ExecuteDataset(sqlHelper.ConnectionStringLocalTransaction, sql);
DataRow dr = ds.Tables[0].Rows[0];
string path = Server.MapPath("~/doc/ladj.doc");
string outpath = Server.MapPath("~/doc/ls/s.doc");
GetDocByTemplet(path, outpath, dr, true);

System.Threading.Thread.Sleep(100); //给系统足够的时间,让doc能顺利转换调用它的线程

downLoad("doc/ls/s.doc");

//}
//catch
//{
//对word导出失败的提示
//RegisterStartupScript("", "<Script Language=JavaScript>alert('word导出失败!')</Script>");
//}
}


private void GetDocByTemplet(string TempletPathName, string OutPathName, DataRow Row, bool IgnoreBookMarkMissing) //word处理函数 string OutPathName,
{
#region
if (!File.Exists(TempletPathName))
throw new Exception("模板文件不存在");
//tsPath.checkDirectory(tsPath.ExtractFilePath(OutPathName));
//复制模板文件覆盖目标文件
if (File.Exists(OutPathName))
{
File.Delete(OutPathName);
}
File.Copy(TempletPathName, OutPathName, true);
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
try
{
object oOutPath = TempletPathName;
oDoc = oWord.Documents.Add(ref oOutPath, ref oMissing, ref oMissing, ref oMissing); //将模板作为一对象进行修改
foreach (Microsoft.Office.Interop.Word.Bookmark bookMark in oDoc.Bookmarks)
{
if (Row.Table.Columns.IndexOf(bookMark.Name) >= 0)
{
if (bookMark.Name.Split(new char[] { '_' })[0] == "xb")
{
if (Row[bookMark.Name].ToString().Trim() == "1") // 性别的判断
{
bookMark.Range.Text = "男";
}
else
{
bookMark.Range.Text = "女";
}
}
else
{
bookMark.Range.Text = Row[bookMark.Name].ToString().Trim();
}
}
else if (bookMark.Name.Split(new char[] { '_' })[0] == "ajly" && bookMark.Name.Split(new char[] { '_' })[1] == Row["ajly"].ToString())
{
bookMark.Range.Text = "√";
}
else if (!IgnoreBookMarkMissing)
{
throw new Exception("不存在的数据:" + bookMark.Name);
}
}
//将修改结果保存在用户选择的路径下
//oDoc.Save();
object p = OutPathName;
//将修改的结果另存为
oDoc.SaveAs(ref p, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}

finally
{
object o = false;
oWord.Application.Quit(ref o, ref oMissing, ref oMissing);
}
#endregion
}


private void downLoad(string loadPath)
{
string FullFileName = loadPath;

FileInfo DownloadFile = new FileInfo(HostingEnvironment.ApplicationPhysicalPath + FullFileName); // 需要转换为绝对路径,否则会自动认到C盘系统里那个IIS目录下面去,而且,无法通过URI的方式来进行数据流读取。如果你生成的文件不在web目录下,也需要明确指出。

// 下面到就是读取文件,通过数据流的方式下载了。

Response.Clear();

Response.ClearHeaders();

Response.Buffer = false;

Response.ContentType = "application/octet-stream";

Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FullFileName, System.Text.Encoding.UTF8));

Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

Response.WriteFile(DownloadFile.FullName);

Response.Flush();

Response.End();
}

...全文
741 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
WUSTAR 2008-03-05
  • 打赏
  • 举报
回复
自己顶一下

62,266

社区成员

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

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

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

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