首先上maven依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
根据自己的业务需求来建立数据实体(QxwhExcelEntity),创建数据集合
List<QxwhExcelEntity> qxwhList = XXXX;
List<QxwhExcelEntity> fzList = new ArrayList<QxwhExcelEntity>(qxwhList);
for (QxwhExcelEntity qxwhExcelEntity : qxwhList) {
params.put("ID", qxwhExcelEntity.getID());
List<t_mx_attachment> fjidLists = taskService.getFJID(params);
if (fjidLists.size()>0){
qxwhExcelEntity.setFujian(qxwhExcelEntity.getID());
}
}
设置拦截器,用于在生成的excel中为字段添加超链接
import cn.afterturn.easypoi.handler.impl.ExcelDataHandlerDefaultImpl;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Hyperlink;
public class ExcelDataHandler extends ExcelDataHandlerDefaultImpl<QxwhExcelEntity> {
@Override
public Hyperlink getHyperlink(CreationHelper creationHelper, QxwhExcelEntity obj, String name, Object value) {
Hyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.FILE);
hyperlink.setLabel(name);
hyperlink.setAddress((String) value);
return hyperlink;
}
}
创建一个excel导出对象
ExportParams exportParams = new ExportParams();
exportParams.setDataHandler(new ExcelDataHandler());
Workbook workbook = ExcelExportUtil.exportExcel(exportParams,
QxwhExcelEntity.class, qxwhList);
Sheet sheet = workbook.getSheetAt(0);
autoSizeColumns(sheet, 45);
构建一个zip流对象 并指定实体
OutputStream out = response.getOutputStream();
ZipOutputStream zip = new ZipOutputStream(out);
ZipEntry entry = new ZipEntry("下载excel" + sdf.format(new Date()) + ".xls");
zip.putNextEntry(entry);
以下为业务逻辑,可参考
String qxid="";
List<QxwhExcelEntity> qxList = taskService.getQXInfo(ids);
for (QxwhExcelEntity qxwhExcelEntity : qxList) {
for(Row row : sheet) {
for(Cell cell : row) {
if(cell.getRichStringCellValue().getString().trim().equals(qxwhExcelEntity.getQXSB())){
rowNum = row.getRowNum();
qxid=qxwhExcelEntity.getID();
params.put("ID", qxid);
List<t_mx_attachment> fjidList = taskService.getFJID(params);
Row newRow = sheet.getRow(rowNum);
Cell newCell = newRow.getCell(45);
if (fjidList.size() > 0) {
newCell.setCellValue("点击查看");
}else{
newCell.setCellValue("");
}
CellStyle cellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setUnderline(Font.U_SINGLE);
font.setColor(IndexedColors.BLUE.getIndex());
cellStyle.setFont(font);
newCell.setCellStyle(cellStyle);
}
}
}
}
workbook.write(zip);
for (Object id : ids) {
params.clear();
params.put("ID", id.toString());
List<t_mx_attachment> fjidList = taskService.getFJID(params);
for (t_mx_attachment t_mx_attachment : fjidList) {
ChannelSftp sftp = SftpUtils.getChannelSftp(host, port, username, password);
String fileName = sftpPath + t_mx_attachment.getAttachmentId() + File.separator + t_mx_attachment.getAttName();
ByteArrayOutputStream swapStream = SftpUtils.getOutputStreamFromFtp(sftp, fileName);
ZipEntry image = new ZipEntry(id + File.separator + t_mx_attachment.getAttName());
zip.putNextEntry(image);
if (swapStream == null) {
System.out.println("流为空");
}
ByteArrayInputStream inputStream = new ByteArrayInputStream(swapStream.toByteArray());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
zip.write(buffer, 0, len);
}
inputStream.close();
}
}
zip.flush();
zip.close();
封装的方法
private void autoSizeColumns(Sheet sheet, int columnNumber) {
for (int i = 0; i < columnNumber; i++) {
sheet.autoSizeColumn(i, true);
int newWidth = sheet.getColumnWidth(i) + sheet.getColumnWidth(i);
sheet.setColumnWidth(i, newWidth);
}
}