zoukankan      html  css  js  c++  java
  • java 读取xlsx

    (1)导入maven

    <!-- 读取excel文件 -->
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
    </dependency>

    (2)代码

    public static void main(String[] args) {
    // 解压Book1.xlsx
    ZipFile xlsxFile;
    try {
    xlsxFile = new ZipFile(new File("/Users/chengtao/downloads/教练入职时间.xlsx"));
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    // 先读取sharedStrings.xml这个文件备用
    ZipEntry sharedStringXML = xlsxFile.getEntry("xl/sharedStrings.xml");
    InputStream sharedStringXMLIS = xlsxFile.getInputStream(sharedStringXML);
    Document sharedString;
    sharedString = dbf.newDocumentBuilder().parse(sharedStringXMLIS);
    NodeList str = sharedString.getElementsByTagName("t");
    String sharedStrings[] = new String[str.getLength()];
    for (int n = 0; n < str.getLength(); n++) {
    Element element = (Element) str.item(n);
    sharedStrings[n] = element.getTextContent();
    }
    // 找到解压文件夹里的workbook.xml,此文件中包含了这张工作表中有几个sheet
    ZipEntry workbookXML = xlsxFile.getEntry("xl/workbook.xml");
    InputStream workbookXMLIS = xlsxFile.getInputStream(workbookXML);
    Document doc = dbf.newDocumentBuilder().parse(workbookXMLIS);
    // 获取一共有几个sheet
    NodeList nl = doc.getElementsByTagName("sheet");

    for (int i = 0; i < nl.getLength(); i++) {
    Element element = (Element) nl.item(i);// 将node转化为element,用来得到每个节点的属性
    System.out.println(element.getAttribute("name"));// 输出sheet节点的name属性的值
    // 接着就要到解压文件夹里找到对应的name值的xml文件,比如在workbook.xml中有<sheet name="Sheet1"
    // sheetId="1" r:id="rId1" /> 节点
    // 那么就可以在解压文件夹里的xl/worksheets下找到sheet1.xml,这个xml文件夹里就是包含的表格的内容
    ZipEntry sheetXML = xlsxFile.getEntry("xl/worksheets/" + element.getAttribute("name").toLowerCase() + ".xml");
    InputStream sheetXMLIS = xlsxFile.getInputStream(sheetXML);
    Document sheetdoc = dbf.newDocumentBuilder().parse(sheetXMLIS);
    NodeList rowdata = sheetdoc.getElementsByTagName("row");
    HashMap<String, String> nameAndEntryTimeMap = new HashMap<String,String>();
    for (int j = 0; j < rowdata.getLength(); j++) {
    System.out.println(" 第" + j +"行 ------------- ");
    // 得到每个行
    // 行的格式:
    /*
    * <row r="1" spans="1:3">r表示第一行,spans表示有几列 <c r="A1"
    * t="s">//r表示该列的列表
    * ,t="s"个人认为是表示这个单元格的内容可以在sharedStrings.xml这个文件里找到,对应的节点
    * 下标就是v节点的值,即0,若没有t属性,则v的值就是该单元格的内容 <v>0</v> </c> <c r="B1"
    * t="s"> <v>1</v> </c> <c r="C1" t="s"> <v>2</v> </c> </row>
    */
    Element row = (Element) rowdata.item(j); // 获取每一行
    NodeList columndata = row.getElementsByTagName("c");// 根据行得到每个行中的列

    String name_Key = null; //姓名
    String time_value = null; //入职时间
    for (int k = 0; k < columndata.getLength(); k++) {
    Element column = (Element) columndata.item(k);
    NodeList values = column.getElementsByTagName("v");
    Element value = (Element) values.item(0);
    if (column.getAttribute("t") != null & column.getAttribute("t").equals("s")) {
    // 如果是共享字符串则在sharedstring.xml里查找该列的值
    System.out.println(" 第" + k +"列 "+sharedStrings[Integer.parseInt(value.getTextContent())] + " ");
    if(k ==0){
    name_Key = sharedStrings[Integer.parseInt(value.getTextContent())];
    }else if(k==2){
    time_value = sharedStrings[Integer.parseInt(value.getTextContent())];
    }
    } else {
    if (value != null) {
    System.out.print(value.getTextContent() + " ");
    }else {
    System.out.println("j : " + j + " k : " + k + " null");
    }
    }
    }
    nameAndEntryTimeMap.put(name_Key, time_value);
    System.out.println();
    }
    System.out.println(nameAndEntryTimeMap);
    }
    } catch (ZipException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
     
  • 相关阅读:
    redis 日常使用
    centos7新装mysql 5.7
    spring 的常用功能
    软件安装教程
    每日成长17年1月
    ubuntu下不用拔盘就可以重新识别usb设备
    使用Linux遇到的一些问题和解决方案
    在XEN上启动guest时loopback设备不足
    使用virtualenv搭建python虚拟开发环境
    Linux局域网登陆响应时间过长
  • 原文地址:https://www.cnblogs.com/ctaixw/p/6288657.html
Copyright © 2011-2022 走看看