1, 对于类似电话号码或手机一类的大数值读取问题
// 取值后会带一个E的问题 double cellValue = row.getCell(k).getNumericCellValue(); fieldValues[k] = new DecimalFormat("#").format(cellValue);
case HSSFCell.CELL_TYPE_NUMERIC: // 数值型 if (HSSFDateUtil.isCellDateFormatted(cell)) { // 如果是date类型则 ,获取该cell的date值 value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString(); } else { // 纯数字 value = String.valueOf(cell.getNumericCellValue()); }
Java代码 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; /** * * @author 崔素强 * */ 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: // Boolean System.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(); } } } 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; /** * * @author 崔素强 * */ 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: // Boolean System.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(); } } }
得到Excel的文件然后读取,这个很简单。关键有两个地方,也许在网上会看到有的这样使用有的那样使用。
case HSSFCell.CELL_TYPE_NUMERIC: // 数值型 if (HSSFDateUtil.isCellDateFormatted(cell)) { // 如果是date类型则 ,获取该cell的date值 value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString(); } else { // 纯数字 value = String.valueOf(cell.getNumericCellValue()); } case HSSFCell.CELL_TYPE_NUMERIC: // 数值型 if (HSSFDateUtil.isCellDateFormatted(cell)) { // 如果是date类型则 ,获取该cell的date值 value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString(); } else { // 纯数字 value = String.valueOf(cell.getNumericCellValue()); }
package com.golden.test; import java.io.File; 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.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** * * @author 崔素强 * */ public class PoiReadXls { @SuppressWarnings( { "unchecked", "deprecation" }) public static void main(String[] args) { File f = new File("c:\\a.xls"); try { InputStream input = new FileInputStream(f); POIFSFileSystem fs = new POIFSFileSystem(input); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); Iterator rows = sheet.rowIterator(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); // System.out.print("行:" + row.getRowNum() + " "); Iterator cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); // System.out.println("列:" + cell.getCellNum()); 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: // Boolean System.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; } } System.out.println(); } } catch (IOException ex) { ex.printStackTrace(); } } } package com.golden.test; import java.io.File; 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.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** * * @author 崔素强 * */ public class PoiReadXls { @SuppressWarnings( { "unchecked", "deprecation" }) public static void main(String[] args) { File f = new File("c:\\a.xls"); try { InputStream input = new FileInputStream(f); POIFSFileSystem fs = new POIFSFileSystem(input); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); Iterator rows = sheet.rowIterator(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); // System.out.print("行:" + row.getRowNum() + " "); Iterator cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); // System.out.println("列:" + cell.getCellNum()); 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: // Boolean System.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; } } System.out.println(); } } catch (IOException ex) { ex.printStackTrace(); } } }
原文章地址:http://jammy-zhou.iteye.com/blog/850240
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow