List<Map<String, String>> queryPackageProductList = service.queryPackageProductList(ppForm.getProductcode(),
ppForm.getSubproductcode());
if (null != queryPackageProductList && queryPackageProductList.size() > 0)
{
String xlsName = "CombineproductList.xls";
response.setHeader("Content-Disposition", "attachment;filename=" + xlsName);
writeExcel(request , queryPackageProductList, response.getOutputStream());
}
private void writeExcel(HttpServletRequest request,
List<Map<String, String>> packageProductList,
ServletOutputStream outputStream)
{
int count = packageProductList.size();
int rowprepage = 50000;
int page = (count % rowprepage == 0) ? (count / rowprepage)
: (count / rowprepage + 1);
WritableWorkbook wwb = null;
try
{
wwb = Workbook.createWorkbook(outputStream);
WritableSheet ws = null;
if (page == 0)
{
ws = wwb.createSheet("Sheet" + 1, 0);
}
for (int p = 1; p <= page; p++)
{
ws = wwb.createSheet("Sheet" + p, 0);
//生成标题行
buildTitle(request, ws);
buildData(packageProductList, count, rowprepage, ws, p);
}
wwb.write();
}
catch (Exception e)
{
log.error(e);
}
finally
{
if (null != wwb)
{
try
{
wwb.close();
}
catch (Exception e)
{
log.error(e);
}
}
}
}
private void buildData(List<Map<String, String>> packageProductList,
int count,
int rowprepage,
WritableSheet ws,
int p) throws RowsExceededException, WriteException
{
for (int i = (p - 1) * rowprepage; i < p * rowprepage; i++)
{
Map<String, String> packageProduct = packageProductList.get(i);
int LNo = 0;
ws.addCell(new Label(LNo++, i + 1, packageProduct.get("code")));
ws.addCell(new Label(LNo++, i + 1, packageProduct.get("name")));
ws.addCell(new Label(LNo++, i + 1, packageProduct.get("age")));
if (i + 1 >= count)
{
break;
}
}
}
private void buildTitle(HttpServletRequest request, WritableSheet ws) throws RowsExceededException, WriteException
{
int colNo = 0;
ws.addCell(new Label(colNo++,
0,
CmsServletAction.messageRsc.getMessage(request.getLocale(),
"编号",
"")));
ws.addCell(new Label(colNo++,
0,
CmsServletAction.messageRsc.getMessage(request.getLocale(),
"名称",
"")));
ws.addCell(new Label(colNo++,
0,
CmsServletAction.messageRsc.getMessage(request.getLocale(),
"个数",
"")));
}