JAVA读取Excel--用POI与Excel交互

来源:互联网 发布:mac用户名怎么改 编辑:程序博客网 时间:2024/06/11 19:09
package com.golden.test;import java.io.File;import java.io.FileInputStream;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;/** *  *  */public class PoiReadXls2 {public static void main(String[] args) {File f = new File("c:\\a.xls");try {FileInputStream is = new FileInputStream(f);HSSFWorkbook wbs = new HSSFWorkbook(is);HSSFSheet childSheet = wbs.getSheetAt(0);// System.out.println(childSheet.getPhysicalNumberOfRows());System.out.println("有行数" + childSheet.getLastRowNum());for (int j = 0; j < childSheet.getLastRowNum(); j++) {HSSFRow row = childSheet.getRow(j);// System.out.println(row.getPhysicalNumberOfCells());// System.out.println("有列数" + row.getLastCellNum());if (null != row) {for (int k = 0; k < row.getLastCellNum(); k++) {HSSFCell cell = row.getCell(k);if (null != cell) {switch (cell.getCellType()) {case HSSFCell.CELL_TYPE_NUMERIC: // 数字System.out.print(cell.getNumericCellValue()+ "   ");break;case HSSFCell.CELL_TYPE_STRING: // 字符串System.out.print(cell.getStringCellValue()+ "   ");break;case HSSFCell.CELL_TYPE_BOOLEAN: // BooleanSystem.out.println(cell.getBooleanCellValue()+ "   ");break;case HSSFCell.CELL_TYPE_FORMULA: // 公式System.out.print(cell.getCellFormula() + "   ");break;case HSSFCell.CELL_TYPE_BLANK: // 空值System.out.println(" ");break;case HSSFCell.CELL_TYPE_ERROR: // 故障System.out.println(" ");break;default:System.out.print("未知类型   ");break;}} else {System.out.print("-   ");}}}System.out.println();}} catch (Exception e) {e.printStackTrace();}}}


 

原创粉丝点击