日期:2014-05-18  浏览次数:20484 次

(特急,在线等!!)郁闷了一天(高手帮忙)ASP.NET创建Excel时出错,不能访问只读文件xx.xls
代码是这样的:

private   void   Button1_Click(object   sender,   System.EventArgs   e)
{
this._x   =   new   Excel.ApplicationClass();
this._x.UserControl   =   false;

/*for   (int   i   =   0   ;i   <   4;   i++)
{        
this.SaveToXls( "D:\\test\\ "   +   i   +   ".xls ");     //   本例是在D盘下建立的test文件夹
}*/
//this.SaveToXls( "D:\\test\\0.xls ");
this.SaveToXls();


this._x.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject((object)   this._x);
System.GC.Collect();
}
private   void   SaveToXls()
{
Excel.WorkbookClass   wb   =   (Excel.WorkbookClass)this._x.Workbooks.Add(System.Reflection.Missing.Value);
Excel.Range   er   =   _x.get_Range( "A1:A1 ", "D4:D4 ");
er.Borders.LineStyle   =   1;
er.Borders.ColorIndex   =   10;
er.Font.Bold   =   true;
er.Interior.ColorIndex=6;   //设置Range的背景色


this._x.Cells[1,1]   =   "aa ";
//合并单元格
_x.get_Range( "A1:D1 ", "A1:D1 ").MergeCells   =   4;  

//单元格背景颜色
_x.get_Range( "A1:D1 ", "A1:D1 ").Interior.ColorIndex   =   4;

//_x.get_Range( "A1:D1 ", "A1:D1 ").Font.ColorIndex   =   104;
//字体大小
_x.get_Range( "A1:D1 ", "A1:D1 ").Font.Size   =   20;
Excel.Range   rng3=_x.get_Range( "D2:D2 ", "D5:D5 ");

//设置自动拉伸
rng3.EntireRow.AutoFit();

//设置列宽
rng3.ColumnWidth   =   70;
Excel.Range   rng1   =   (Excel.Range)_x.Cells[1,     1];
rng1.ColumnWidth   =   50;

//设置对齐方式
rng1.HorizontalAlignment   =     Excel.XlHAlign.xlHAlignCenter;


#region 两种选择方式
/*选择某个单元格*/
Excel.Range   rng4   =     (Excel.Range)_x.Cells[3,2];
rng4.HorizontalAlignment   =     Excel.XlHAlign.xlHAlignRight;

/*选择某块区域*/
Excel.Range     rngTest   =   _x.get_Range(_x.Cells[2,2],_x.Cells[5,5]);
rngTest.Font.Bold   =   true;
#endregion

for(int   i   =   2;i   <=   5;   i++)
{
this._x.Cells[i,1]=i.ToString()   +   "+8i ";
this._x.Cells[i,2]= " 'bbb2 ";
this._x.Cells[i,3]= " 'ccc3 ";
this._x.Cells[i,4]= " 'aaa4aaa4aaa4aaa4aaa4aaa4aaa4 ";
}      
wb.Saved   =   true;
//this._x.ActiveWorkbook.SaveCopyAs(filename);
//this._x.ActiveWorkbook.SaveCopyAs( "d:\\test\\aa.xls ");
this._x.ActiveWorkbook.SaveCopyAs(Server.MapPath(Request.ApplicationPath)   +   "\\test\\ "   +   "myexxx.xls ");
}


运行后出错信息:

不能访问只读文件“myexxx.xls”。  
说明:   执行当前   Web   请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。  

异常详细信息:   System.Runtime.InteropServices.COMException:   不能访问只读文件“myexxx.xls”。

源错误: