数据来自 通义千问🎈
依赖包
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.2</version>
</dependency>
v5.2.2。
创建Excel
xlsx 格式。
简单版
创建一个包含数据的 Excel 文件
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;public class ExcelWriter {public static void main(String[] args) {try (Workbook workbook = new XSSFWorkbook(); // 创建一个新的 xlsx 工作簿FileOutputStream fileOut = new FileOutputStream("output.xlsx")) { // 创建一个输出流// 创建一个工作表Sheet sheet = workbook.createSheet("Sheet1");// 创建一行Row row = sheet.createRow(0);// 创建单元格并填充数据Cell cell = row.createCell(0);cell.setCellValue("Hello World!");cell = row.createCell(1);cell.setCellValue(new Date());// 写入文件workbook.write(fileOut);System.out.println("Excel file created successfully.");} catch (IOException e) {e.printStackTrace();}}
}
进阶版
创建更复杂的 Excel 文件
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream;
import java.io.IOException;public class ExcelComplexWriter {public static void main(String[] args) {try (Workbook workbook = new XSSFWorkbook(); // 创建一个新的 xlsx 工作簿FileOutputStream fileOut = new FileOutputStream("output_complex.xlsx")) { // 创建一个输出流// 创建一个工作表Sheet sheet = workbook.createSheet("Sheet1");// 创建一行Row row = sheet.createRow(0);// 创建单元格并填充数据Cell cell = row.createCell(0);cell.setCellValue("Name");cell = row.createCell(1);cell.setCellValue("Age");// 创建第二个行row = sheet.createRow(1);cell = row.createCell(0);cell.setCellValue("John Doe");cell = row.createCell(1);cell.setCellValue(30);// 设置样式CellStyle style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());style.setFillPattern(FillPatternType.SOLID_FOREGROUND);// 应用样式cell = row.createCell(2);cell.setCellValue("New York");cell.setCellStyle(style);// 写入文件workbook.write(fileOut);System.out.println("Excel file created successfully.");} catch (IOException e) {e.printStackTrace();}}
}
一些Excel的设置
列宽、单元格自动换行。
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream;
import java.io.IOException;public class ExcelWriterWithWidthAndWrap {public static void main(String[] args) {try (Workbook workbook = new XSSFWorkbook(); // 创建一个新的 xlsx 工作簿FileOutputStream fileOut = new FileOutputStream("output_with_width_and_wrap.xlsx")) { // 创建一个输出流// 创建一个工作表Sheet sheet = workbook.createSheet("Sheet1");// 设置列宽sheet.setColumnWidth(0, 20 * 256); // 设置第一列的宽度为 20 个字符sheet.setColumnWidth(1, 40 * 256); // 设置第二列的宽度为 40 个字符// 创建一行Row row = sheet.createRow(0);// 创建单元格并填充数据Cell cell = row.createCell(0);cell.setCellValue("Hello World!");cell = row.createCell(1);cell.setCellValue("This is a very long sentence that needs to be wrapped automatically.");// 设置样式CellStyle style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());style.setFillPattern(FillPatternType.SOLID_FOREGROUND);style.setWrapText(true); // 设置自动换行// 应用样式cell.setCellStyle(style);// 写入文件workbook.write(fileOut);System.out.println("Excel file created successfully.");} catch (IOException e) {e.printStackTrace();}}
}
Apache POI 官网
https://poi.apache.org/apidocs/index.html
5.0.x 的文档:
https://poi.apache.org/apidocs/5.0/
---END---
本文链接:
https://www.cnblogs.com/luo630/p/18387024
ben发布于博客园
ben发布于博客园