0
点赞
收藏
分享

微信扫一扫

java 生成Excel的几种方式

Java生成Excel的几种方式
  1. Apache POI:Apache POI是一个流行的用于处理Microsoft Office文档的Java库。它提供了一组API,可以用于创建、读取和修改Excel文件。可以使用Apache POI来创建新的Excel文件并填充数据,或者将现有的数据导出到Excel文件中。
  2. JExcelAPI:JExcelAPI是另一个用于处理Excel文件的Java库。它提供了一组简单易用的API,可以用于创建、读取和修改Excel文件。可以使用JExcelAPI来创建新的Excel文件并填充数据,或者将现有的数据导出到Excel文件中。
  3. EasyExcel:EasyExcel是阿里巴巴开源的一款基于注解的Java处理Excel的工具库。它提供了一组简单易用的API,可以用于快速创建、读取和写入Excel文件。可以使用EasyExcel来创建、读取和写入Excel文件,并且支持大量的数据量高效导入导出。
  4. Apache POI + Freemarker:结合Apache POI和Freemarker模板引擎,可以实现更加灵活的Excel导出。可以使用Freemarker创建Excel模板,然后使用Apache POI将数据填充到模板中生成最终的Excel文件。
  5. 使用第三方工具:除了上述的库之外,还有一些第三方工具可以帮助您进行Excel导出,例如:Aspose.Cells、JasperReports等。这些工具通常提供了更丰富的功能和更高级的特性,但可能需要付费或具有一定的学习曲线。


Apache POI 示例:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

public class ApachePOIExample {

    public static void main(String[] args) {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("Sheet1");

            Row headerRow = sheet.createRow(0);
            createCell(headerRow, 0, "Name");
            createCell(headerRow, 1, "Age");
            createCell(headerRow, 2, "City");

            Row dataRow = sheet.createRow(1);
            createCell(dataRow, 0, "John Doe");
            createCell(dataRow, 1, "30");
            createCell(dataRow, 2, "New York");

            try (FileOutputStream outputStream = new FileOutputStream("output.xlsx")) {
                workbook.write(outputStream);
                System.out.println("Excel generated successfully.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void createCell(Row row, int columnNumber, String cellValue) {
        Cell cell = row.createCell(columnNumber);
        cell.setCellValue(cellValue);
    }
}


  1. JExcelAPI 示例:

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import java.io.File;

public class JExcelAPIExample {

    public static void main(String[] args) {
        try {
            WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls"));
            WritableSheet sheet = workbook.createSheet("Sheet1", 0);

            Label nameLabel = new Label(0, 0, "Name");
            sheet.addCell(nameLabel);

            Label ageLabel = new Label(1, 0, "Age");
            sheet.addCell(ageLabel);

            Label cityLabel = new Label(2, 0, "City");
            sheet.addCell(cityLabel);

            Label nameValue = new Label(0, 1, "John Doe");
            sheet.addCell(nameValue);

            Label ageValue = new Label(1, 1, "30");
            sheet.addCell(ageValue);

            Label cityValue = new Label(2, 1, "New York");
            sheet.addCell(cityValue);

            workbook.write();
            workbook.close();

            System.out.println("Excel generated successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  1. EasyExcel 示例:

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;

import java.util.ArrayList;
import java.util.List;

public class EasyExcelExample {

    public static void main(String[] args) {
        try {
            List<ExcelData> data = new ArrayList<>();
            data.add(new ExcelData("John Doe", "30", "New York"));

            String fileName = "output.xlsx";
            String sheetName = "Sheet1";

            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            // 设置表头样式

            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            // 设置内容样式

            HorizontalCellStyleStrategy styleStrategy =
                    new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

            EasyExcel.write(fileName, ExcelData.class)
                    .sheet(sheetName)
                    .registerWriteHandler(styleStrategy)
                    .doWrite(data);

            System.out.println("Excel generated successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class ExcelData {
        private String name;
        private String age;
        private String city;

        public ExcelData(String name, String age, String city) {
            this.name = name;
            this.age = age;
            this.city = city;
        }

        // Getters and setters
    }
}

举报

相关推荐

0 条评论