@RequestMapping(value = "/importexcel.htm", method = RequestMethod.GET)
public ModelAndView _importExcel(HttpServletRequest request, HttpServletResponse response, Integer cId) throws Exception {
List<Map<String, Object>> list = d_ExchangeCodeService.selectExcelRecord(cId);
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet(list.get(0).get("NAME").toString());
sheet.setColumnWidth((short) 0, (short) (35.7 * 150));
sheet.setColumnWidth((short) 1, (short) (35.7 * 150));
sheet.setColumnWidth((short) 2, (short) (35.7 * 150));
sheet.setColumnWidth((short) 3, (short) (35.7 * 100));
sheet.setColumnWidth((short) 4, (short) (35.7 * 250));
sheet.setColumnWidth((short) 5, (short) (35.7 * 150));
sheet.setColumnWidth((short) 6, (short) (35.7 * 150));
Row row = sheet.createRow((short) 0);
CellStyle cs = wb.createCellStyle();
CellStyle cs2 = wb.createCellStyle();
Font f = wb.createFont();
Font f2 = wb.createFont();
f.setFontHeightInPoints((short) 10);
f.setColor(IndexedColors.RED.getIndex());
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
f2.setFontHeightInPoints((short) 10);
f2.setColor(IndexedColors.BLACK.getIndex());
f2.setBoldweight(Font.BOLDWEIGHT_BOLD);
cs.setFont(f);
cs.setBorderLeft(CellStyle.BORDER_THIN);
cs.setBorderRight(CellStyle.BORDER_THIN);
cs.setBorderTop(CellStyle.BORDER_THIN);
cs.setBorderBottom(CellStyle.BORDER_THIN);
cs2.setFont(f2);
cs2.setBorderLeft(CellStyle.BORDER_THIN);
cs2.setBorderRight(CellStyle.BORDER_THIN);
cs2.setBorderTop(CellStyle.BORDER_THIN);
cs2.setBorderBottom(CellStyle.BORDER_THIN);
Cell cell = row.createCell(0);
cell.setCellValue("用户名");
cell.setCellStyle(cs);
cell = row.createCell(1);
cell.setCellValue("订单号");
cell.setCellStyle(cs);
cell = row.createCell(2);
cell.setCellValue("兑换券序列号");
cell.setCellStyle(cs);
cell = row.createCell(3);
cell.setCellValue("兑换券金额");
cell.setCellStyle(cs);
cell = row.createCell(4);
cell.setCellValue("兑换券类型名称");
cell.setCellStyle(cs);
cell = row.createCell(5);
cell.setCellValue("使用时间");
cell.setCellStyle(cs);
cell = row.createCell(6);
cell.setCellValue("使用结束日期");
cell.setCellStyle(cs);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
for (short i = 0; i < list.size(); i++) {
row = sheet.createRow((short) i + 1);
cell = row.createCell(0);
cell.setCellValue(list.get(i).get("usr_UserID") == null ? "未使用" : list.get(i).get("usr_UserID").toString());
cell.setCellStyle(cs2);
cell = row.createCell(1);
cell.setCellValue(list.get(i).get("ord_OrderID") == null ? "未使用" : list.get(i).get("ord_OrderID").toString());
cell.setCellStyle(cs2);
cell = row.createCell(2);
cell.setCellValue(list.get(i).get("Account").toString());
cell.setCellStyle(cs2);
cell = row.createCell(3);
cell.setCellValue(Double.parseDouble(list.get(i).get("Amount").toString()));
cell.setCellStyle(cs2);
cell = row.createCell(4);
cell.setCellValue(list.get(i).get("NAME").toString());
cell.setCellStyle(cs2);
cell = row.createCell(5);
cell.setCellValue(list.get(i).get("UsedTime") == null ? "未使用" : df.format(list.get(i).get("UsedTime")).toString());
cell.setCellStyle(cs2);
cell = row.createCell(6);
cell.setCellValue(df.format(list.get(i).get("BlankOutTime")).toString());
cell.setCellStyle(cs2);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((list.get(0).get("NAME").toString() + ".xls").getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
return null;
}