ReadExcel

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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.ss.usermodel.WorkbookFactory;


public class ReadExcel {

public static void main(String[] args) {
defaultRead();
customerRead();
}

/**
* 新风格
*/
private static void defaultRead() {
// 根据输入流创建Workbook对象
Workbook wb = null;
InputStream is = null;
try {
is = new FileInputStream(new File("c://SummaryHSSF.xls"));
wb = WorkbookFactory.create(is);
// get到Sheet对象
Sheet sheet = wb.getSheetAt(0);
// 这个必须用接口
for (Row row : sheet) {
for (Cell cell : row) {
// cell.getCellType是获得cell里面保存的值的type
// 如Cell.CELL_TYPE_STRING
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
// 得到Boolean对象的方法
System.out.print(cell.getBooleanCellValue() + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
// 先看是否是日期格式
if (DateUtil.isCellDateFormatted(cell)) {
// 读取日期格式
System.out.print(cell.getDateCellValue() + " ");
} else {
// 读取数字
System.out.print(cell.getNumericCellValue() + " ");
}
break;
case Cell.CELL_TYPE_FORMULA:
// 读取公式
System.out.print(cell.getCellFormula() + " ");
break;
case Cell.CELL_TYPE_STRING:
// 读取String
System.out.print(cell.getRichStringCellValue()
.toString() + " ");
break;
}
}
System.out.println("");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 传统风格
*/
private static void customerRead() {
InputStream is = null;
try {
is = new FileInputStream(new File("c://SummaryHSSF.xls"));
// 根据输入流创建workbok对象
Workbook wb = WorkbookFactory.create(is);
Sheet sheet = wb.getSheetAt(0);// 获取工作薄中的第一个工作表单
for (Iterator<Row> rit = sheet.iterator(); rit.hasNext();) {
Row row = (Row) rit.next();
for (Iterator<Cell> cit = row.iterator(); cit.hasNext();) {
Cell cell = (Cell) cit.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + " ");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + " ");
break;
case Cell.CELL_TYPE_FORMULA:
System.out.print(cell.getCellFormula() + " ");
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)){
System.out.print(cell.getDateCellValue()+" ");
}else{
System.out.print(cell.getNumericCellValue()+" ");
}
break;
}

}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值