POI相关操作

1.将MultpartFile(Spring)转化为InputStream

/**
     * 转化为InputStream
     *
     * @param multipartFile multipartFile
     * @return InputStream
     * @throws IOException IOException
     */
    public static InputStream parseInputStream(MultipartFile multipartFile) throws IOException {
        File file = File.createTempFile("temp", null);
        multipartFile.transferTo(file);
        FileInputStream inputStream = new FileInputStream(file);
        file.deleteOnExit();
        return inputStream;
    }

2.将文件InputStream转化为Workbook

/**
     * 获取Workbook
     *
     * @param inputStream inputStream
     * @return inputStream
     * @throws IOException IOException
     */
    public static Workbook getWorkBook(InputStream inputStream) throws IOException {
        return WorkbookFactory.create(inputStream);
    }

3.设置值以及获取值

public static boolean checkExcelFormat(Workbook wb) {
    int count = wb.getNumberOfSheets();
    Sheet sheet = sheet = wb.getSheetAt(0);
    Row row = sheet.getRow(0);
    //    获取A1的String的值
    row.getCell(0).getStringCellValue();

    //    设置A1的值
    row.getCell(0).setCellValue("value");
}

4.设置一列为文本类型

//    i为列数, textCellStyle(wb)为表格样式
sheet.setDefaultColumnStyle(i, textCellStyle(wb));

5.设置样式

private static CellStyle textCellStyle(Workbook wb) {
        CellStyle textCellStyle = wb.createCellStyle();
        textCellStyle.setAlignment(HorizontalAlignment.CENTER);
        DataFormat format = wb.createDataFormat();
        textCellStyle.setDataFormat(format.getFormat("@"));
        return textCellStyle;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值