小資料的匯出 最大隻能65536條
阿新 • • 發佈:2019-01-04
@SuppressWarnings("unused")
@RequestMapping(params = "excelExport")
public void excelExport(@RequestParam HashMap<String, Object> paramMap, HttpServletRequest request,HttpServletResponse response) {
response.setContentType("application/vnd.ms-excel");
String codedFileName = null;
OutputStream fOut = null;
HttpSession session = request.getSession();
try {
codedFileName = "使用者資訊";
// 根據瀏覽器進行轉碼,使其支援中文檔名
if (BrowserUtils.isIE(request)) {
response.setHeader("content-disposition","attachment;filename=" + java.net.URLEncoder.encode(codedFileName,"UTF-8") + ".xls");
} else {
String newtitle = new String(codedFileName.getBytes("UTF-8"),"ISO8859-1");
response.setHeader("content-disposition","attachment;filename=" + newtitle + ".xls");
}
// 產生工作簿物件
HSSFWorkbook workbook = null;
List<SysUser> dataList = sysUserService.getByCondition(paramMap);
workbook = exportExcel("使用者資訊", dataList);
fOut = response.getOutputStream();
workbook.write(fOut);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fOut.flush();
fOut.close();
} catch (IOException e) {
}
}
}
public HSSFWorkbook exportExcel(String sheetName, List<SysUser> dataList) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(sheetName);
CellStyle titleStyle = ExcelUtil.getTitleStyle(workbook);
HSSFRow row = sheet.createRow((int) 0);
row.setHeight((short) 900);
HSSFCell cell = null;
int i = 0;
cell = row.createCell(i++);
cell.setCellValue("真實姓名");
cell.setCellStyle(titleStyle);
cell = row.createCell(i++);
cell.setCellValue("使用者名稱");
cell.setCellStyle(titleStyle);
cell = row.createCell(i++);
cell.setCellValue("手機號");
cell.setCellStyle(titleStyle);
cell = row.createCell(i++);
cell.setCellValue("聯絡電話");
cell.setCellStyle(titleStyle);
cell = row.createCell(i++);
cell.setCellValue("郵箱地址");
cell.setCellStyle(titleStyle);
int b = 1;
for(SysUser data : dataList) {
row = sheet.createRow(b);
int a = 0;
row.createCell(a++).setCellValue(data.getRealName());
row.createCell(a++).setCellValue(data.getUserName());
row.createCell(a++).setCellValue(data.getPhone());
row.createCell(a++).setCellValue(data.getUserTel());
row.createCell(a++).setCellValue(data.getEmail());
b++;
}
for(int a = 0; a < i; a++){
sheet.autoSizeColumn(a);
}
return workbook;
}