Java实现导出Excel(Apache POI)

Java可以通过Apache POI库来实现导出Excel文件
超详细的步骤及示例代码:

  1.添加POI依赖:在Maven项目中,需要在pom.xml中添加以下依赖:
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
 2. 创建Excel文件:通过创建Workbook对象来创建Excel文件,支持多种格式,如XLS和XLSX。以下代码创建一个XLSX格式的Excel文件:
// 创建工作簿对象
Workbook workbook = new XSSFWorkbook();
// 创建工作表对象
Sheet sheet = workbook.createSheet("Sheet1");
 3. 创建表头:通过创建Row和Cell对象来创建Excel表头,并设置表头样式:
// 创建表头行
Row headerRow = sheet.createRow(0);
// 创建表头单元格
Cell headerCell = headerRow.createCell(0);
// 设置表头单元格的值
headerCell.setCellValue("姓名");
// 创建样式对象
CellStyle headerStyle = workbook.createCellStyle();
// 设置字体加粗
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);
// 设置对齐方式
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerCell.setCellStyle(headerStyle);
 4. .创建数据行:通过遍历数据列表来创建每一行的数据,并设置单元格样式:
// 创建数据行
int rowIndex = 1;
for (User user : userList) {
    Row row = sheet.createRow(rowIndex++);
    // 创建姓名单元格
    Cell nameCell = row.createCell(0);
    nameCell.setCellValue(user.getName());
    // 创建年龄单元格
    Cell ageCell = row.createCell(1);
    ageCell.setCellValue(user.getAge());
    // 创建样式对象
    CellStyle style = workbook.createCellStyle();
    // 设置对齐方式
    style.setAlignment(HorizontalAlignment.CENTER);
    nameCell.setCellStyle(style);
    ageCell.setCellStyle(style);
}
 5. 输出Excel文件:通过输出流将Workbook对象写入磁盘文件或内存流中。
// 输出Excel文件
FileOutputStream outputStream = new FileOutputStream("userList.xlsx");
workbook.write(outputStream);
outputStream.close();

完整代码:

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

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

public class ExcelExportDemo {

    public static void main(String[] args) throws Exception {
        // 创建数据列表
        List<User> userList = new ArrayList<>();
        userList.add(new User("张三", 20));
        userList.add(new User("李四", 30));

        // 创建工作簿对象
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表对象
        Sheet sheet = workbook.createSheet("Sheet1");

        // 创建表头行
        Row headerRow = sheet.createRow(0);
        // 创建表头单元格
        Cell headerCell = headerRow.createCell(0);
        // 设置表头单元格的值
        headerCell.setCellValue("姓名");
        // 创建样式对象
        CellStyle headerStyle = workbook.createCellStyle();
        // 设置字体加粗
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerStyle.setFont(headerFont);
        // 设置对齐方式
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        headerCell.setCellStyle(headerStyle);

        // 创建表头单元格2
        Cell headerCell2 = headerRow.createCell(1);
        // 设置表头单元格2的值
        headerCell2.setCellValue("年龄");
        // 设置样式
        headerCell2.setCellStyle(headerStyle);

        // 创建数据行
        int rowIndex = 1;
        for (User user : userList) {
            Row row = sheet.createRow(rowIndex++);
            // 创建姓名单元格
            Cell nameCell = row.createCell(0);
            nameCell.setCellValue(user.getName());
            // 创建年龄单元格
            Cell ageCell = row.createCell(1);
            ageCell.setCellValue(user.getAge());
            // 创建样式对象
            CellStyle style = workbook.createCellStyle();
            // 设置对齐方式
            style.setAlignment(HorizontalAlignment.CENTER);
            nameCell.setCellStyle(style);
            ageCell.setCellStyle(style);
        }

        // 输出Excel文件
        FileOutputStream outputStream = new FileOutputStream("userList.xlsx");
        workbook.write(outputStream);
        outputStream.close();
    }

    static class User {
        private String name;
        private int age;

        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用第三方库来操作KML文件。其中一个常用的库是JAK,它提供了一些类和方法用于创建和操作KML文件。以下是一个简单的示例代码,演示如何使用JAK库将数据导出为KML文件: ```java import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.jak.Kml; import org.jak.KmlException; import org.jak.Placemark; import org.jak.Point; public class KmlExporter { public static void main(String[] args) throws FileNotFoundException, KmlException { // 创建Kml对象 Kml kml = new Kml(); // 创建Placemark对象 Placemark placemark = new Placemark(); // 设置Placemark的名称和描述 placemark.setName("My Placemark"); placemark.setDescription("This is a sample placemark"); // 创建Point对象 Point point = new Point(); // 设置点的坐标 point.setLatitude(37.42228990140251); point.setLongitude(-122.0822035425683); // 将点添加到Placemark对象 placemark.setGeometry(point); // 将Placemark添加到Kml对象 List<Placemark> placemarks = new ArrayList<Placemark>(); placemarks.add(placemark); kml.setFeature(placemarks); // 将Kml对象写入KML文件 File file = new File("sample.kml"); kml.marshal(new FileOutputStream(file)); } } ``` 在这个例子中,我们创建了一个名为"My Placemark"的Placemark对象,该对象包含一个点的坐标。然后,我们将该Placemark添加到Kml对象中,并将Kml对象写入名为"sample.kml"的文件中。你可以根据自己的需求修改代码和添加更多的Placemark对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值