主要依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
Excel 导入
public static List<Map<Integer, String>> inExcel(String filePath, Boolean head) {
try {
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
List<Map<Integer, String>> lists = new ArrayList<>();
XSSFWorkbook book = new XSSFWorkbook(fileInputStream);
XSSFSheet sheet = book.getSheetAt(0);
if (head) {
XSSFRow rowHead = sheet.getRow(0);
int cols = rowHead.getPhysicalNumberOfCells();
Map<Integer, String> map = new HashMap<>(cols);
for (int j = 0; j < cols; j++) {
rowHead.getCell(j).setCellType(CellType.STRING);
String row1 = rowHead.getCell(j).getStringCellValue();
map.put(j + 1, row1);
}
lists.add(map);
}
for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
XSSFRow row = sheet.getRow(i);
int cols = row.getPhysicalNumberOfCells();
Map<Integer, String> map = new HashMap<>(cols);
for (int j = 0; j <= cols; j++) {
XSSFCell cell = row.getCell(j);
if (cell == null){
map.put(j + 1, "");
}else {
cell.setCellType(CellType.STRING);
String row1 = cell.getStringCellValue();
map.put(j + 1, row1);
}
}
lists.add(map);
}
return lists;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Excel 导出
@ToString
@Getter
public class ExportDto {
private List<ExportPart> exportParts;
public ExportDto() {
exportParts = new ArrayList<>(20);
}
public void add(String name, Object value){
ExportPart exportPart = new ExportPart(name, value);
exportParts.add(exportPart);
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class ExportPart{
private String name;
private Object value;
}
}
private XSSFWorkbook export(List<ExportDto> exportDtos) {
try {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
sheet.setDefaultColumnWidth((short) 18);
XSSFRow row = sheet.createRow(0);
if (CollectionUtil.isEmpty(exportDtos)){
return workbook;
}
List<ExportDto.ExportPart> exportParts = exportDtos.get(0).getExportParts();
for (short i = 0; i < exportParts.size(); i++) {
XSSFCell cell = row.createCell(i);
XSSFRichTextString text = new XSSFRichTextString(exportParts.get(i).getName())
cell.setCellValue(text);
}
for (int i = 0; i < exportDtos.size(); i++) {
row = sheet.createRow(i + 1);
ExportDto exportDto = exportDtos.get(i);
List<ExportDto.ExportPart> partList = exportDto.getExportParts();
for (int j = 0; j < partList.size(); j++) {
Object value = partList.get(j).getValue();
String string = value == null ? "" : value.toString();
XSSFCell cell = row.createCell(j);
XSSFRichTextString richString = new XSSFRichTextString(string);
XSSFFont font = workbook.createFont();
richString.applyFont(font);
cell.setCellValue(richString);
}
}
return workbook;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private void setResponseOutputStream(HttpServletResponse response, XSSFWorkbook hssfWorkbook, String fileName) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
try {
response.addHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
try {
hssfWorkbook.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void export(HttpServletResponse response) {
List<IndicatorsEntity> entities = indicatorsDao.queryAllList();
ArrayList<ExportDto> exportDtos = new ArrayList<>();
AtomicReference<Integer> num = new AtomicReference<>(1);
entities.forEach(entity -> {
ExportDto exportDto = new ExportDto();
exportDto.add("序号", String.valueOf(num.getAndSet(num.get() + 1)));
exportDto.add("所属期", entity.getBelongYear());
exportDtos.add(exportDto);
});
String fileName = UUIDUtil.generateDefaultUUID() + ".xlsx";
XSSFWorkbook workbook = export(exportDtos);
setResponseOutputStream(response, workbook, fileName);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南