java 讀取xlsx

來源:互聯網
上載者:User

標籤:auto   sys   ntb   cat   hash   nodelist   artifact   org   val   

(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();
}

}
 

 

java 讀取xlsx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.