• <noscript id="e0iig"><kbd id="e0iig"></kbd></noscript>
  • <td id="e0iig"></td>
  • <option id="e0iig"></option>
  • <noscript id="e0iig"><source id="e0iig"></source></noscript>
  • poi 讀寫excel

    1. Basic definitions for Apache POI library

    This section briefly describe about basic classes used during Excel Read and Write.

    1. HSSF is prefixed before the class name to indicate operations related to a Microsoft Excel 2003 file.
    2. XSSF is prefixed before the class name to indicate operations related to a Microsoft Excel 2007 file or later.
    3. XSSFWorkbook and HSSFWorkbook are classes which act as an Excel Workbook
    4. HSSFSheet and XSSFSheet are classes which act as an Excel Worksheet
    5. Row defines an Excel row
    6. Cell defines an Excel cell addressed in reference to a row.

    2. Download Apache POI

    Apache POI library is easily available using Maven Dependencies.

    pom.xml

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

    3. Apache POI library – Writing a Simple Excel

    The below code shows how to write a simple Excel file using Apache POI libraries. The code uses a 2 dimensional data array to hold the data. The data is written to a XSSFWorkbook object. XSSFSheet is the work sheet being worked on. The code is as shown below:

    ApachePOIExcelWrite.java

    package com.mkyong;
    
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class ApachePOIExcelWrite {
    
        private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";
    
        public static void main(String[] args) {
    
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Datatypes in Java");
            Object[][] datatypes = {
                    {"Datatype", "Type", "Size(in bytes)"},
                    {"int", "Primitive", 2},
                    {"float", "Primitive", 4},
                    {"double", "Primitive", 8},
                    {"char", "Primitive", 1},
                    {"String", "Non-Primitive", "No fixed size"}
            };
    
            int rowNum = 0;
            System.out.println("Creating excel");
    
            for (Object[] datatype : datatypes) {
                Row row = sheet.createRow(rowNum++);
                int colNum = 0;
                for (Object field : datatype) {
                    Cell cell = row.createCell(colNum++);
                    if (field instanceof String) {
                        cell.setCellValue((String) field);
                    } else if (field instanceof Integer) {
                        cell.setCellValue((Integer) field);
                    }
                }
            }
    
            try {
                FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
                workbook.write(outputStream);
                workbook.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            System.out.println("Done");
        }
    }

    On executing the above code, you get below excel as an output.

    apache-poi-read-write-excel

    4. Apache POI library – Reading an Excel file

    The below code explains how to read an Excel file using Apache POI libraries. The function getCellTypeEnum is deprecated in version 3.15 and will be renamed to getCellType from version 4.0 onwards.

    ApachePOIExcelRead.java

    package com.mkyong;
    
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Iterator;
    
    public class ApachePOIExcelRead {
    
        private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";
    
        public static void main(String[] args) {
    
            try {
    
                FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
                Workbook workbook = new XSSFWorkbook(excelFile);
                Sheet datatypeSheet = workbook.getSheetAt(0);
                Iterator<Row> iterator = datatypeSheet.iterator();
    
                while (iterator.hasNext()) {
    
                    Row currentRow = iterator.next();
                    Iterator<Cell> cellIterator = currentRow.iterator();
    
                    while (cellIterator.hasNext()) {
    
                        Cell currentCell = cellIterator.next();
                        //getCellTypeEnum shown as deprecated for version 3.15
                        //getCellTypeEnum ill be renamed to getCellType starting from version 4.0
                        if (currentCell.getCellTypeEnum() == CellType.STRING) {
                            System.out.print(currentCell.getStringCellValue() + "--");
                        } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
                            System.out.print(currentCell.getNumericCellValue() + "--");
                        }
    
                    }
                    System.out.println();
    
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }

    On executing the above code, you get the below output.

    Datatype--Type--Size(in bytes)--
    int--Primitive--2.0--
    float--Primitive--4.0--
    double--Primitive--8.0--
    char--Primitive--1.0--
    String--Non-Primitive--No fixed size--
    版權聲明:本文為tianshishangxin1原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
    本文鏈接:https://blog.csdn.net/tianshishangxin1/article/details/103255093

    智能推薦

    SpringBoot整合POI實現Excel與word文檔的讀寫操作(excel篇)

    SpringBoot整合POI實現Excel表格的讀寫操作(1) 導入Pom依賴 POI寫操作 POI讀操作 實體 數據庫表 Controller層接口 測試及結果 8.可能會出現的異常 IOException、FileNotFoundException。 注意文件保存路徑的位置。關于java利用poi操作Word文檔的案例請看下一篇文章。...

    POI讀取加密Excel

    摘要 1.POI讀取加密Excel功能 2.解決辦法 2.1安裝JCE Unlimited 2.1.1 方案 2.1.2 結果 2.1.3 原因 2.2.Reflection 2.2.1 方案 2.2.2 結果 2.2.3 注意 參考文獻 摘要 解決POI讀取加密Excel文件時報的 EncryptedDocumentException: Export Restrictions in place錯...

    poi導出Excel報表

       下面是對poi導出excel的封裝,稍微改動一下就能用于其它項目       pojo類見http://liudeh-009.iteye.com/blog/1847626的Student類       Excel導出類:             ...

    使用POI解析Excel

    Excel作為一種常用的數據存儲格式,在很多項目中都會有相應的導入導出的功能。這篇文章會介紹如何使用Java操作Excel,以及如何解決大文件讀寫時內存溢出的問題。 1、OpenXML標準 Word、Excel、PPT是Office辦公套件中最常用的三個組件。早期的Office套件使用二進制格式,這里面包括以.doc、.xls、.ppt為后綴的文件;直到07這個劃時代的版本將基于XML的壓縮格式作...

    POI Excel文件導出

        工具類: 使用這些方法就可以將Excel導出  ...

    猜你喜歡

    poi導出excel

    最近做了一個通過poi導出excel的功能,當做筆記記錄一下 導出的樣式為 需要原樣輸出,代碼中注釋挺詳細的   導出效果:...

    HTML中常用操作關于:頁面跳轉,空格

    1.頁面跳轉 2.空格的代替符...

    freemarker + ItextRender 根據模板生成PDF文件

    1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...

    電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!

    Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...

    requests實現全自動PPT模板

    http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...

    精品国产乱码久久久久久蜜桃不卡