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();
}
}
}
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();
}
}
}