急切请教一个问题 我是用NPOI组件 用来进行EXCEL导出操作 代码如下
问题: 我如果直接使用ASP.NET按钮控件调用 OnExcelOut 可以弹出导出的Excel文件没问题
问题是在我使用 JS的Ajax调用 OnExcelOut 方法的时候 就没有反映了。 着急中有大师帮忙解决一下吗?
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Reflection; using System.IO; using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; using NPOI.HPSF; namespace TRAIL.DbSalary.Example.View.Ajax { public partial class ExcelOutPut : System.Web.UI.Page { HSSFWorkbook hssfworkbook; protected void Page_Load(object sender, EventArgs e) { OnExcelOut(); } //主要就是这部分代码 public void OnExcelOut() {
//*********************************** string filename = "test.xls";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
Response.Clear();
InitializeWorkbook();
GenerateData();
Response.BinaryWrite(WriteToStream().GetBuffer());
Response.End();
//*********************************** } private MemoryStream WriteToStream() { //Write the stream data of workbook to the root directory MemoryStream file = new MemoryStream(); hssfworkbook.Write(file); return file; } void GenerateData() { ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1"); sheet1.CreateRow(0).CreateCell(0).SetCellValue("This is a Sample"); int x = 1; for (int i = 1; i <= 15; i++) { IRow row = sheet1.CreateRow(i); for (int j = 0; j < 15; j++) { row.CreateCell(j).SetCellValue(x++); } } } void InitializeWorkbook() { hssfworkbook = new HSSFWorkbook(); ////create a entry of DocumentSummaryInformation DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "NPOI Team"; hssfworkbook.DocumentSummaryInformation = dsi; ////create a entry of SummaryInformation SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Subject = "NPOI SDK Example"; hssfworkbook.SummaryInformation = si; } } }
//主要就是这部分代码
hssfworkbook .Write(Response.OutputStream);
Response.Write(Response.OutputStream);
Response.Clear();
InitializeWorkbook();
GenerateData();
Response.BinaryWrite(WriteToStream().GetBuffer());
Response.End();
(这部分替换掉)看行不。。。
@冉ran:
hssfworkbook .Write(Response.OutputStream);
感谢你的回答 不过这部分是NPOI的代码 还不能随便换掉。
不能用ajax做下载,用location.href=xxx就行了
但是我有一个源码是可以进行调用的 也能正常进行弹出下载提示的。
@_大师兄_: 贴一下 你的源码 看看
用ajax 应该是没触发到 Page_Load 事件,你跟断点看看..
最好还是断点吧
不知前端代码是怎么样的?