读取Excel

2020-07-14  本文已影响0人  浅浅星空
        <!--APACHE POI-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.11</version>
        </dependency>
package com.jiaoyinbrother.util;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;

public class POIDemo {

    private static final String STR_FORMAT = "UPDATE user SET THIRD_ORDER_ID = '%s',UPDATED_USER='SYSTEM',UPDATED_STAMP=now()" +
            "where ID = %s;";

    public static void main(String[] args) throws Exception {
        poiTestMethod();
    }

    public static void poiTestMethod() throws Exception {
        //1.读取Excel文档对象
        XSSFWorkbook  hssfWorkbook = new XSSFWorkbook(new FileInputStream("E:\\tshuo\\tmall.xlsx"));
        //2.获取要解析的表格(第一个表格)
        XSSFSheet sheet = hssfWorkbook.getSheetAt(0);
        //获得最后一行的行号
        int lastRowNum = sheet.getLastRowNum();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i <= lastRowNum; i++) {//遍历每一行
            //3.获得要解析的行
            XSSFRow row = sheet.getRow(i);
            //4.获得每个单元格中的内容(String)
            String third_order_id = row.getCell(0).getStringCellValue();
            String id = row.getCell(37).toString();
            id = id.replace(".0", "");
            sb.append(String.format(STR_FORMAT, third_order_id,id));

            if ("100333".equals(id)) {
                break;
            }
        }
        System.out.println("-----------------------------");
        System.out.println(sb.toString());
        System.out.println("-----------------------------");
    }
}
上一篇下一篇

猜你喜欢

热点阅读