码迷,mamicode.com
首页 > 其他好文 > 详细

导出EXCEL的 两个方法

时间:2017-11-01 18:55:13      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:encode   export   exp   单位   opera   enc   require   format   reason   

方法一:

/**
* 导出参数模板设置,通过相似查找出来的数据
*/
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的 两个方法

标签:encode   export   exp   单位   opera   enc   require   format   reason   

原文地址:http://www.cnblogs.com/fuqiang-terry/p/7767755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!