我已动态创建了一些图表,并希望使用closedxml将数据导出到Excel。 我在图例上创建了一个自定义项目,当我单击它时,页面回发并正确引发事件处理程序。但是我无法弹出保存窗口,因此无法保存excel文件。请帮帮我?
我在asp.NET Framework 4.5上使用C#。
这是我的代码的简化版本。
protected void Page_Load(object sender, EventArgs e)
{
Add_Chart();
}
public void Add_Chart()
{
System.Web.UI.DataVisualization.Charting.Chart Add_Chart1 = new System.Web.UI.DataVisualization.Charting.Chart();
//Create Chart here
//Add chart to htmltablecell
Add_Chart1.Click += Chart_Legend_Click;
}
protected void Chart_Button_Click(object sender, ImageMapEventArgs e)
{
string sheetname = "trial";
string filename = "trial.xlsx";
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add(sheetname);
HttpResponse httpResponse = Response;
httpResponse.Clear();
httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
httpResponse.AddHeader("content-disposition", "attachment;filename=" + filename);
//Create header and data rows here
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
workbook.SaveAs(memoryStream);
memoryStream.WriteTo(httpResponse.OutputStream);
memoryStream.Close();
}
httpResponse.End();
}