[转] java使用poi.3.10读取excel 2010
- 摘要:packagepoi;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.Iterator;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel
- 标签:2010 excel 使用 Java
class="dp-j">
- package?poi;??
- import?java.io.FileInputStream;??
- import?java.io.IOException;??
- import?java.io.InputStream;??
- import?java.util.Iterator;??
- import?org.apache.poi.hssf.usermodel.HSSFCell;??
- 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;??
- ???
- public?class?ReadExcel001?{??
- ????public?static?void?main(String[]?args)?{??
- ????????readXml("D:/test.xlsx");??
- ????????System.out.println("-------------");??
- ????????readXml("d:/test2.xls");??
- ????}??
- ????public?static?void?readXml(String?fileName){??
- ????????boolean?isE2007?=?false;??????
- ????????if(fileName.endsWith("xlsx"))??
- ????????????isE2007?=?true;??
- ????????try?{??
- ????????????InputStream?input?=?new?FileInputStream(fileName);????
- ????????????Workbook?wb??=?null;??
- ??????????????
- ????????????if(isE2007)??
- ????????????????wb?=?new?XSSFWorkbook(input);??
- ????????????else??
- ????????????????wb?=?new?HSSFWorkbook(input);??
- ????????????Sheet?sheet?=?wb.getSheetAt(0);???????
- ????????????Iterator<Row>?rows?=?sheet.rowIterator();???
- ????????????while?(rows.hasNext())?{??
- ????????????????Row?row?=?rows.next();????
- ????????????????System.out.println("Row?#"?+?row.getRowNum());????
- ????????????????Iterator<Cell>?cells?=?row.cellIterator();??????
- ????????????????while?(cells.hasNext())?{??
- ????????????????????Cell?cell?=?cells.next();??
- ????????????????????System.out.println("Cell?#"?+?cell.getColumnIndex());??
- ????????????????????switch?(cell.getCellType())?{?????
- ????????????????????case?HSSFCell.CELL_TYPE_NUMERIC:??
- ????????????????????????System.out.println(cell.getNumericCellValue());??
- ????????????????????????break;??
- ????????????????????case?HSSFCell.CELL_TYPE_STRING:??
- ????????????????????????System.out.println(cell.getStringCellValue());??
- ????????????????????????break;??
- ????????????????????case?HSSFCell.CELL_TYPE_BOOLEAN:??
- ????????????????????????System.out.println(cell.getBooleanCellValue());??
- ????????????????????????break;??
- ????????????????????case?HSSFCell.CELL_TYPE_FORMULA:??
- ????????????????????????System.out.println(cell.getCellFormula());??
- ????????????????????????break;??
- ????????????????????default:??
- ????????????????????????System.out.println("unsuported?sell?type");??
- ????????????????????break;??
- ????????????????????}??
- ????????????????}??
- ????????????}??
- ????????}?catch?(IOException?ex)?{??
- ????????????ex.printStackTrace();??
- ????????}??
- ????}??
- }?
?