第一步前端代码:文件流方式导出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("
");"); ");
//sHtml.Append("编号 ");
//sHtml.Append("初评编号 ");
sHtml.Append("估价员 ");
sHtml.Append("业务员 ");
sHtml.Append("中介银行 ");
sHtml.Append("回复日期 ");
sHtml.Append("物业地址 ");
sHtml.Append("楼盘名称 ");
sHtml.Append("楼栋 ");
sHtml.Append("房号 ");
sHtml.Append("建筑面积 ");
sHtml.Append("套内面积 ");
sHtml.Append("类型 ");
sHtml.Append("朝向 ");
sHtml.Append("景观 ");
sHtml.Append("竣工日期 ");
sHtml.Append("所在楼层 ");
sHtml.Append("总楼层 ");
sHtml.Append("电梯 ");
sHtml.Append("装修 ");
sHtml.Append("户型 ");
sHtml.Append("户型结构 ");
sHtml.Append("预估单价 ");
sHtml.Append("预估总值 ");
sHtml.Append("其他说明
for (int i = 0; i < rfs.Count; i++)
{
sHtml.Append("");
//sHtml.Append("" + inspectionTasks[i].TaskId + " ");
//sHtml.Append("" + inspectionTasks[i].PreReportNo + " ");//初评编号
sHtml.Append("" + rfs[i].Judger + " ");//估价员
sHtml.Append("" + rfs[i].Inputer + " "); //业务员
sHtml.Append("" + rfs[i].BankName + " ");//中介银行
sHtml.Append("" + rfs[i].ReplyTime + " "); //回复日期
sHtml.Append("" + rfs[i].Address + " "); //物业地址
sHtml.Append("" + rfs[i].ProjectName + " ");//楼盘名称
sHtml.Append("" + rfs[i].BuildName + " ");//楼栋
sHtml.Append("" + rfs[i].HouseName + " ");//房号
sHtml.Append("" + rfs[i].BuildSize + " ");//建筑面积
sHtml.Append("" + rfs[i].InBuildSize + " ");//套内面积
sHtml.Append("" + rfs[i].PropertyType + " ");//类型
sHtml.Append("" + rfs[i].FaceTo + " ");//朝向
sHtml.Append("" + rfs[i].YtCount + " ");//景观
sHtml.Append("" + rfs[i].OverDate + " ");//竣工日期
sHtml.Append("" + rfs[i].AtLayer + " ");//所在楼层
sHtml.Append("" + rfs[i].TotalLayer + " ");//总楼层
sHtml.Append("" + rfs[i].HasDianTi + " ");//电梯
sHtml.Append("" + rfs[i].Zhuangxiu + " ");//装修
sHtml.Append("" + rfs[i].Hxjg + " ");//户型
sHtml.Append("" + rfs[i].MgtStatus + " ");//户型结构
sHtml.Append("" + rfs[i].JudgePrice + " ");//预估单价
//sHtml.Append("");
sHtml.Append("" + rfs[i].TotalPrice + " ");//预估总值
sHtml.Append("" + rfs[i].JudgerText + " ");//其他说明
}
sHtml.Append("
BLL.Common.ExcelFile(context, sHtml.ToString(), "的物业" + DateTime.Today.ToShortDateString() + ".xls");
}
catch
{
}
}
第三步是创建Excle文件流
///
/// 创建Excel文件流
///
///
///
///
public static void ExcelFile(HttpContext context, string sHtmlTableStr, string excelName)
{
#region 设置响应头(文件名和文件格式)
//设置响应的类型为Excel
context.Response.Charset = "gb2312"; //必须写,否则会有乱码
context.Response.ContentEncoding = System.Text.Encoding.UTF8; //必须写,否则会有乱码
context.Response.ContentType = "application/vnd.ms-excel";
//设置下载的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());
}
}
}
}
}