zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    Windows API—CreateEvent—创建事件
    C++的注册和回调
    Python内置模块-logging
    使用 C++ 处理 JSON 数据交换格式
    Python生成器
    5.Spring-Boot缓存数据之Redis
    6.Spring-Boot项目发布到独立的tomcat中
    7.Spring-Boot自定义Banner
    8.Spring-Boot之SpringJdbcTemplate整合Freemarker
    9.Spring-Boot之Mybatis-LogBack-Freemarker
  • 原文地址:https://www.cnblogs.com/redhat0019/p/8945949.html
Copyright © 2011-2022 走看看