您的位置:首页 > 编程语言 > Java开发

poi导出excel

2017-09-07 15:33 18 查看
1、pom.xml文件中添加依赖

<!--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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java javaweb