poi 导出excel
使用jar版本:
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency>
demo:
package com.ssh.media.server.utils; import com.ssh.commons.jedis.JedisUtil; import com.ssh.media.server.config.RedisConstant; import com.ssh.media.server.dto.ProgramCategoryVo; import com.ssh.media.server.dto.ProgramDto; import com.ssh.media.server.dto.ProgramVO; import com.ssh.media.server.mapper.DetectionPlayUrlConfigMapper; import com.ssh.media.server.mapper.ProgramMapper; import com.ssh.media.server.mapper.RotationResMapper; import com.ssh.media.server.utils.redis.RedisUtils; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * @author liuxn * @date 2020/8/10 **/ @Component public class ExcelUtils { public Workbook createFile(List<ProgramDto> list) { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet1 = workbook.createSheet("节目"); HSSFSheet sheet2 = workbook.createSheet("轮播资源"); sheet1.autoSizeColumn(1); sheet2.autoSizeColumn(1); HSSFCellStyle style = workbook.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); String[] headers1 = {"节目ID", "节目名", "上映年份", "分类", "分集数", "分集名称", "播放URL","播放链接ID"}; String[] headers2 = {"轮播频道ID", "轮播频道名", "播放URL","播放链接ID"}; //创建标题 //创建标题样式 // HSSFCellStyle style = workbook.createCellStyle(); HSSFRow row1 = sheet1.createRow(0); for (int i = 0; i < headers1.length; i++) { HSSFCell cell = row1.createCell(i); cell.setCellValue(headers1[i]); cell.setCellStyle(style); } HSSFRow row2 = sheet2.createRow(0); for (int i = 0; i < headers2.length; i++) { HSSFCell cell = row2.createCell(i); cell.setCellValue(headers2[i]); cell.setCellStyle(style); } JedisUtil instance = JedisUtil.getInstance(); int index1 = 1; int index2 = 1; for (ProgramDto programDto : list) { if ("rotation".equals(programDto.getType())) { //sheet2 HSSFRow row = sheet2.createRow(index2++); createSheet2(row, programDto); } else { //sheet2 HSSFRow row = sheet1.createRow(index1++); createSheet1(row, programDto, instance); } } return workbook; } private void createSheet2(HSSFRow row, ProgramDto programDto) { HSSFCell cell = row.createCell(0); cell.setCellValue(programDto.getProgramId()); HSSFCell cell1 = row.createCell(1); cell1.setCellValue(programDto.getProgramName()); HSSFCell cell2 = row.createCell(2); cell2.setCellValue(programDto.getProgramUrl()); HSSFCell cell3 = row.createCell(3); cell3.setCellValue(programDto.getDiversityPlayId()); } private void createSheet1(HSSFRow row, ProgramDto programDto, JedisUtil jedisUtil) { HSSFCell cell = row.createCell(0); cell.setCellValue(programVO.getProgramId()); HSSFCell cell1 = row.createCell(1); cell1.setCellValue(programVO.getProgramName()); HSSFCell cell2 = row.createCell(2); cell2.setCellValue(programVO.getResYear()); HSSFCell cell3 = row.createCell(3); cell3.setCellValue(category); HSSFCell cell4 = row.createCell(4); cell4.setCellValue(programDto.getEpisodes()); HSSFCell cell5 = row.createCell(5); cell5.setCellValue(programDto.getDiversityName()); HSSFCell cell6 = row.createCell(6); cell6.setCellValue(programDto.getProgramUrl()); HSSFCell cell7 = row.createCell(7); cell7.setCellValue(programDto.getDiversityPlayId()); } }