poi 导出excel

1、添加jar 

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>

2、编辑excel

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

import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by Steve on 2018/10/24 0024.
 */
public class XLSXFileWriter {

    public static void writer() throws Exception {
        Workbook workbook = new XSSFWorkbook();
        // 创建shell
        Sheet sheet = workbook.createSheet("南京英霸建材科技发展有限公司财务指标对比结果表");

        // 创建行
        Row row = sheet.createRow(0);
        // 创建列
        Cell cell = row.createCell(0);
        cell.setCellValue("南京英霸建材科技发展有限公司财务指标对比结果表");
        // 合并单元格(行,行,列,列)
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
        // 设置单元格样式
        CellStyle cellStyle = workbook.createCellStyle();
        // 设置水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 设置垂直居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cell.setCellStyle(cellStyle);

        row = sheet.createRow(1);
        row.createCell(0).setCellValue("导出日期:2018-09-10");
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 1));
        row = sheet.createRow(2);
        // 设置字体加粗
        cellStyle = workbook.createCellStyle();
        // 设置水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 设置垂直居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        Font font = workbook.createFont();
        font.setBold(true);
        cellStyle.setFont(font);
        cell = row.createCell(0);
        cell.setCellValue("指标值");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(1);
        cell.setCellValue("南京英霸建材科技发展有限公司");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(2);
        cell.setCellValue("陆特能源");
        cell.setCellStyle(cellStyle);

        // 设置自适应列宽
        for (int i = 0; i < 3; i++) {
            sheet.autoSizeColumn(i,true);
            sheet.setColumnWidth(i,sheet.getColumnWidth(i)*2);
        }

        File file = new File("F:/var/excel/test.xlsx");
        if (!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        workbook.write(new FileOutputStream(file));
    }

    public static void main(String[] args) throws Exception {
        writer();
        System.out.println("完成");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java的POI导出Excel文件的步骤: 1. 首先,需要在项目中引入POI的依赖,可以通过Maven或手动下载jar包的方式引入。 2. 创建一个工作簿对象,可以通过HSSFWorkbook或XSSFWorkbook类来创建,前者用于创建xls格式的Excel文件,后者用于创建xlsx格式的Excel文件。 3. 创建一个工作表对象,可以通过工作簿对象的createSheet()方法来创建。 4. 创建行和单元格对象,可以通过工作表对象的createRow()和createCell()方法来创建。 5. 设置单元格的值,可以通过单元格对象的setCellValue()方法来设置。 6. 将工作簿对象写入到输出流中,可以通过工作簿对象的write()方法来实现。 以下是一个简单的示例代码,用于将数据导出Excel文件中: ```java import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class ExcelExporter { public static void export() throws IOException { // 创建工作簿对象 Workbook workbook = new HSSFWorkbook(); // 创建工作表对象 Sheet sheet = workbook.createSheet("Sheet1"); // 创建行对象 Row row = sheet.createRow(0); // 创建单元格对象 Cell cell = row.createCell(0); // 设置单元格的值 cell.setCellValue("Hello, World!"); // 将工作簿对象写入到输出流中 FileOutputStream fos = new FileOutputStream("output.xls"); workbook.write(fos); fos.close(); } } ``` 调用export()方法即可将数据导出到名为output.xls的Excel文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值