1、 POI介紹
Apache POI是用Java編寫的免費開源的跨平臺的Java API,Apache POI提供API給Java程?序?qū)icrosoft Office格式檔案讀和寫的功能,其中使用最多的就是使用POI操作Excel文?件。
jxl:專門操作Excel
maven坐標(biāo):
POI結(jié)構(gòu):
2、入門案例
2.1??從Excel文件讀取數(shù)據(jù)
使用POI可以從一個已經(jīng)存在的Excel文件中讀取數(shù)據(jù)。
//使用POI讀取Excel文件中的數(shù)據(jù)
@Test
public void test1() throws Exception{
????//加載指定文件,創(chuàng)建一個Excel對象(工作簿)
????XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("C:\\io\\score.xlsx")));
????//讀取Excel文件中第一個Sheet標(biāo)簽頁
????XSSFSheet sheet = excel.getSheetAt(0);
????//遍歷Sheet標(biāo)簽頁,獲得每一行數(shù)據(jù)
????for (Row row : sheet) {
????????//遍歷行,獲得每個單元格對象
????????for (Cell cell : row) {
????????????System.out.println(cell.getStringCellValue());
????????}
????}
????//關(guān)閉資源
????excel.close();
}
通過上面的入門案例可以看到,POI操作Excel表格封裝了幾個核心對象:
上面案例是通過遍歷工作表獲得行,遍歷行獲得單元格,最終獲取單元格中的值。
還有一種方式就是獲取工作表最后一個行號,從而根據(jù)行號獲得行對象,通過行獲取最 后一個單元格索引,從而根據(jù)單元格索引獲取每行的一個單元格對象,代碼如下:
//使用POI讀取Excel文件中的數(shù)據(jù)
@Test
public void test2() throws Exception{
????//加載指定文件,創(chuàng)建一個Excel對象(工作簿)
????XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("c:\\io\\score.xlsx")));
????//讀取Excel文件中第一個Sheet標(biāo)簽頁
????XSSFSheet sheet = excel.getSheetAt(0);
????//獲得當(dāng)前工作表中最后一個行號,需要注意:行號從0開始
????int lastRowNum = sheet.getLastRowNum();
????System.out.println("lastRowNum = " + lastRowNum);
????for(int i=0;i<=lastRowNum;i++){
????????XSSFRow row = sheet.getRow(i);//根據(jù)行號獲取每一行
????????//獲得當(dāng)前行最后一個單元格索引
????????short lastCellNum = row.getLastCellNum();
????????System.out.println("lastCellNum = " + lastCellNum);
????????for(int j=0;j<lastCellNum;j++){
????????????XSSFCell cell = row.getCell(j);//根據(jù)單元格索引獲得單元格對象
????????????System.out.println(cell.getStringCellValue());
????????}
????}
????//關(guān)閉資源
????excel.close();
}
? 文章來源:http://www.zghlxwxcb.cn/news/detail-838824.html
2.2?向Excel文件寫入數(shù)據(jù)
使用POI可以在內(nèi)存中創(chuàng)建一個Excel文件并將數(shù)據(jù)寫入到這個文件,最后通過輸出流將?內(nèi)存中的Excel文件下載到磁盤。文章來源地址http://www.zghlxwxcb.cn/news/detail-838824.html
//使用POI向Excel文件寫入數(shù)據(jù),并且通過輸出流將創(chuàng)建的Excel文件保存到本地磁盤
@Test
public void test3() throws Exception{
????//在內(nèi)存中創(chuàng)建一個Excel文件(工作簿)
????XSSFWorkbook excel = new XSSFWorkbook();
????//創(chuàng)建一個工作表對象
????XSSFSheet sheet = excel.createSheet("oracle");
????//在工作表中創(chuàng)建行對象
????XSSFRow title = sheet.createRow(0);
????//在行中創(chuàng)建單元格對象
????title.createCell(0).setCellValue("姓名");
????title.createCell(1).setCellValue("地址");
????title.createCell(2).setCellValue("年齡");
????XSSFRow dataRow = sheet.createRow(1);
????dataRow.createCell(0).setCellValue("小明");
????dataRow.createCell(1).setCellValue("北京");
????dataRow.createCell(2).setCellValue("20");
????//創(chuàng)建一個輸出流,通過輸出流將內(nèi)存中的Excel文件寫到磁盤
????FileOutputStream out = new FileOutputStream(new File("c:\\io\\hello.xlsx"));
????excel.write(out);
????out.flush();
????excel.close();
}
到了這里,關(guān)于Apache POI Excel的讀寫的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!