poi导出excel
2017-09-07 15:33
18 查看
1、pom.xml文件中添加依赖
2、实体类(用到了注解)
3、查询数据,并调用导出的方法
4、工具类
5、前端页面调用 要使用window.location.href 不能使用ajax调用
<!--EasyPOI--> <dependency> <groupId>org.jeecg</groupId> <artifactId>easypoi-base</artifactId> <version>2.3.0.2</version> <exclusions> <exclusion> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.jeecg</groupId> <artifactId>easypoi-web</artifactId> <version>2.3.0.2</version> </dependency> <dependency> <groupId>org.jeecg</groupId> <artifactId>easypoi-annotation</artifactId> <version>2.3.0.2</version> </dependency> <!--EasyPOI 校验,下面两个实现 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.3.Final</version> <optional>true</optional> </dependency> <dependency> <groupId>org.apache.bval</groupId> <artifactId>org.apache.bval.bundle</artifactId> <version>1.1.0</version> </dependency>
2、实体类(用到了注解)
import lombok.Data; import org.jeecgframework.poi.excel.annotation.Excel; import org.jeecgframework.poi.excel.annotation.ExcelCollection; import java.math.BigDecimal; import java.util.Date; import java.util.List; /** * @author Yanli Yang */ @Data public class StatementAccountDetails{ /** * */ private Long id; /** * 订单id */ private Long orderId; /** * 订单编号 */ @Excel(name = "Order No.") private String orderNo; /** * 实收 */ @Excel(name = "Amount Received(RM)") private BigDecimal paidInAmount; /** * 卖家SKU */ @Excel(name = "SellerID") private String sellerSku; /** * 平台SKU 这个字段不要了 */ private String platformSku; /** * 交易时间 */ @Excel(name = "Order Time") private Date tradingTime; /** * 第三方支付手续费 */ @Excel(name = "Third Party Payment Handling Fee") private BigDecimal poundage; /** * 运费 */ @Excel(name = "Shipping") private BigDecimal freight; /** * 对账单id */ @Excel(name = "Reconciliation Order No.") private Long statementOfAccountId; /** * 手续费的税费 */ @Excel(name = "Tax for handling fee") private BigDecimal poundageTaxes; /** * 运费的税费 */ @Excel(name = "Tax for shipping") private BigDecimal freightTaxes; /** * 订单金额 */ @Excel(name = "Order Amount") private BigDecimal orderAmount; @ExcelCollection(name = "GoodsDetails", orderNum = "6") private List<StatementAccountGoods> statementAccountGoodsList; }
3、查询数据,并调用导出的方法
@RequestMapping("exportExcel") public void exportExcel(HttpServletResponse response,String statementAccountId){ Integer accountId = null; if(StringUtils.isNotEmpty(statementAccountId)){ accountId = Integer.parseInt(statementAccountId); } List<StatementAccountDetails> accountList = this.statementAccountService.getExportAccountDetailsList(accountId); if (accountList != null && accountList.size() > 0){ for (StatementAccountDetails statementAccountDetails : accountList) { List<StatementAccountGoods> statementAccountGoodses = this.statementAccountService.getExportAccountGoodsList(statementAccountDetails.getOrderId()); statementAccountDetails.setStatementAccountGoodsList(statementAccountGoodses); } } ExportParams exportParams = new ExportParams(); exportParams.setTitle("Reconciliation Details"); exportParams.setSheetName("Reconciliation Details"); Workbook workbook = ExcelExportUtil.exportExcel(exportParams,StatementAccountDetails.class,accountList); try { ResponseUtils.writeExcel(response,workbook,"ReconciliationDetails.xls"); } catch (IOException e) { log.error("对账单明细导出Excel错误",e); } }
4、工具类
import javax.servlet.http.HttpServletResponse; import java.io.BufferedOutputStream; import java.io.IOException; import java.io.OutputStream; /** * HttpServletResponse 相关的工具类 * * @author songjunjie */ public class ResponseUtils { /** * 将excel数据写到HttpServletResponse中,浏览器将提示下载文件。默认的编码格式为UTF-8 * * @param response * @param data 数据 * @param filename 浏览器下载文件时,显示的文件名字 * @throws IOException */ public static void writeExcel(HttpServletResponse response, byte[] data , String filename) throws IOException { // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // workbook.write(byteArrayOutputStream); // byte[] bytes = byteArrayOutputStream.toByteArray(); // 清空response response.reset(); // 设置response的Header response.setContentType(HttpContentTypes.XLS + ";charset=utf-8"); response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "iso8859-1")); response.addHeader("Content-Length", "" + data.length); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); outputStream.write(data); outputStream.flush(); outputStream.close(); } }
5、前端页面调用 要使用window.location.href 不能使用ajax调用
window.location.href = basePath+"/appointmentCtr/exportData?patientId="+appointment.patientId;
相关文章推荐
- POI导出excel和word
- java中使用poi导出数据到EXCEL模板中。
- POI导出Excel
- POI导出EXCEL经典实现
- 使用poi导入导出excel文件方法使用及改进
- poi导出excel,支持2007和2003,支持javabean,利用反射对poi进行简单封装,支持任意实体和乱序的excel表格
- 使用poi对Excel表的写入修改,再导出
- java POI导出EXCEL经典实现
- POI导出EXCEL经典实现
- struts实现poi导出成excel
- Java使用POI导出excel(上)——基本操作
- POI3.8 导出大数据excel
- java实现excel的导入导出(poi详解)
- SpringMVC实现poi 解析excel 导入导出
- poi导出word与excel模版
- Spring+SpringMVC+MyBatis +apche poi 实现excel 导出工具类封装
- SpringMvc 使用poi导入导出Excel
- POI导出EXCEL到网页下载
- Java实现POI导出Excel报表功能