java读取xlsx_Java读取xls和xlsx文件的实现

该博客介绍如何使用Apache POI库在Java中读取xls和xlsx文件。提供了两个静态方法,readXls和readXlsx,分别处理两种类型的Excel文件,通过迭代行和列,根据单元格类型打印出相应内容。
摘要由CSDN通过智能技术生成

运行本程序,需要类库poi-3.7,dom4j和xmlbeans的支持。

代码分享:import java.io.*;

import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;

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.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

class ExcelReader

{

static void readXlsx(File inputFile)

{

try

{

// Get the workbook instance for XLSX file

XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));

// Get first sheet from the workbook

XSSFSheet sheet = wb.getSheetAt(0);

Row row;

Cell cell;

// Iterate through each rows from first sheet

Iterator rowIterator = sheet.iterator();

while (rowIterator.hasNext())

{

row = rowIterator.next();

// For each row, iterate through each columns

Iterator cellIterator = row.cellIterator();

while (cellIterator.hasNext())

{

cell = cellIterator.next();

switch (cell.getCellType())

{

case Cell.CELL_TYPE_BOOLEAN:

System.out.println(cell.getBooleanCellValue());

break;

case Cell.CELL_TYPE_NUMERIC:

System.out.println(cell.getNumericCellValue());

break;

case Cell.CELL_TYPE_STRING:

System.out.println(cell.getStringCellValue());

break;

case Cell.CELL_TYPE_BLANK:

System.out.println(" ");

break;

default:

System.out.println(cell);

}

}

}

}

catch (Exception e)

{

System.err.println("Exception :" + e.getMessage());

}

}

static void readXls(File inputFile)

{

try

{

// Get the workbook instance for XLS file

HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));

// Get first sheet from the workbook

HSSFSheet sheet = workbook.getSheetAt(0);

Cell cell;

Row row;

// Iterate through each rows from first sheet

Iterator rowIterator = sheet.iterator();

while (rowIterator.hasNext())

{

row = rowIterator.next();

// For each row, iterate through each columns

Iterator cellIterator = row.cellIterator();

while (cellIterator.hasNext())

{

cell = cellIterator.next();

switch (cell.getCellType())

{

case Cell.CELL_TYPE_BOOLEAN:

System.out.println(cell.getBooleanCellValue());

break;

case Cell.CELL_TYPE_NUMERIC:

System.out.println(cell.getNumericCellValue());

break;

case Cell.CELL_TYPE_STRING:

System.out.println(cell.getStringCellValue());

break;

case Cell.CELL_TYPE_BLANK:

System.out.println(" ");

break;

default:

System.out.println(cell);

}

}

}

}

catch (FileNotFoundException e)

{

System.err.println("Exception" + e.getMessage());

}

catch (IOException e)

{

System.err.println("Exception" + e.getMessage());

}

}

public static void main(String[] args)

{

File inputFile = new File("C:\input.xls");

File inputFile2 = new File("C:\input.xlsx");

readXls(inputFile);

readXlsx(inputFile2);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值