導出EXCEL的 兩個方法

方法一:

/**
* 導出參數模板設置,經過類似查找出來的數據
*/
private void handleExportToExcelByLikeQuery(Page page, HttpServletResponse response) {
try {
List<ExworkRecordExportVo> list = new ArrayList<>();
List<ExworksRecord> exworksRecordList = page.getList();
if (exworksRecordList != null) {
for (ExworksRecord e : exworksRecordList) {
ExworkRecordExportVo v = new ExworkRecordExportVo();
v.setOrderNo(e.getOrderNo());
v.setApplierNo(e.getApplierNo());
v.setApplierName(e.getApplierName());
v.setLeaderNo(e.getLeaderNo());
v.setLeaderName(e.getLeaderName());
v.setpInTime(e.getpInTime());
v.setpOutTime(e.getpOutTime());
switch (e.getStatus()) {
case "1":
v.setStatus("審覈中");
break;
case "2":
v.setStatus("已經過");
break;
case "3":
v.setStatus("駁回");
break;
case "4":
v.setStatus("廢棄");
break;
default:
v.setStatus("");
break;
}
v.setRemark(e.getRemark());
v.setFinishStatus("1".equals(e.getFinishStatus())?"未完成":"完成");
v.setCreateTime(e.getCreateTime());
list.add(v);
}
}
ExportParams params = new ExportParams("申請列表", "申請列表", ExcelType.HSSF);
Workbook workbook = ExcelExportUtil.exportExcel(params, ExworkRecordExportVo.class, list);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("申請列表--" + dateFormat.format(new Date()) + "" + ".xls", "UTF-8"));// 組裝附件名稱和格式
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

主要用的三個jar包
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
ExcelExportUtil.exportExcel(params, ExworkRecordExportVo.class, list); 
主要參數: 1:ExportParams 、2:實體類 ExworkRecordExportVo 3:實體類對應的list
在封裝一下excel的名字和其餘參數便可

方法二:
public void exportConferenceEnrolChangeList(@RequestParam(required = true, value = "enrolId") Integer enrolId,
@RequestParam(required = true, value = "accessToken") String accessToken, HttpServletResponse response) throws IOException {
logger.info("*****調用 ConferenceEnrolChangeController 類 - exportConferenceEnrolChangeList 方法,參數:enrolId:" + enrolId + ", accessToken:" + accessToken);
Assert.assertNotEmpty(accessToken, "accessToken");
Assert.assertIntNotEmpty(enrolId, "enrolId");
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("報名名單配置變動記錄.xls", "UTF-8"));

// 第一步,建立一個webbook,對應一個Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("變動記錄");
// 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,建立單元格,並設置值表頭 設置表頭居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個居中格式

HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("操做人");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("mip");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("單位");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("部門");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("變動操做");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("變動內容");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("變動緣由");
cell.setCellStyle(style);

// 第五步,寫入實體數據 service查詢數據庫獲得的list
List<ConferenceEnrolChangeRecord> list = conferenceEnrolChangeService.getConferenceEnrolChangeListForExport(enrolId);

for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 1);
ConferenceEnrolChangeRecord record = list.get(i);
// 第六步,建立單元格,並設置值
row.createCell((short) 0).setCellValue(record.getOperationName());
row.createCell((short) 1).setCellValue(record.getMipNo());
row.createCell((short) 2).setCellValue(record.getCompany());
row.createCell((short) 3).setCellValue(record.getDepartment());
row.createCell((short) 4).setCellValue(record.getChangeOperation());
row.createCell((short) 5).setCellValue(record.getChangeContent());
row.createCell((short) 6).setCellValue(record.getChangeReason());
}

try {
ServletOutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
是一個接接口調用方法,建立步驟很明確,一步一步操做便可;
相關文章
相關標籤/搜索