导出EXCEL的 两个方法

Posted 街机玩家

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了导出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();
}
}
是一个接接口调用方法,创建步骤很明确,一步一步操作即可;



以上是关于导出EXCEL的 两个方法的主要内容,如果未能解决你的问题,请参考以下文章

java怎么实现导出excel

随手记怎么导出excel 导出excel的方法

将DataTable导出为Excel文件的方法

网页数据导出excel的方法

Oracle导出excel

java中使用jxl导出Excel表格详细通用步骤