poi简单知识记录

主pom:

顶端  <poi.version>3.12</poi.version>

 

<!--  poi start-->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi.version}</version>
                <scope>provided</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>${poi.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-examples</artifactId>
                <version>${poi.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-excelant</artifactId>
                <version>${poi.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-scratchpad</artifactId>
                <version>${poi.version}</version>
                <scope>provided</scope>
            </dependency>
            <!-- poi end -->

 

子pom:

<!-- poi start -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-examples</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-excelant</artifactId>
        </dependency>
        <dependency>
        <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
        </dependency>
        <!-- poi end -->

 

 

代码:

package com.loan.common.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class POITest {
    public static void main(String[] args) {
        //创建Workbook
        HSSFWorkbook workbook = new HSSFWorkbook();
        //样式
        HSSFCellStyle setBorder = workbook.createCellStyle();
        //字体
        HSSFFont font = workbook.createFont();
        //创建sheet
        HSSFSheet sheet = workbook.createSheet("sheet1");
        HSSFRow row = sheet.createRow(1);
        HSSFCell cell = row.createCell(1);
        cell.setCellValue("hello herman");
        
        font.setFontName("仿宋_GB2312");
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
        font.setFontHeightInPoints((short) 22);
        
        setBorder.setFillForegroundColor((short) 13);// 设置背景色
        setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
        setBorder.setFont(font);//选择需要用到的字体格式
        setBorder.setWrapText(true);//设置自动换行
        
        cell.setCellStyle(setBorder);
        
        try {
            FileOutputStream outputStream = new FileOutputStream("/home/herman/123333/1111.xlsx");
            workbook.write(outputStream);
            outputStream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            //读取
            read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void read() throws IOException {
        try {
            //读取excel文件
            FileInputStream in = new FileInputStream("/home/herman/123333/1111.xlsx");
            //将输入流转换为workbook对象
            HSSFWorkbook workbook = new HSSFWorkbook(in);
            //获取工作表
             
            HSSFSheet sheet = workbook.getSheetAt(0);
            //获取行
            HSSFRow row = sheet.getRow(1);
            //读取单元格
            HSSFCell cell = row.getCell(1);
            //得到单元格的内容
            String value = cell.getStringCellValue();
            //打印到控制台
            System.out.println("B2的单元格内容为:"+value);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值