//导出当前页
function btn_ExPortPage() {
var $gridList = $("#gridList");
var page = $gridList.getGridParam('page'); // 获取jqGrid 的页数
var rows = $("#gridPager_center").find("select[role='listbox']").val(); // 获取每页行数 $gridList.getGridParam('rows'); // rows
//查询参数 并带上 jqGrid 参数
var obj=$("#form1").formSerialize();
obj.page=page;
obj.rows=rows;
//
//$.ajax({
// type:"POST",
// url: "/Fee/DeliveryFee/ExPortPage",
// data: obj,
// dataType: "json",
// async: false,
// //traditional:true,
// success: function (data) {
// var rows = data.rows;
// }
//});
//序列化参数
var params = $.param(obj);
//执行导出方法
document.location.href = "/Fee/DeliveryFee/ExPortPage?" + params;
}
后端代码 :
/// <summary>
/// 导出当前页
/// </summary>
/// <param name="pagination"></param>
/// <param name="request"></param>
public void ExPortPage(MvcPagination pagination, DeliveryFeeListRequest request)
{
var unit = OmsObject.CurrentUnit(Session);
var manager = new DeliveryFeeManager();
//分页
request.pi = pagination.page - 1;
request.pc = pagination.rows;
var dba = DBA.GetInstance("");
List<TDeliveryFee> list= manager.GetList(dba, unit.id, request, pagination);
dba.Close();
ExportExcel(list,"物流费用报表(部分)");
}
NPOI :
private static void ExportExcel(List<TDeliveryFee> dt, string fileName = "")
{
var book = new HSSFWorkbook();
if (fileName == "")
fileName = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now);
fileName = fileName.Trim();
string ext = Path.GetExtension(fileName);
if (ext.ToLower() == ".xls" || ext.ToLower() == ".xlsx")
fileName = fileName.Replace(ext, string.Empty);
var sheet = book.CreateSheet(fileName);
var rowIdx = 0;
var row = sheet.CreateRow(rowIdx++);
var colIdx = 0;
row.CreateCell(colIdx++).SetCellValue("销售单号");
row.CreateCell(colIdx++).SetCellValue("物流公司");
row.CreateCell(colIdx++).SetCellValue("运单号码");
row.CreateCell(colIdx++).SetCellValue("创建时间");
row.CreateCell(colIdx++).SetCellValue("毛重");
row.CreateCell(colIdx++).SetCellValue("物流公司费用");
row.CreateCell(colIdx++).SetCellValue("货主费用");
row.CreateCell(colIdx++).SetCellValue("使用状态");
foreach (TDeliveryFee tmp in dt)
{
row = sheet.CreateRow(rowIdx++);
colIdx = 0;
row.CreateCell(colIdx).SetCellValue(tmp.codec);//销售单号
colIdx += 1;
row.CreateCell(colIdx).SetCellValue(tmp.delivery_name);//物流公司
colIdx += 1;
row.CreateCell(colIdx).SetCellValue(tmp.express_no);//运单号码
colIdx += 1;
row.CreateCell(colIdx).SetCellValue(tmp.create_time.ToString());//创建时间
colIdx += 1;
row.CreateCell(colIdx).SetCellValue(tmp.weight_g_str);//毛重
colIdx += 1;
row.CreateCell(colIdx).SetCellValue(tmp.price_str);//物流公司费用
colIdx += 1;
row.CreateCell(colIdx).SetCellValue(tmp.price2_str);//货主费用
colIdx += 1;
row.CreateCell(colIdx).SetCellValue(tmp.status_name);//使用状态
colIdx += 1;
}
HttpResponse httpResponse = System.Web.HttpContext.Current.Response;
httpResponse.Clear();
httpResponse.Buffer = true;
httpResponse.Charset = Encoding.UTF8.BodyName;
httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
httpResponse.ContentEncoding = Encoding.UTF8;
httpResponse.ContentType = "application/vnd.ms-excel; charset=UTF-8";
book.Write(httpResponse.OutputStream);
httpResponse.End();
}