第一步前端代码:文件流方式导出EXcle不能用Ajax方式,要用 window.location.href 的方式
function HasReferToExcel() {
if (confirm("系一次最多导出10000笔数据,是否继续?")) {
window.location.href = "/ashx/Refer.ashx"
}
}
第二步后台的代码:我用的是ashx来写的
public void HasReferToExcel(HttpContext context)
{
try
{
IList
int iTotalCount = 0;
rfs = new BLL.Refer(context).HasReferToExcel(1, 10000, sQuery, 1, out iTotalCount);
System.Text.StringBuilder sHtml = new System.Text.StringBuilder();
sHtml.Append("
编号 | 初评编号 | 估价员 | 业务员 | 中介银行 | 回复日期 | 物业地址 | 楼盘名称 | 楼栋 | 房号 | 建筑面积 | 套内面积 | 类型 | 朝向 | 景观 | 竣工日期 | 所在楼层 | 总楼层 | 电梯 | 装修 | 户型 | 户型结构 | 预估单价 | 预估总值 | 其他说明 | |
" + inspectionTasks[i].TaskId + " | " + inspectionTasks[i].PreReportNo + " | " + rfs[i].Judger + " | " + rfs[i].Inputer + " | " + rfs[i].BankName + " | " + rfs[i].ReplyTime + " | " + rfs[i].Address + " | " + rfs[i].ProjectName + " | " + rfs[i].BuildName + " | " + rfs[i].HouseName + " | " + rfs[i].BuildSize + " | " + rfs[i].InBuildSize + " | " + rfs[i].PropertyType + " | " + rfs[i].FaceTo + " | " + rfs[i].YtCount + " | " + rfs[i].OverDate + " | " + rfs[i].AtLayer + " | " + rfs[i].TotalLayer + " | " + rfs[i].HasDianTi + " | " + rfs[i].Zhuangxiu + " | " + rfs[i].Hxjg + " | " + rfs[i].MgtStatus + " | " + rfs[i].JudgePrice + " | " + rfs[i].TotalPrice + " | " + rfs[i].JudgerText + " |
}
第三步是创建Excle文件流
///
//设置下载的Excel文件名
context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(excelName, System.Text.Encoding.UTF8).ToString());
//Clear方法删除所有缓存中的HTML输出。但此方法只删除Response显示输入信息,不删除
context.Response.Clear();
#endregion
// 写入到客户端
byte[] array = System.Text.Encoding.GetEncoding("gb2312").GetBytes(sHtmlTableStr);
context.Response.BinaryWrite(array);
context.Response.End();
}
第二种方法
private static void ReferExcel(string sQuery, string name, HSSFWorkbook wb)
{
//设置导出到Excel时的格式
ISheet sheet = wb.CreateSheet(name);
//自动换行
ICellStyle cellStyle1 = wb.CreateCellStyle();
cellStyle1.WrapText = true;
ICellStyle cellStyle2 = wb.CreateCellStyle();
cellStyle2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
cellStyle2.VerticalAlignment = VerticalAlignment.CENTER;//垂直居中
string sql = " select top 5000 物业名称=a.proname+a.sBuild+a.sRoom,建筑面积=a.BuildSize,登记价=a.PriceRight,估价单价=a.JudgePrice,价格范围=a.JudgePrice_1,业务员=case when a.queryEmp='''' then c.name else queryEmp end,估价师=case when a.judger=0 then a.empname else d.name end,评估总值=a.TotalPrices,咨询日期=a.inputDate,银行名称=a.BankName,支行=a.SubBankName from ((tzc_refer_1 a left join mrbaseinf c on a.inputer=c.EmpID) left join mrbaseinf d on d.empid=a.judger) left join wmControversy e on e.Coopid=a.id left join tzc_bankbase bk on bk.id=a.bankid where "+sQuery+"";
DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.connstr, sql);
//循环出查出的数据
for (int i = 0; i < dt.Rows.Count + 1; i++)
{
IRow row = sheet.CreateRow(i);
for (int j = 0; j < dt.Columns.Count; j++)
{
//当i=0时,就循环出表头的数据名称
if (i == 0)
{
ICell cell = row.CreateCell(j);
cell.SetCellValue(dt.Columns[j].ColumnName);
}
else
{
//当i大于0时,循环出第一行的数据
ICell cell = row.CreateCell(j);
double dTemp = 0;
//判断是不是浮点型的数据
if (double.TryParse(dt.Rows[i - 1][j].ToString(), out dTemp))
{
cell.SetCellValue(dTemp);
}
else
{
cell.SetCellValue(dt.Rows[i - 1][j].ToString());
}
}
}
}
}