java 读取 xlsx

Posted 林**

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 读取 xlsx相关的知识,希望对你有一定的参考价值。

package test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class T {
    public static void main(String[] args) throws IOException {
        new T().readXlsx("D:\\data\\parse_result.xlsx", 1);
        
    }
    
    /**
     * 
     * @param path  xlsx文件路径
     * @param numSheet  读取第几张表(从 0 开始)
     * @return
     * @throws IOException
     */
    public List<DeviceInfo> readXlsx(String path, int numSheet) throws IOException {
        InputStream is = new FileInputStream(path);
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
        DeviceInfo deviceInfo = null;
        List<DeviceInfo> list = new ArrayList<DeviceInfo>();
            //读取第几张表
            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
            //读取行
            for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
                XSSFRow xssfRow = xssfSheet.getRow(rowNum);
                if (xssfRow != null) {
                    deviceInfo = new DeviceInfo();
                    XSSFCell SN = xssfRow.getCell(0);
                    XSSFCell cpu = xssfRow.getCell(1);
                    XSSFCell ram = xssfRow.getCell(2);
                    XSSFCell times = xssfRow.getCell(3);
                    XSSFCell WAN = xssfRow.getCell(4);
                    XSSFCell value = xssfRow.getCell(5);
                    XSSFCell PPPOEStatus = xssfRow.getCell(3);
                    
                    try {
                        deviceInfo.setSN(SN.toString());
                        deviceInfo.setCpu(cpu.toString());
                        deviceInfo.setRam(ram.toString());
                        deviceInfo.setTimes(times.toString());
                        deviceInfo.setWAN(WAN.toString());
                        deviceInfo.setValue(value.toString());
                        deviceInfo.setPPPOEStatus(PPPOEStatus.toString());
                    } catch (NullPointerException e) {
                    }
                    list.add(deviceInfo);
                }
            }
        for(DeviceInfo d: list) {
            System.out.println(d.getSN());
        }
        return list;
    }

    private String getValue(XSSFCell sN) {
        return null;
    }
}

 

以上是关于java 读取 xlsx的主要内容,如果未能解决你的问题,请参考以下文章

Java 读取xlsx

java读取txt/pdf/xls/xlsx/doc/docx/ppt/pptx

使用Apache POI读取Excel .XLSX时出错

在 Java 中读取 xlsx 复选框值

使用java读取xlsx格式的execl,除了poi还都有哪些方式

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例