使用POI讀取/建立Execl(.xlsx)檔案,poi.xlsx

來源:互聯網
上載者:User

使用POI讀取/建立Execl(.xlsx)檔案,poi.xlsx

最近項目中用到瞭解析Execl表格的功能,在網上百度了一下自己寫了一個小Demo。由於項目中使用的是Execl2007,就是尾碼為.xlsx的,所以只研究瞭解析和建立Execl2007的檔案,解析Execl2007和Execl2003有一定的區別,大家在解析的時候要注意。

一下是Demo中用到的jar包(可能有幾個包用不到,懶得刪了):

其他的我就不過多的介紹了,每個方法的具體用法可以參考官方手冊,直接上代碼:

poi文檔和jar包:http://poi.apache.org/download.html

  1 package com.lym.test;  2   3 import java.io.FileInputStream;  4 import java.io.FileOutputStream;  5 import java.io.InputStream;  6 import java.io.OutputStream;  7   8 import org.apache.poi.hssf.util.HSSFColor;  9 import org.apache.poi.ss.usermodel.Cell; 10 import org.apache.poi.ss.usermodel.CellStyle; 11 import org.apache.poi.ss.usermodel.Font; 12 import org.apache.poi.ss.usermodel.Row; 13 import org.apache.poi.ss.usermodel.Workbook; 14 import org.apache.poi.xssf.usermodel.XSSFSheet; 15 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 16  17 /** 18  * 使用POI讀取/建立Execl(2007版.xlsx)檔案 19  *  20  * @author 劉彥民 21  * 22  */ 23 public class POIReaderExecl { 24  25     public static void main(String[] args) throws Exception { 26  27          readExeclFile(); 28 //        createExeclFile(); 29     } 30  31     /** 32      * 建立Execl檔案 33      *  34      * @throws Exception 35      */ 36     public static void createExeclFile() throws Exception { 37         // 建立工作空間 38         XSSFWorkbook wb = new XSSFWorkbook(); 39         // 建立工作表 40         XSSFSheet sheet = wb.createSheet("testdata"); 41         // 建立標題列(第一行) 42         Row row0 = sheet.createRow(0); 43         // 建立列 44         for (int i = 0; i < 11; i++) { 45             Cell cell_1 = row0.createCell(i, Cell.CELL_TYPE_STRING);// 第一個參數:列數(從0開始),第二個參數:列類型 46             // 設定儲存格樣式 47             CellStyle style = getStyle(wb); 48             cell_1.setCellStyle(style); 49             cell_1.setCellValue("Hello Column" + i); 50             // 設定自動調整大小 51             sheet.autoSizeColumn(i); 52         } 53         for (int rowNum = 1; rowNum < 200; rowNum++) { 54             Row row = sheet.createRow(rowNum); 55             for (int i = 0; i < 11; i++) { 56                 Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING); 57                 cell.setCellValue("cell(" + String.valueOf(rowNum + 1) +", "+ String.valueOf(i + 1) + ")"); 58             } 59         } 60         OutputStream out = new FileOutputStream("D://data.xlsx"); 61         wb.write(out); 62         out.flush(); 63         out.close(); 64     } 65  66     /** 67      * 讀取Execl檔案 68      *  69      * @throws Exception 70      */ 71     public static void readExeclFile() throws Exception { 72         // 建立輸入資料流 73         InputStream in = new FileInputStream("D://order.xlsx"); 74         // 建立工作空間 75         XSSFWorkbook wb = new XSSFWorkbook(in); 76         // 擷取工作表 77         XSSFSheet sheet = wb.getSheetAt(0);// 擷取第一個工作表 78         // 擷取總行數 79         int rowNums = sheet.getLastRowNum(); 80         // 擷取總列數 81         int colNums = sheet.getPhysicalNumberOfRows(); 82         // 工作行 83         Row row = null; 84         // 工作儲存格 85         Cell cell = null; 86  87         System.out.println("總行數:" + rowNums); 88         System.out.println("總列數:" + colNums); 89         // 迴圈遍曆每行的內容 90         for (int i = 1; i <= rowNums; i++) { 91             // 擷取第i行的工作行,第0行是列頭,所以從第1行開始 92             row = sheet.getRow(i); 93             // 擷取每個儲存格的值 94             for (int j = 0; j < colNums; j++) { 95                 cell = row.getCell(j); 96                 cell.setCellType(Cell.CELL_TYPE_STRING); 97                 System.out.print(cell.toString() + "        "); 98             } 99             System.out.println();100         }101     }102 103     /**104      * 設定儲存格樣式105      */106     public static CellStyle getStyle(Workbook workbook) {107 108         CellStyle style = workbook.createCellStyle();109         style.setAlignment(CellStyle.ALIGN_CENTER);110         style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);111         // 設定儲存格字型112         Font headerFont = workbook.createFont(); // 字型113         headerFont.setFontHeightInPoints((short) 14);114         headerFont.setColor(HSSFColor.RED.index);115         headerFont.setFontName("宋體");116         style.setFont(headerFont);117         style.setWrapText(true);118 119         // 設定儲存格邊框及顏色120         style.setBorderBottom((short) 1);121         style.setBorderLeft((short) 1);122         style.setBorderRight((short) 1);123         style.setBorderTop((short) 1);124         style.setWrapText(true);125         return style;126     }127 128 }

讀取order.xlsx檔案的內容:

產生data.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.