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

Apache POI簡(jiǎn)介及應(yīng)用場(chǎng)景

 更新時(shí)間:2023年11月28日 11:24:43   作者:Ja kar ta  
Apache POI 是一個(gè)處理Miscrosoft Office各種文件格式的開(kāi)源項(xiàng)目,我們可以使用POI在Java程序中對(duì)Miscrosoft Office各種文件進(jìn)行讀寫(xiě)操作,本文給大家介紹Apache POI簡(jiǎn)介,感興趣的朋友一起看看吧

三十二、Apache POI

32.1 介紹

Apache POI 是一個(gè)處理Miscrosoft Office各種文件格式的開(kāi)源項(xiàng)目。簡(jiǎn)單來(lái)說(shuō)就是,我們可以使用POI在Java程序中對(duì)Miscrosoft Office各種文件進(jìn)行讀寫(xiě)操作。

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

Apache POI 的應(yīng)用場(chǎng)景:

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

各種業(yè)務(wù)系統(tǒng)導(dǎo)出Excel報(bào)表

批量導(dǎo)入業(yè)務(wù)數(shù)據(jù)

32.2 入門(mén)案例

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

Apache POI的maven坐標(biāo):

<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ù)寫(xiě)入Excel文件

1). 代碼開(kāi)發(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 {
    /**
     * 通過(guò)POI創(chuàng)建Excel 文件并且寫(xiě)入文件內(nèi)容
     */
    public static void write() throws Exception {
        //在內(nèi)存中創(chuàng)建一個(gè)Excel文件
        XSSFWorkbook excel = new XSSFWorkbook();
        //在Excel文件中創(chuàng)建一個(gè)Sheet頁(yè)
        XSSFSheet sheet = excel.createSheet("info");
        //在Sheet中創(chuàng)建一個(gè)行對(duì)象,rownum編號(hào)從0開(kāi)始
        XSSFRow row = sheet.createRow(0);
        //創(chuàng)建單元格并且寫(xiě)入文件內(nèi)容
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("城市");
        //創(chuàng)建一個(gè)新行
        row = sheet.createRow(1);
        row.createCell(0).setCellValue("張三");
        row.createCell(1).setCellValue("北京");
        row = sheet.createRow(2);
        row.createCell(0).setCellValue("李四");
        row.createCell(1).setCellValue("南京");
        //通過(guò)輸出流將內(nèi)存中的Excel文件寫(xiě)入到磁盤(pán)
        FileOutputStream out = new FileOutputStream(new File("D:\\info.xlsx"));
        excel.write(out);
        //關(guān)閉資源
        out.close();
        excel.close();
    }
    public static void main(String[] args) throws Exception {
        write();
    }
}

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

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

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

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

1). 代碼開(kāi)發(fā)

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

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

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

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

相關(guān)文章

最新評(píng)論