java讀取xlsx檔案的所有資料

來源:互聯網
上載者:User

標籤:UI   tostring   lld   apache   case   excel   日期   book   inf   

 

 

此次demo中使用到的jar。(點擊進入)

 

 

 

 

java:

package com.test;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFDataFormat;import org.apache.poi.ss.usermodel.*;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.Date;/** * Created by wdnnccey on 17/2/27. * <p> * <p> * 2017年02月27日21:27:26, * */public class TestXlsx2 {    public static void main(String[] args) {        File excelFile = new File("/Users/wdnnccey/Desktop/test/test.xlsx"); //替換你文檔地址        XSSFWorkbook wb = null;        try {            wb = new XSSFWorkbook(new FileInputStream(excelFile));        } catch (IOException e) {            e.printStackTrace();        }        int numberOfSheets = wb.getNumberOfSheets();        String str = "";        for (int x = 0; x < numberOfSheets; x++) {            XSSFSheet sheet = wb.getSheetAt(x);            int columnNum = 0;            if (sheet.getRow(0) != null) {                columnNum = sheet.getRow(0).getLastCellNum()                        - sheet.getRow(0).getFirstCellNum();            }            if (columnNum > 0) {                for (Row row : sheet) {                    String[] singleRow = new String[columnNum];                    int n = 0;                    for (int i = 0; i < columnNum; i++) {                        Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK);                        switch (cell.getCellType()) {                            case Cell.CELL_TYPE_BLANK:                                singleRow[n] = "";                                if (cell == null || cell.equals("") || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {                                    System.out.print("<Null>|");                                } else {                                    System.out.print(singleRow[n] + "|");                                }                                break;                            case Cell.CELL_TYPE_BOOLEAN:                                singleRow[n] = Boolean.toString(cell                                        .getBooleanCellValue());                                System.out.print(singleRow[n] + "|");                                break;                            // 數值                            case Cell.CELL_TYPE_NUMERIC:                                if (DateUtil.isCellDateFormatted(cell)) {                                    SimpleDateFormat sdf = null;                                    if (cell.getCellStyle().getDataFormat() == HSSFDataFormat                                            .getBuiltinFormat("h:mm")) {                                        sdf = new SimpleDateFormat("HH:mm");                                    } else {// 日期                                        sdf = new SimpleDateFormat("yyyy-MM-dd");                                    }                                    Date date = cell.getDateCellValue();                                    System.out.print(sdf.format(date) + "|");                                } else {                                    cell.setCellType(Cell.CELL_TYPE_STRING);                                    String temp = cell.getStringCellValue();                                    // 判斷是否包含小數點,如果不含小數點,則以字串讀取,如果含小數點,則轉換為Double類型的字串                                    if (temp.indexOf(".") > -1) {                                        singleRow[n] = String.valueOf(new Double(temp))                                                .trim();                                        System.out.print(singleRow[n] + "|");                                    } else {                                        singleRow[n] = temp.trim();                                        System.out.print(singleRow[n] + "|");                                    }                                }                                break;                            case Cell.CELL_TYPE_STRING:                                singleRow[n] = cell.getStringCellValue().trim();                                System.out.print(singleRow[n] + "|");                                break;                            case Cell.CELL_TYPE_ERROR:                                singleRow[n] = "";                                System.out.print(singleRow[n] + "|");                                break;                            case Cell.CELL_TYPE_FORMULA:                                cell.setCellType(Cell.CELL_TYPE_STRING);                                String temp = cell.getStringCellValue();                                // 判斷是否包含小數點,如果不含小數點,則以字串讀取,如果含小數點,則轉換為Double類型的字串                                if (temp.indexOf(".") > -1) {                                    temp = String.valueOf(new Double(temp))                                            .trim();                                    Double cny = Double.parseDouble(temp);//6.2041                                    DecimalFormat df = new DecimalFormat("0.00");                                    String CNY = df.format(cny);                                    System.out.print(CNY + "|");                                } else {                                    singleRow[n] = temp.trim();                                    System.out.print(singleRow[n] + "|");                                }                            default:                                singleRow[n] = "";                                break;                        }                        n++;                    }                    System.out.println();                }                System.out.println("===========================================================Sheet分割線===========================================================");            }        }    }}

 

java讀取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.