以下是一個(gè)詳細(xì)的示例,展示了如何使用SXSSFWorkbook將多個(gè)List數(shù)據(jù)和單個(gè)實(shí)體DTO導(dǎo)出到多個(gè)Sheet頁(yè):
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFCell;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
public class ExportToMultipleSheetsExample {
? ? public static void main(String[] args) {
? ? ? ? // 創(chuàng)建工作簿
? ? ? ? SXSSFWorkbook workbook = new SXSSFWorkbook();
? ? ? ? // 添加第一個(gè)Sheet頁(yè)(示例:字符串列表)
? ? ? ? List<String> stringData = getListData(); ?// 獲取字符串列表數(shù)據(jù)
? ? ? ? addSheetWithData(workbook, "String Data", stringData);
? ? ? ? // 添加第二個(gè)Sheet頁(yè)(示例:整數(shù)列表)
? ? ? ? List<Integer> integerData = getListData(); ?// 獲取整數(shù)列表數(shù)據(jù)
? ? ? ? addSheetWithData(workbook, "Integer Data", integerData);
? ? ? ? // 添加第三個(gè)Sheet頁(yè)(示例:?jiǎn)蝹€(gè)實(shí)體DTO)
? ? ? ? EntityDto entityDto = getEntityDto(); ?// 獲取單個(gè)實(shí)體DTO數(shù)據(jù)
? ? ? ? addSheetWithEntityDto(workbook, "Entity Data", entityDto);
? ? ? ? try (FileOutputStream fileOut = new FileOutputStream("output.xlsx")) {
? ? ? ? ? ? // 將Workbook寫入文件
? ? ? ? ? ? workbook.write(fileOut);
? ? ? ? ? ? System.out.println("Excel導(dǎo)出完成!");
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? // 關(guān)閉工作簿
? ? ? ? ? ? workbook.dispose();
? ? ? ? }
? ? }
? ? private static <T> void addSheetWithData(SXSSFWorkbook workbook, String sheetName, List<T> data) {
? ? ? ? // 創(chuàng)建Sheet頁(yè)
? ? ? ? SXSSFSheet sheet = workbook.createSheet(sheetName);
? ? ? ? // 添加數(shù)據(jù)
? ? ? ? for (int i = 0; i < data.size(); i++) {
? ? ? ? ? ? SXSSFRow row = sheet.createRow(i);
? ? ? ? ? ? SXSSFCell cell = row.createCell(0);
? ? ? ? ? ? cell.setCellValue(String.valueOf(data.get(i)));
? ? ? ? }
? ? }
? ? private static void addSheetWithEntityDto(SXSSFWorkbook workbook, String sheetName, EntityDto entityDto) {
? ? ? ? // 創(chuàng)建Sheet頁(yè)
? ? ? ? SXSSFSheet sheet = workbook.createSheet(sheetName);
? ? ? ? // 添加表頭
? ? ? ? SXSSFRow headerRow = sheet.createRow(0);
? ? ? ? headerRow.createCell(0).setCellValue("ID");
? ? ? ? headerRow.createCell(1).setCellValue("Name");
? ? ? ? headerRow.createCell(2).setCellValue("Age");
? ? ? ? // 添加數(shù)據(jù)行
? ? ? ? SXSSFRow dataRow = sheet.createRow(1);
? ? ? ? dataRow.createCell(0).setCellValue(entityDto.getId());
? ? ? ? dataRow.createCell(1).setCellValue(entityDto.getName());
? ? ? ? dataRow.createCell(2).setCellValue(entityDto.getAge());
? ? }
? ? private static List<String> getListData() {
? ? ? ? // 返回字符串列表數(shù)據(jù)(示例)
? ? ? ? return List.of("Apple", "Banana", "Orange", "Grapes");
? ? }
? ? private static List<Integer> getIntegerListData() {
? ? ? ? // 返回整數(shù)列表數(shù)據(jù)(示例)
? ? ? ? return List.of(10, 20, 30, 40, 50);
? ? }
? ? private static EntityDto getEntityDto() {
? ? ? ? // 返回單個(gè)實(shí)體DTO數(shù)據(jù)(示例)
? ? ? ? return new EntityDto(1, "John Doe", 25);
? ? }
? ? private static class EntityDto {
? ? ? ? private int id;
? ? ? ? private String name;
? ? ? ? private int age;
? ? ? ? public EntityDto(int id, String name, int age) {
? ? ? ? ? ? this.id = id;
? ? ? ? ? ? this.name = name;
? ? ? ? ? ? this.age = age;
? ? ? ? }
? ? ? ? public int getId() {
? ? ? ? ? ? return id;
? ? ? ? }
? ? ? ? public String getName() {
? ? ? ? ? ? return name;
? ? ? ? }文章來源:http://www.zghlxwxcb.cn/news/detail-671637.html
? ? ? ? public int getAge() {
? ? ? ? ? ? return age;
? ? ? ? }
? ? }
}文章來源地址http://www.zghlxwxcb.cn/news/detail-671637.html
到了這里,關(guān)于java中用SXSSFWorkbook把多個(gè)list數(shù)據(jù)和單個(gè)實(shí)體dto導(dǎo)出到excel如何導(dǎo)出到多個(gè)sheet頁(yè)詳細(xì)實(shí)例?(親測(cè))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!