// 导出报表(商铺交易统计)
@RequestMapping("/downloadOrder/{page}")
public voiddownLoadOrder(@PathVariable intpage,
HttpServletRequestrequest,OrderBean orderBean, HttpServletResponse response)
throwsException {
if(page <= 0) {
page = 1;
}
PageUntilspageBean = tborderService.countTborderByPage(orderBean,page,
Const.PAGE_ROWS);
@SuppressWarnings("unchecked")
List<OrderBean>list = pageBean.getList(); // 得到数据库数据
// 第一步,创建一个webbook,对应一个Excel文件
Workbook wb = newHSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet =(HSSFSheet) wb.createSheet("sheet1");
// 第三步,在sheet中添加表头第0行,
HSSFRow row =sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头设置表头居中
HSSFCellStyle style =(HSSFCellStyle) wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
// 生成一个字体
HSSFFont font =(HSSFFont) wb.createFont();
font.setColor(HSSFColor.BLACK.index);//HSSFColor.VIOLET.index //字体颜色
font.setFontHeightInPoints((short)12);
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字体增粗
style.setFont(font);
// 创建格式
CellStyle cellStyle =wb.createCellStyle();
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setFont(font);
HSSFCell cell =row.createCell(0);
cell.setCellValue("订单号");
cell.setCellStyle(style);
sheet.setColumnWidth(0,4000);// 设置列的宽度
cell =row.createCell(1);
cell.setCellValue("用户名");
cell.setCellStyle(style);
sheet.setColumnWidth(1,4000);
cell =row.createCell(2);
cell.setCellValue("订单状态");
cell.setCellStyle(style);
sheet.setColumnWidth(2,4000);
cell =row.createCell(3);
cell.setCellValue("下单时间");
cell.setCellStyle(style);
sheet.setColumnWidth(3,4000);
cell =row.createCell(4);
cell.setCellValue("商品数量");
cell.setCellStyle(style);
sheet.setColumnWidth(4,4000);
for (int i =0; i < list.size(); i++) {
HSSFRow newrow =sheet.createRow((int) i + 1);
HSSFCell newcell =newrow.createCell(0);
newcell.setCellValue(list.get(i).getOcode());
newcell.setCellStyle(style);
sheet.setColumnWidth(0,4000);// 设置列的宽度
newcell =newrow.createCell(1);
newcell.setCellValue(list.get(i).getLoginName());
newcell.setCellStyle(style);
sheet.setColumnWidth(1,4000);// 设置列的宽度
newcell =newrow.createCell(2);
if(list.get(i).getState() == 1) {
newcell.setCellValue("正常订单");
} else if(list.get(i).getState() == 2) {
newcell.setCellValue("退款订单");
} else if(list.get(i).getState() == 3) {
newcell.setCellValue("换货订单");
} else if(list.get(i).getState() == 4) {
newcell.setCellValue("未确认订单");
} else {
newcell.setCellValue("作废订单");
}
newcell.setCellStyle(style);
sheet.setColumnWidth(2,4000);// 设置列的宽度
newcell =newrow.createCell(3);
DateFormat df3 =DateFormat.getDateInstance(DateFormat.MEDIUM);
newcell.setCellValue(df3.format(list.get(i).getOtime()));
newcell.setCellStyle(style);
sheet.setColumnWidth(3,4000);// 设置列的宽度
newcell =newrow.createCell(4);
newcell.setCellValue(list.get(i).getPsum());
newcell.setCellStyle(style);
sheet.setColumnWidth(4,4000);// 设置列的宽度
}
// 生成导出文件日期
Calendar c = Calendar.getInstance();
String time =c.get(Calendar.YEAR) + "_" +c.get(Calendar.MONTH) + "_"
+ c.get(Calendar.DATE) + "_" +c.get(Calendar.HOUR) + "_"
+ c.get(Calendar.MINUTE) + "_" +c.get(Calendar.SECOND);
// 生成文件名(以下涉及到文件的输入和输出流)
File excelFile = newFile("订单统计_" + time + "_" +page + ".xls");
FileOutputStream fos = newFileOutputStream(excelFile);
wb.write(fos);
fos.close();
// 读到流中
InputStream inStream = newFileInputStream(excelFile);
// 设置输出的格式
//httpServletResponse.reset();
response.setContentType("application/x-download;charset=utf-8");
response.setHeader("Content-Disposition", "filename="
+ newString(excelFile.toString().getBytes("gb2312"),
"iso8859-1"));// 设置文件头编码方式和文件名,解决文件名中文乱码
//httpServletResponse.addHeader("Content-Disposition",
//"attachment; filename=\"" + excelFile + "\"");
// 循环取出流中的数据
byte[] b= new byte[1024];
try {
OutputStream out =response.getOutputStream();
wb.write(out);
while(inStream.read(b) > 0) {
out.write(b);
}
inStream.close();
out.close();
excelFile.delete();
} catch(IOException ex) {
ex.printStackTrace();
}
}
jsp页面:
function download() {
if ('${pageBean.pageNo}' == null || '${pageBean.pageNo}' == 0) {
alert('没有要导出的数据');
return;
}
document.logaction.ocode.value = '${ocode}';
document.logaction.otime1.value = '${otime1}';
document.logaction.otime2.value = '${otime2}';
document.logaction.action = "admin/downloadOrder/${pageBean.pageNo}.do";
document.logaction.submit();
}
<form action="" method="post"id="logaction" name="logaction">
<table>
<input type="hidden" id="formpath" name="formpath"/>
<tr>
<td>
订单号: <input name="ocode" id="ocode"type="text">
<select name="source" id="source"onchange="changetype(this.value)">
<option value="0" selected="selected">--发布角色--</option>
<option value="1">平台</option>
<option value="2">供应商</option>
</select>
<input type="button"
value="搜索信息" class="inputbutton" key="third_relation"
onclick="check(this)">
</td>
<td>
<input name="otime1" id="otime1"type="text"
onClick="WdatePicker()" class="Wdate"> -<input
name="otime2" id="otime2"type="text" onClick="WdatePicker()"
class="Wdate">
<input type="button"
value="下单时间" class="inputbutton" key="third_relation"
onclick="check(this)"> <input
type="button"value="订单下载" class="inputbutton"
key="third_relation" onclick="download()">
</td>
<td>
<div class="cell" style="float: right;">总共购买:${count}个</div>
</td>
</tr>
</table>
</form>