程序如下
private void btnReport_Click(object sender, EventArgs e)
{
DataTable dt=es.GetBData() ;
string STRT="T_PMS_OilReport";
outputEC(dt,STRT);
}public void outputEC(DataTable dt,string strFN)
{
int rowIndex=4;
int colIndex=1; Excel.Application myExcel=new Excel.ApplicationClass();
Excel.Worksheet xSt=new Excel.WorksheetClass();
Excel.Workbook xBk=myExcel.Application.Workbooks.Add(true);
xSt=(Excel.Worksheet)xBk.ActiveSheet;
//
//取得标题
//
foreach(DataColumn col in dt.Columns)
{
colIndex++;
myExcel.Cells[4,colIndex] = col.ColumnName;
xSt.get_Range(myExcel.Cells[4,colIndex],myExcel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
} //
//取得表格中的数据
//
foreach(DataRow row in dt.Rows )
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dt.Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
myExcel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(myExcel.Cells[rowIndex,colIndex],myExcel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if(col.DataType == System.Type.GetType("System.String"))
{
myExcel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
xSt.get_Range(myExcel.Cells[rowIndex,colIndex],myExcel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
myExcel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
//
//加载一个合计行
//
int rowSum = rowIndex + 1;
int colSum = 2;
myExcel.Cells[rowSum,2] = "合计";
xSt.get_Range(myExcel.Cells[rowSum,2],myExcel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
myExcel.Visible=true;
//xSt.Export(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
// xBk.SaveCopyAs(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls");
xBk.SaveCopyAs(Server.MapPath(".")+"\\"+strFN+".xls");
dt = null;
xBk.Close(false, null,null);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
myExcel = null;
xSt = null;
GC.Collect();
// string path = Server.MapPath(this.xlfile.Text+".xls");
string path = Server.MapPath(strFN+".xls");
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行
Response.End();
}
运行时出错:System.UnauthorizedAccessException: 拒绝访问
是什么问题啊,急啊!
private void btnReport_Click(object sender, EventArgs e)
{
DataTable dt=es.GetBData() ;
string STRT="T_PMS_OilReport";
outputEC(dt,STRT);
}public void outputEC(DataTable dt,string strFN)
{
int rowIndex=4;
int colIndex=1; Excel.Application myExcel=new Excel.ApplicationClass();
Excel.Worksheet xSt=new Excel.WorksheetClass();
Excel.Workbook xBk=myExcel.Application.Workbooks.Add(true);
xSt=(Excel.Worksheet)xBk.ActiveSheet;
//
//取得标题
//
foreach(DataColumn col in dt.Columns)
{
colIndex++;
myExcel.Cells[4,colIndex] = col.ColumnName;
xSt.get_Range(myExcel.Cells[4,colIndex],myExcel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
} //
//取得表格中的数据
//
foreach(DataRow row in dt.Rows )
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dt.Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
myExcel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(myExcel.Cells[rowIndex,colIndex],myExcel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if(col.DataType == System.Type.GetType("System.String"))
{
myExcel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
xSt.get_Range(myExcel.Cells[rowIndex,colIndex],myExcel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
myExcel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
//
//加载一个合计行
//
int rowSum = rowIndex + 1;
int colSum = 2;
myExcel.Cells[rowSum,2] = "合计";
xSt.get_Range(myExcel.Cells[rowSum,2],myExcel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
myExcel.Visible=true;
//xSt.Export(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
// xBk.SaveCopyAs(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls");
xBk.SaveCopyAs(Server.MapPath(".")+"\\"+strFN+".xls");
dt = null;
xBk.Close(false, null,null);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
myExcel = null;
xSt = null;
GC.Collect();
// string path = Server.MapPath(this.xlfile.Text+".xls");
string path = Server.MapPath(strFN+".xls");
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行
Response.End();
}
运行时出错:System.UnauthorizedAccessException: 拒绝访问
是什么问题啊,急啊!
Excel.Application myExcel=new Excel.ApplicationClass();在web.config的<system.web> 加入模拟登陆<identity impersonate="true"/>