使用poi解析excel表格,并导入数据

废话不多说,先导入jar包

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

    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.1.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl -->
    <dependency>
        <groupId>net.sourceforge.jexcelapi</groupId>
        <artifactId>jxl</artifactId>
        <version>2.6.12</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>4.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>3.1.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>

</dependencies>

其他博客中说只导入POI和POI-ooxml,试了,直接报错,classnotfoundError,所以还是老老实实导jar包吧,

上代码

 

import java.io.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class demo01 {
    public static void ExcelRead() throws Exception {
        //确定要操作的是c:/1.xls
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File("D:/readExcel.xlsx")));
//		HSSFWorkbook workbook = new HSSFWorkbook();
        //从第0个单元格开始取值
        XSSFSheet sheet = workbook.getSheetAt(0);
        //sheet.getPhysicalNumberOfRows();求出所有行数
        for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
            //取一行操作
            XSSFRow row = sheet.getRow(i);
            //row.getPhysicalNumberOfCells();求出本行的单元格数,也就是列数
            for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
                System.out.print(row.getCell(j) + "\t");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) throws Exception {
      	ExcelRead();
       // ExcelWrite();
    }


    public static void setBoderStyle(XSSFCellStyle style) {
        style.setBorderTop(BorderStyle.DOUBLE); // 上边框为双线
        style.setBorderRight(BorderStyle.DASH_DOT_DOT); // 右边框为虚线
        style.setBorderBottom(BorderStyle.DASHED); // 底边框为单线
        style.setBottomBorderColor((short) 1);// 底边框为红色
    }

    public static void setFontStyle(XSSFWorkbook workbook, XSSFCellStyle style) {
        XSSFFont font = workbook.createFont();// 要设置字体样式先要创建字体
        font.setFontHeightInPoints((short) 16);// 字号
        font.setBold(true);// 加粗
        font.setItalic(true);// 斜体
        font.setColor((short) 8);// 字体颜色是红色
        style.setFont(font); // 把这个设置好的字体样色压入样式
    }

    public static void allColumnAutoSize(XSSFSheet sheet) {
        // 遍历所有单元格,把单元格皆设置为最优列宽。
        for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
            XSSFRow row = sheet.getRow(i);
            for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
                sheet.autoSizeColumn(j);
            }
        }
    }

    public static void ExcelWrite() {

        // 创建一个webbook,对应一个Excel文件
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 在webbook中添加一个Excel单元表sheet,并设置单元表的问题
        XSSFSheet sheet = workbook.createSheet("单元表标题");

        // 在sheet中添加第0行,注意老版本poi对Excel的行数列数是有限制
        XSSFRow row = sheet.createRow(0);
        // 创建一个居中样式
        XSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        // 在于这个居中样式的基础上,添加表格边框样式
        setBoderStyle(style);
        // 创建第0个单元格
        XSSFCell cell = row.createCell(0);
        // 设置这个单元格的内容为“一”
        cell.setCellValue("一");
        // 设置这个单元格的格式为上面设置好的居中样式+表格边框样式
        cell.setCellStyle(style);
        // 同理创建第1个单元格并且设置好样式,下面以此类推
        cell = row.createCell(1);
        cell.setCellValue("二");
        cell.setCellStyle(style);
        cell = row.createCell(2);
        cell.setCellValue("三");
        cell.setCellStyle(style);

        // 创建第1行
        row = sheet.createRow(1);
        // 清空上面设置好的居中样式+表格边框样式
        style = workbook.createCellStyle();
        // 设置字体样式
        setFontStyle(workbook, style);
        cell = row.createCell(0);
        cell.setCellValue("111");
        cell.setCellStyle(style);
        cell = row.createCell(1);
        cell.setCellValue("222");
        cell.setCellStyle(style);
        cell = row.createCell(2);
        cell.setCellValue("333");
        cell.setCellStyle(style);
        // 自动调整列宽
        allColumnAutoSize(sheet);

        // 将文件存到指定位置
        try {
            //false代表覆盖输出
            FileOutputStream fileOutputStream = new FileOutputStream(
                    "D:/test.xlsx", false);
            workbook.write(fileOutputStream);
            //人走带门
            fileOutputStream.close();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

离离原上草77

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值