//检查excel中解析的数据格式
private static String getCellValue(HSSFCell cell){ String value = null;
if(cell==null)//如果最后两列值为空,此时cell.getCellType()会报 {
空指针异常,令其返回值为空 value = \; return value;
}
//简单的查检列类型 else {
switch(cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING://字符串
value = cell.getRichStringCellValue().getString(); break;
case HSSFCell.CELL_TYPE_NUMERIC://数字
long dd = (long)cell.getNumericCellValue(); value = dd+\; break;
case HSSFCell.CELL_TYPE_BLANK: value = \; break;
case HSSFCell.CELL_TYPE_FORMULA:
value = String.valueOf(cell.getCellFormula()); break;
case HSSFCell.CELL_TYPE_BOOLEAN://boolean型值
value = String.valueOf(cell.getBooleanCellValue()); break;
case HSSFCell.CELL_TYPE_ERROR:
value = String.valueOf(cell.getErrorCellValue()); break; default: break; }
return value; }
}
//检查excel中解析的数据格式
private static String getCellValue2007(XSSFCell cell){ String value = null;
if(cell==null)//如果最后两列值为空,此时 {
cell.getCellType()会报空指针异常,令其返回值为空
value = \; return value;
}
//简单的查检列类型 else {
switch(cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING://字符串 value = break;
case XSSFCell.CELL_TYPE_NUMERIC://数字
long dd = (long)cell.getNumericCellValue(); value = dd+\; break;
case XSSFCell.CELL_TYPE_BLANK: value = \; break;
case XSSFCell.CELL_TYPE_FORMULA:
value = String.valueOf(cell.getCellFormula()); break;
case XSSFCell.CELL_TYPE_BOOLEAN://boolean型值 value = break;
case XSSFCell.CELL_TYPE_ERROR: value =
break;
cell.getRichStringCellValue().getString();
String.valueOf(cell.getBooleanCellValue());
String.valueOf(cell.getErrorCellValue());
{
default: break; }
return value; } }
private void importExcel2003(InputStream fis) throws IOException
//创建Excel工作薄
HSSFWorkbook hwb = new HSSFWorkbook(fis); //得到第一个工作表
HSSFSheet sheet = hwb.getSheetAt(0); HSSFRow row = null; CarInfoPrint cip=null;
//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
for(int i = 0; i < hwb.getNumberOfSheets(); i++) { sheet = hwb.getSheetAt(i);
//遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 for(int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) { row = sheet.getRow(j); //第0行为表头,获取第一行开始 cip = new CarInfoPrint();
//此方法调用getCellValue(HSSFCell cell)对解析出来的数据进
行判断,并做相应的处理
/* if(getCellValue(row.getCell(0)) != null } */
&& !\
cip.setCarNo(Long.valueOf(getCellValue(row.getCell(0))));
cip.setMainId(getCellValue(row.getCell(0))); cip.setCarNo(getCellValue(row.getCell(1))); cip.setBelongTo(getCellValue(row.getCell(2))); cip.setSex(getCellValue(row.getCell(3))); cip.setDepartment(getCellValue(row.getCell(4)));
cip.setPhone(getCellValue(row.getCell(5))); cip.setSign(getCellValue(row.getCell(6)));
cip.setCarType(getCellValue(row.getCell(7))); cip.setUsedBy(getCellValue(row.getCell(8)));
/* if(getCellValue(row.getCell(4)) != null try {
cip.setDate(ft.parse(getCellValue(row.getCell(4)))); } catch (ParseException e) { e.printStackTrace(); } } */
try {
} catch (Exception e) { }
// TODO Auto-generated catch block e.printStackTrace();
&& !\
carInfoPrintService.insertCarInfoPrint(cip);
// infos.add(cip); } } }
private void importExcel2007(InputStream fis) throws IOException {
//创建Excel工作薄
Workbook hwb=null;
hwb = new XSSFWorkbook(fis); //得到第一个工作表
XSSFSheet sheet = (XSSFSheet )hwb.getSheetAt(0); XSSFRow row = null; CarInfoPrint cip=null;
//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
for(int i = 0; i < hwb.getNumberOfSheets(); i++) { sheet = (XSSFSheet) hwb.getSheetAt(i);
//遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 for(int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) { row = sheet.getRow(j); //第0行为表头,获取第一行开始
相关推荐: