controller类 @RequestMapping("/exportAllse") @ResponseBody public JsonResult exportAllses(HttpServletResponse response, HttpSession session) throws Throwable { AntiCheatingOutcryResultsQuery query = (AntiCheatingOutcryResultsQuery) session.getAttribute("query"); try { //poi导出 ServletOutputStream out = response.getOutputStream(); //设置文件名字 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String fileName = "excel标题" + sdf.format(new Date()); response.reset(); // 解决中文名称乱码 response.setContentType("application/vnd.ms-excel"); response.setHeader( "Content-Disposition", "attachment;filename=\""+ new String(fileName.getBytes( "gb2312" ), "ISO8859-1" )+ ".xls" + "\""); String[] titles = {"订单编号1", "
订单编号2
", "
订单编号3
","
订单编号4
","
订单编号5
","
订单编号6
","提交结果","
订单编号7
","
订单编号8
"}; //进行批次列表信息excel文件写入 // antiCheatingOutcryResultsServiceimpl.exportantiCheatingOutcryResults(titles, out, query); antiCheatingOutcryResultsServiceimpl.exportantiCheatingOutcryResultsceshi(titles, out, query); } catch (Exception e) { logger.error("导出列表失败!对象:" + "antiCheatingOutcryResults", e); return new JsonResult(Status.ERROR, "导出列表失败!"); } return new JsonResult(Status.OK, "导出列表成功!"); } }
service实现类
public void exportantiCheatingOutcryResultsceshi(String[] titles, ServletOutputStream out, AntiCheatingOutcryResultsQuery query) throws Throwable{
try{
// 第一步,创建一个workbook,对应一个Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
// ******************** 生成单元格样式 ********************
CellStyle styleOdd = creatStyle(workbook, 1);
CellStyle styleEven = creatStyle(workbook, 2);
List<AntiCheatingOutcryResultsVo> antiCheatingOutcryResultsVoList =antiCheatingOutcryResultsMapper.queryPageResultsLists(query);
/* List<Object> listx = new ArrayList<Object>();
// 限制个数
int pageSize=20;
// 页数
int page= antiCheatingOutcryResultsVoList.size() % pageSize == 0? antiCheatingOutcryResultsVoList.size() / pageSize : antiCheatingOutcryResultsVoList.size() /pageSize + 1;
List<List<AntiCheatingOutcryResultsVo>> result=new ArrayList<List<AntiCheatingOutcryResultsVo>>();
int remaider=antiCheatingOutcryResultsVoList.size()%page; //(先计算出余数)
int number=antiCheatingOutcryResultsVoList.size()/page; //然后是商
int offset=0;//偏移量
for(int i=0;i<page;i++){
List<AntiCheatingOutcryResultsVo> value=null;
if(remaider>0){
value=antiCheatingOutcryResultsVoList.subList(i*number+offset, (i+1)*number+offset+1);
remaider--;
offset++;
}else{
value=antiCheatingOutcryResultsVoList.subList(i*number+offset, (i+1)*number+offset);
}
listx.add(value);
}
*/
List<List<AntiCheatingOutcryResultsVo>> result=new ArrayList<List<AntiCheatingOutcryResultsVo>>();
// 限制个数
int pageSize=20;
int sourceSize = antiCheatingOutcryResultsVoList.size();
//页数
int size= antiCheatingOutcryResultsVoList.size() % pageSize == 0? antiCheatingOutcryResultsVoList.size() / pageSize : antiCheatingOutcryResultsVoList.size() /pageSize + 1;
for (int i = 0; i < size; i++) {
List<AntiCheatingOutcryResultsVo> listx1 = new ArrayList<AntiCheatingOutcryResultsVo>();
for (int j = i * pageSize; j < (i + 1) * pageSize; j++) {
if (j < sourceSize) {
listx1.add(antiCheatingOutcryResultsVoList.get(j));
}
}
result.add(listx1);
}
// 第五步,写入实体数据
// List<OrderInformationVo> users = orderMapper.exportAll(batchId);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
if( antiCheatingOutcryResultsVoList != null && ! antiCheatingOutcryResultsVoList.isEmpty()){
for (int i = 0; i < result.size(); i++) {//gaile
List<AntiCheatingOutcryResultsVo> lists= (List<AntiCheatingOutcryResultsVo>) result.get(i);//geile
HSSFSheet sheet = workbook.createSheet("sheet"+i);
CellStyle styleHeader = creatStyle(workbook, 0);
// 最后生成表头,并调整列宽
HSSFRow row = sheet.createRow(0);
for (int n = 0; n < titles.length; n++) {
setCell(row, n, styleHeader, titles[n]);
sheet.autoSizeColumn(n, true);
}
for (int k = 0; k < lists.size(); k++) {
// 表格(行)内容
HSSFRow hssfRow = sheet.createRow(k + 1);
AntiCheatingOutcryResultsVo antiCheatingOutcryResultsVo = lists.get(k);
// 表格(行)内容
String[] cellValues = getCellValuess(antiCheatingOutcryResultsVo);
// 奇、偶数行样式不一样
CellStyle style;
if ((hssfRow.getRowNum() & 1) == 1) {
style = styleOdd;
} else {
style = styleEven;
}
for (int j = 0; j < cellValues.length; j++) {
setCell(hssfRow, j, style, cellValues[j]);
}
}
}
}
// 第七步,将文件存到指定位置
FileSystemView fsv = FileSystemView.getFileSystemView();
try {
workbook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
try {
throw new Exception("导出信息失败!");
} catch (Exception e1) {
e1.printStackTrace();
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
/* * 设置单元格的值、样式 */ private void setCell(HSSFRow row, int column, CellStyle style, String cellValue) { HSSFCell cell = row.createCell(column); cell.setCellStyle(style); if (StringUtils.isBlank(cellValue)) { cellValue = ""; } // 去掉两端空格 cellValue = StringUtils.trim(cellValue); cell.setCellValue(cellValue); }
导出的内容
private String[] getCellValuess(AntiCheatingOutcryResultsVo antiCheatingOutcryResultsVo) { if (antiCheatingOutcryResultsVo.getReturnResponseStatusEnum()==null){ return new String[]{ antiCheatingOutcryResultsVo.getOrderId(), antiCheatingOutcryResultsVo.getCreateDatenew(), antiCheatingOutcryResultsVo.getMsg(), antiCheatingOutcryResultsVo.getCode(), antiCheatingOutcryResultsVo.getSendMobile(), antiCheatingOutcryResultsVo.getsProvince(), antiCheatingOutcryResultsVo.getOfferTitle() }; }else { return new String[]{ antiCheatingOutcryResultsVo.getOrderId(), antiCheatingOutcryResultsVo.getCreateDatenew(), antiCheatingOutcryResultsVo.getMsg(), antiCheatingOutcryResultsVo.getCode(), antiCheatingOutcryResultsVo.getSendMobile(), antiCheatingOutcryResultsVo.getReturnResponseStatusEnum().getCode(), antiCheatingOutcryResultsVo.getReturnResponseStatusEnum().getDesc(), antiCheatingOutcryResultsVo.getsProvince(), antiCheatingOutcryResultsVo.getOfferTitle() };} }