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

java poi判斷excel是xlsx還是xls類型

 更新時(shí)間:2024年10月29日 15:55:09   作者:java_丫丫程序媛  
這篇文章主要為大家詳細(xì)介紹了如何利用java poi來(lái)判斷excel是xlsx還是xls類型,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下

java poi判斷excel是xlsx還是xls

依賴

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

代碼

package com.test.excel;
 
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
import java.io.File;
import java.nio.file.Files;
 
public class ExcelRateOneTest {
 
    public static void main(String[] args) {
        File file = new File("D:\\年輕干部人才庫(kù)名冊(cè).xls");
        try {
            Workbook workbook = null;
            try {
                workbook = new XSSFWorkbook(Files.newInputStream(file.toPath()));
            } catch (Exception e) {
                System.out.println("該excel文件不是xlsx類型"+e.getMessage());
            }
            try {
                if (null == workbook) {
                    workbook = new HSSFWorkbook(Files.newInputStream(file.toPath()));
                }
            } catch (Exception e) {
                System.out.println("該文件不是excel文件"+e.getMessage());
            }
            if (workbook != null) {
                Sheet sheet = workbook.getSheetAt(0);
                Row row = sheet.getRow(0);
                Cell cell = row.getCell(0);
                System.out.println("單元格內(nèi)容:" + getCellValue(cell));
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
 
    /**
     * 根據(jù)單元格類型獲取單元格內(nèi)容,將內(nèi)容轉(zhuǎn)為字符串類型
     * @param cell 單元格
     * @return
     */
    public static String getCellValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                return cell.getNumericCellValue() + "";
            case BOOLEAN:
                return cell.getBooleanCellValue() + "";
            case FORMULA:
                return cell.getCellFormula() + "";
            default:
                return "";
        }
    }
}

知識(shí)補(bǔ)充

除了上文的內(nèi)容,小編還為大家整理了Java利用poi解析xlsx和xls文件的相關(guān)代碼,希望對(duì)大家有所幫助

遇到過(guò)處理excel的問(wèn)題,在網(wǎng)上找了很久,感覺(jué)他們的代碼太亂太復(fù)雜,這是我精簡(jiǎn)版的excel處理代碼,簡(jiǎn)單暴力。

首先,為什么使用poi?jxl只能處理03版之前的excel,也就是xls結(jié)尾的,不能處理xlsx。poi兼容兩種格式,poi解析兩種格式的文件時(shí),唯一的不同就是xls時(shí),為HSSF;xlsx時(shí),為XSSF。

首先是處理Cell對(duì)象的小方法:

//這個(gè)方法對(duì)cell進(jìn)行處理,傳入cell對(duì)象,返回cell中內(nèi)容,String類型。
public static String getCellFormatValue(Cell cell) {
		String cellValue = "";
		if (cell != null) {
			// 判斷cell類型
			switch (cell.getCellType()) {
			case Cell.CELL_TYPE_NUMERIC: {
				cellValue = String.valueOf(cell.getNumericCellValue());
				break;
			}
			case Cell.CELL_TYPE_STRING: {
				cellValue = cell.getRichStringCellValue().getString();
				break;
			}
			default:
				cellValue = "";
			}
		}
		return cellValue;
	}

兩種處理excel的代碼:

xls時(shí):

InputStream inputStream = new FileInputStream("絕對(duì)路徑.xls");
		HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
		HSSFSheet s = workbook.getSheetAt(0);
		for (int j = 0; j < s.getPhysicalNumberOfRows(); j++) {//獲取總行數(shù)
			Row row = s.getRow(j); // 取出第i行 getRow(index) 獲取第(j)行
			for (int k = 0; k < row.getPhysicalNumberOfCells(); k++) { // getPhysicalNumberOfCells() 獲取當(dāng)前行的總列數(shù)
				String value1 = getCellFormatValue(row.getCell(k));//取出第j行第k列的值
				System.out.println(value1);
			}
		}
		workbook.close();

xlsx時(shí):

InputStream inputStream = new FileInputStream("絕對(duì)路徑.xlsx");
		XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
		XSSFSheet s = workbook.getSheetAt(0);
		for (int j = 0; j < s.getPhysicalNumberOfRows(); j++) {//獲取總行數(shù)
			Row row = s.getRow(j); // 取出第i行 getRow(index) 獲取第(j)行
			for (int k = 0; k < row.getPhysicalNumberOfCells(); k++) { // getPhysicalNumberOfCells() 獲取當(dāng)前行的總列數(shù)
				String value1 = getCellFormatValue(row.getCell(k));//取出第j行第k列的值
				System.out.println(value1);
			}
		}
		workbook.close();

以上就是java poi判斷excel是xlsx還是xls類型的詳細(xì)內(nèi)容,更多關(guān)于java判斷excel是xlsx還是xls的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論