将代码移动到类会导致HttpException错误
本文关键字:HttpException 错误 代码移动 | 更新日期: 2023-09-27 18:27:05
我已经编写了使用EPPlus将数据从网页导出到excel的代码,我想在整个项目中多次重用这些代码,所以我已经将代码移到了它自己的类中。
在页面上的代码中,它可以很好地工作,但当我将它移到自己的类中时,我在Response.Clear()
行上得到以下错误:
System.Web.HttpException (0x80004005): Response is not available in this context
这是我的代码
ExcelHelper类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using OfficeOpenXml;
using OfficeOpenXml.Table;
namespace myClassLib
{
public class ExcelHelper : System.Web.UI.Page
{
public void exportExcel(DataTable myDataTable)
{
Response.Clear();
Response.Charset = "";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=Output.xlsx");
DataTable dt= myDataTable;
ExcelPackage pck = new ExcelPackage();
using(pck)
{
ExcelWorksheet wsDt = pck.Workbook.Worksheets.Add("Sheet1");
wsDt.Cells["A1"].LoadFromDataTable(dt, true, TableStyles.None);
int colNumber = 1;
foreach (DataColumn col in dt.Columns)
{
if (col.DataType == typeof(DateTime))
{
wsDt.Column(colNumber).Style.Numberformat.Format = "yyyy-mm-dd";
}
colNumber = colNumber + 1;
}
wsDt.Cells[wsDt.Dimension.Address].AutoFitColumns();
Response.BinaryWrite(pck.GetAsByteArray());
}
Response.Flush();
Response.End();
}
}
}
调用类的代码
ExcelHelper myExcelHelper = new ExcelHelper();
myExcelHelper.exportExcel(myDataTable);
非常感谢您的任何帮助
您从Page
继承了帮助程序,可能是因为您希望访问Response
属性中提供的HttpResponse
实例。但是,Page
类应该由ASP.NET http管道实例化,以便正确设置其所有内部状态。在你的场景中不会发生这种情况。如果您按照如下方式重构它,您可以在一个助手类中生成Excel,并将一些额外的好东西导出到文件中。
public class ExcelExporter
{
DataTable dt;
string filename = "Output.xlsx";
// create exporter for datatable
public ExcelExporter(DataTable dt)
{
this.dt = dt;
}
// creates an exporter for a datatable and filename
public ExcelExporter(DataTable dt, string filename):this(dt)
{
this.filename = filename;
}
// exports excel to given httpResponse
public void Export(HttpResponse response)
{
Response.Clear();
Response.Charset = "";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader(
"content-disposition",
String.Format("attachment;filename={0}",filename));
Response.BinaryWrite(Export());
Response.Flush();
Response.End();
}
// export excel package in path with defaukt name
public void Export(string path)
{
Export(path, filename);
}
// export excel package as file in path
public void Export(string path, string file)
{
var full = Path.Combine(path, file);
File.WriteAllBytes(full, Export());
}
//exort Excel package as Byte array
public byte[] Export()
{
ExcelPackage pck = new ExcelPackage();
using (pck)
{
ExcelWorksheet wsDt = pck.Workbook.Worksheets.Add("Sheet1");
wsDt.Cells["A1"].LoadFromDataTable(dt, true, TableStyles.None);
int colNumber = 1;
foreach (DataColumn col in dt.Columns)
{
if (col.DataType == typeof(DateTime))
{
wsDt.Column(colNumber).Style.Numberformat.Format = "yyyy-mm-dd";
}
colNumber = colNumber + 1;
}
wsDt.Cells[wsDt.Dimension.Address].AutoFitColumns();
return pck.GetAsByteArray();
}
}
}
要使用这个新版本,您将在ASP.NET页面上如下调用它:
myExcelHelper = new ExcelExproter(myDataTable);
myExcelHelper.Export(Response); // Response will be the HttpResponse of the current executing page