java复合表头_Java中使用 poi 导出复合表头(合并表头)

packageorg.alanlau.common.util;importorg.apache.commons.fileupload.disk.DiskFileItem;importorg.apache.commons.fileupload.disk.DiskFileItemFactory;importorg.apache.commons.lang3.time.DateFormatUtils;import org.apache.poi.ss.usermodel.*;importorg.apache.poi.ss.util.CellRangeAddress;importorg.apache.poi.ss.util.RegionUtil;importorg.apache.poi.xssf.usermodel.XSSFClientAnchor;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;importorg.apache.tomcat.util.http.fileupload.IOUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.web.multipart.MultipartFile;importorg.springframework.web.multipart.commons.CommonsMultipartFile;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.io.UnsupportedEncodingException;importjava.net.URLEncoder;importjava.time.LocalDateTime;importjava.time.format.DateTimeFormatter;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importjava.util.Map;/*** 支持复合表头导出的Excel工具类

*

*@authorliukun

*@version1.0

* @date 2020/2/18 20:22*/

public classExcelUtils {privateExcelUtils() {throw new UnsupportedOperationException("initialization is prohibited...");

}private static Logger logger = LoggerFactory.getLogger(ExcelUtils.class);/*** 图片*/

public static classImage {private byte[] imageData;public Image(byte[] imageData) {this.imageData =imageData;

}public byte[] getImageData() {returnimageData;

}

}/*** 导出(目前仅支持小于两行表头的合并操作)

*@paramrealName 导出文件名

*@paramdata 导出数据

*@paramfields 导出字段,比如 date|时间,firstHeader|表头1,secondHeader|表头2,sub001|编号-0001|子标题1,sub002|编号-0001|子标题2,sub003|编号-0001|子标题3,sub004|编号-0001|子标题4,remark|备注*/

public static void export(HttpServletResponse response, String realName, List>data, String fields) {

String fileName= realName + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".xlsx";

writeFileToResponse(construct(fields, data, fileName), response);

}/*** 导出并转换时间格式(目前仅支持小于两行表头的合并操作)

*@paramrealName 导出文件名

*@paramdata 导出数据

*@paramfields 导出字段,比如 date|时间,firstHeader|表头1,secondHeader|表头2,sub001|编号-0001|子标题1,sub002|编号-0001|子标题2,sub003|编号-0001

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Apache POI库来导出动态表头的Excel文件。首先,你需要创建一个Workbook对象,然后创建一个Sheet对象。接下来,你可以使用Row和Cell对象创建行和单元格,并设置相应的值。 下面是一个示例代码,演示如何使用Apache POI导出具有动态表头的Excel文件: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.List; public class ExcelExporter { public static void main(String[] args) { List<String> headers = Arrays.asList("Header 1", "Header 2", "Header 3"); // 动态表头 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 创建表头行 Row headerRow = sheet.createRow(0); for (int i = 0; i < headers.size(); i++) { Cell headerCell = headerRow.createCell(i); headerCell.setCellValue(headers.get(i)); } // 创建数据行 // 假设有两行数据 List<List<String>> data = Arrays.asList( Arrays.asList("Data 1", "Data 2", "Data 3"), Arrays.asList("Data 4", "Data 5", "Data 6") ); int rowIndex = 1; // 数据行索引从1开始 for (List<String> rowData : data) { Row dataRow = sheet.createRow(rowIndex++); for (int i = 0; i < rowData.size(); i++) { Cell dataCell = dataRow.createCell(i); dataCell.setCellValue(rowData.get(i)); } } // 保存Excel文件 try (FileOutputStream outputStream = new FileOutputStream("output.xlsx")) { workbook.write(outputStream); System.out.println("Excel导出成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例,我们使用了XSSFWorkbook类来创建一个Excel文件,并创建了一个名为"Sheet1"的工作表。然后,我们根据动态表头创建了表头行,并使用循环创建了数据行。最后,我们将工作簿写入输出流,并保存为名为"output.xlsx"的文件。 你可以根据自己的需求修改表头和数据,然后使用上述代码来导出具有动态表头的Excel文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值