欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Apache POI簡介及應用場景

 更新時間:2023年11月28日 11:24:43   作者:Ja kar ta  
Apache POI 是一個處理Miscrosoft Office各種文件格式的開源項目,我們可以使用POI在Java程序中對Miscrosoft Office各種文件進行讀寫操作,本文給大家介紹Apache POI簡介,感興趣的朋友一起看看吧

三十二、Apache POI

32.1 介紹

Apache POI 是一個處理Miscrosoft Office各種文件格式的開源項目。簡單來說就是,我們可以使用POI在Java程序中對Miscrosoft Office各種文件進行讀寫操作。

一般情況下,POI都是用于操作Excel文件。

Apache POI 的應用場景:

銀行網(wǎng)銀系統(tǒng)導出交易明細

各種業(yè)務系統(tǒng)導出Excel報表

批量導入業(yè)務數(shù)據(jù)

32.2 入門案例

Apache POI 既可以將數(shù)據(jù)寫入Excel文件,也可以讀取Excel文件中的數(shù)據(jù),接下來分別進行實現(xiàn)。

Apache POI的maven坐標:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.16</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version>
</dependency>

32.2.1 將數(shù)據(jù)寫入Excel文件

1). 代碼開發(fā)

package com.sky.test;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class POITest {
    /**
     * 通過POI創(chuàng)建Excel 文件并且寫入文件內(nèi)容
     */
    public static void write() throws Exception {
        //在內(nèi)存中創(chuàng)建一個Excel文件
        XSSFWorkbook excel = new XSSFWorkbook();
        //在Excel文件中創(chuàng)建一個Sheet頁
        XSSFSheet sheet = excel.createSheet("info");
        //在Sheet中創(chuàng)建一個行對象,rownum編號從0開始
        XSSFRow row = sheet.createRow(0);
        //創(chuàng)建單元格并且寫入文件內(nèi)容
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("城市");
        //創(chuàng)建一個新行
        row = sheet.createRow(1);
        row.createCell(0).setCellValue("張三");
        row.createCell(1).setCellValue("北京");
        row = sheet.createRow(2);
        row.createCell(0).setCellValue("李四");
        row.createCell(1).setCellValue("南京");
        //通過輸出流將內(nèi)存中的Excel文件寫入到磁盤
        FileOutputStream out = new FileOutputStream(new File("D:\\info.xlsx"));
        excel.write(out);
        //關閉資源
        out.close();
        excel.close();
    }
    public static void main(String[] args) throws Exception {
        write();
    }
}

2). 實現(xiàn)效果

在D盤中生成info.xlsx文件,創(chuàng)建名稱為info的Sheet頁,同時將內(nèi)容成功寫入。

外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

32.2.2 讀取Excel文件中的數(shù)據(jù)

1). 代碼開發(fā)

/**
 * 通過POI讀取Excel文件中的內(nèi)容
 */
public static void read() throws Exception {
    InputStream inputStream = new FileInputStream(new File("D:\\info.xlsx"));
    //讀取磁盤上已經(jīng)存在的Excel文件
    XSSFWorkbook excel = new XSSFWorkbook(inputStream);
    //讀取Excel文件中的第一個Sheet頁
    XSSFSheet sheet = excel.getSheetAt(0);
    //獲取Sheet中最后一行的行號
    int lastRowNum = sheet.getLastRowNum();
    for (int i=0;i<=lastRowNum;i++){
        //獲得某一行
        XSSFRow row = sheet.getRow(i);
        //獲得單元格對象
        String cellValue1 = row.getCell(0).getStringCellValue();
        String cellValue2 = row.getCell(1).getStringCellValue();
        System.out.println(cellValue1+" "+cellValue2);
    }
    //關閉資源
    inputStream.close();
    excel.close();
}
public static void main(String[] args) throws Exception {
    //write();
    read();
}

2). 實現(xiàn)效果

外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

到此這篇關于Apache POI簡介的文章就介紹到這了,更多相關Apache POI簡介內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論