在Java中生成Excel文件,你可以使用多种库,以下是一些常用的库和方法:
Apache POI:这是最流行的Java库之一,用于处理Microsoft Office文档。
- Maven依赖:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.2</version> </dependency>
- 示例代码: “`java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream; import java.io.IOException;
public class ExcelGenerator {
public static void main(String[] args) { Workbook workbook = new XSSFWorkbook(); // 创建Excel工作簿 Sheet sheet = workbook.createSheet("Example Sheet"); // 创建工作表 // 创建行和单元格 Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Hello World"); // 写入文件 try (FileOutputStream outputStream = new FileOutputStream("example.xlsx")) { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } }
} “`
- Maven依赖:
JExcelAPI(也称为JXL):这是另一个用于处理Excel文件的库,但只支持到Excel ‘03格式。
- Maven依赖:
<dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.12</version> </dependency>
- 示例代码: “`java import jxl.Workbook; import jxl.write.Label; import jxl.write.Number; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook;
import java.io.FileOutputStream;
public class ExcelGenerator {
public static void main(String[] args) { try { WritableWorkbook workbook = Workbook.createWorkbook(new FileOutputStream("example.xls")); WritableSheet sheet = workbook.createSheet("Example Sheet", 0); // 创建单元格 Label label = new Label(0, 0, "Hello World"); Number number = new Number(1, 0, 123); sheet.addCell(label); sheet.addCell(number); workbook.write(); workbook.close(); } catch (Exception e) { e.printStackTrace(); } }
} “`
- Maven依赖:
SimpleExcelWriter:这是一个简单易用的库,用于快速创建Excel文件。
- Maven依赖:
<dependency> <groupId>com.github.abel533</groupId> <artifactId>simpleexcel</artifactId> <version>1.12</version> </dependency>
- 示例代码: “`java import com.github.abel533.echarts.json.GsonOption; import com.github.abel533.echarts期权.Option; import com.github.abel533.simpleexcel.core.SimpleWriter; import com.github.abel533.simpleexcel.core.write.style.WriteCellStyle; import com.github.abel533.simpleexcel.core.write.style.WriteCellStyleFactory;
import java.io.FileOutputStream;
public class ExcelGenerator {
public static void main(String[] args) { SimpleWriter writer = SimpleWriter.write("example.xlsx").head(Head1.class).build(); try { WriteCellStyle headWriteCellStyle = WriteCellStyleFactory.headWriteCellStyle(); WriteCellStyle contentWriteCellStyle = WriteCellStyleFactory.contentWriteCellStyle(); // 写入数据 writer.write(dataList, headWriteCellStyle, contentWriteCellStyle); } catch (Exception e) { e.printStackTrace(); } finally { writer.close(); } }
} “`
- Maven依赖:
Apache Tika:用于解析和读取文件内容,包括Excel文件。
EasyExcel:一个基于Java的简单、易用、性能高效的Excel处理库。
- Maven依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.0.5</version> </dependency>
- 示例代码: “`java import com.alibaba.excel.EasyExcel;
import java.util.List;
public class ExcelGenerator {
public static void main(String[] args) { String fileName = "example.xlsx"; List<?> data = getData(); // 获取数据 EasyExcel.write(fileName, YourDataClass.class).sheet("Sheet1").doWrite(data); } private static List<?> getData() { // 获取数据的逻辑 return null; }
} “`
- Maven依赖:
选择哪个库取决于你的具体需求,例如是否需要处理大量数据、是否需要读取和写入Excel文件等。