poi 3.10 读取excel(2003/2007)
2014-03-09 11:03
302 查看
public class TestExcel { private static String filePath = ""; private static String version = "2003";//默认为2003版本 private static Workbook wb = null; private static Sheet sheet = null; static User user = new User(); public static void setVersion(String filePath){ if(filePath.endsWith(".xlsx")){ version = "2007"; } } public static Workbook getWorkbook() throws IOException, InvalidFormatException{ BufferedInputStream in = new BufferedInputStream(new FileInputStream(filePath)); if("2003".equals(version)){ POIFSFileSystem fs = new POIFSFileSystem(in); wb = (Workbook)new HSSFWorkbook(fs); }else if("2007".equals(version)){ wb = (Workbook)new XSSFWorkbook(in); } return wb; } public static void print(){ try { wb = getWorkbook(); int sheets = wb.getNumberOfSheets();//获取sheet的数量 // System.out.println(sheets); sheet = wb.getSheetAt(0);//取第一个sheet for(int i = sheet.getFirstRowNum()+1;i<sheet.getPhysicalNumberOfRows();i++){ Row row = sheet.getRow(i); for(int j = row.getFirstCellNum();j<row.getPhysicalNumberOfCells();j++){ Cell cell = row.getCell(j); // System.out.println(cell.getCellStyle());//样式 // cell.getCellType();//获得类型 /** * CellType 类型 值 * CELL_TYPE_NUMERIC 数值型 0 * CELL_TYPE_STRING 字符串型 1 * CELL_TYPE_FORMULA 公式型 2 * CELL_TYPE_BLANK 空值 3 * CELL_TYPE_BOOLEAN 布尔型 4 * CELL_TYPE_ERROR 错误 5 * * * */ Object content = ""; if(cell.getCellType() == cell.CELL_TYPE_BLANK){ content = ""; }else if(cell.getCellType() == cell.CELL_TYPE_NUMERIC){ content = cell.getNumericCellValue(); }else if(cell.getCellType() == cell.CELL_TYPE_STRING){ content = cell.getStringCellValue(); }else if(cell.getCellType() == cell.CELL_TYPE_BOOLEAN){ content = cell.getBooleanCellValue(); } System.out.println(content); } } } catch (IOException e) { System.out.println("读取文件失败。"); e.printStackTrace(); } catch (InvalidFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { filePath = "d:"+File.separator+"workbook.xlsx"; setVersion(filePath); print(); }
相关文章推荐
- 通过POI统一读取Excel文件(兼容97-2003和2007+两种格式)
- poi 读取excel 2003 及 2007 精简版
- java的poi技术读取Excel[2003-2007,2010]
- JAVA用POI读取和创建2003和2007版本Excel
- Java POI读取Excel 2003/2007/2010例子
- poi读取EXCEL工具,兼容2003以下和2007
- struts:poi读取excel文件(兼容2003、2007)
- java的poi技术读取Excel[2003-2007,2010]
- Java POI读取Excel 2003/2007/2010例子
- JAVA用POI读取和创建2003和2007版本Excel完美示例
- java POI读取excel 2007/2003
- poi 读取文件夹内的excel ,兼容2003和2007
- java的poi技术读取Excel[2003-2007,2010]
- Java的poi技术读取Excel[2003-2007,2010]
- java的poi技术读取Excel[2003-2007,2010]
- Java中通过POI读取Excel 2003 - 2007的xls,xlsx格式
- Java POI 读取Office excel (2003,2007)及相关jar包
- java的poi技术读取Excel[2003-2007,2010]
- java中用poi处理Excel表格之读取(2003和2007通用方法)
- Java的poi技术读取Excel[2003-2007,2010]