java poi读取Excel,怎么判断空行?寻高手

b770817895 2012-05-31 09:30:07
这几天正在做批量导入,遇到了这样个问题,就是Excel中的一行如果是空行,此空行是空格,不是没有数据,哪位大哥能指点指点该怎么判断?以下是读取代码:
private static List<List<Object>> read2003Excel(File file)
throws IOException {

try {
FileInputStream is = new FileInputStream(file);
HSSFWorkbook wbs = new HSSFWorkbook(is);
HSSFSheet childSheet = wbs.getSheetAt(1);// 此处可能有问题
int rowNum = childSheet.getLastRowNum() + 1;
// System.out.println(childSheet.getPhysicalNumberOfRows());
System.out.println("有行数" + rowNum);
for (int j = 1; j < rowNum; j++) {
HSSFRow row = childSheet.getRow(j);
// System.out.println(row.getPhysicalNumberOfCells());
// System.out.println("有列数" + row.getLastCellNum());
if (null != row) {
Map map = new HashMap();
for (int k = 0; k < row.getLastCellNum(); k++) {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
HSSFCell cell = row.getCell((short) k);
HSSFCell cell1 = row.getCell((short) 0);
System.out.println(cell1.toString());
if (null != cell) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
String s = String.valueOf(((long) cell
.getNumericCellValue()));
map.put(list.get(k), s);
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
map.put(list.get(k), cell.getStringCellValue());
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
map.put(list.get(k), " ");
}
} else {
System.out.print(" ");
}
}
System.out.println(map);
} else {
System.out.println("空");
}

}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

没分了,将就了
...全文
1505 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
reyoapple 2014-10-24
  • 打赏
  • 举报
回复
这个可能是你要的效果:http://www.cnblogs.com/interdrp/p/4019583.html
qianlifu12345 2013-09-03
  • 打赏
  • 举报
回复
急求方法,我也遇到了同样问题。package com.cargosmart.vec.Len; import java.io.*; import java.nio.channels.SelectableChannel; import java.util.Iterator; import java.util.Scanner; import javax.naming.directory.SearchResult; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDateUtil; 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.hssf.util.Region; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.Row; public class POISample{ @SuppressWarnings("deprecation") public static void main(String[] args){ FileInputStream in = null; HSSFWorkbook workbook = null; HSSFWorkbook workbooktarget=null; try{ in = new FileInputStream("C:\\spec_temp.xls");//将excel文件转为输入流 POIFSFileSystem fs = new POIFSFileSystem(in);//构建POIFSFileSystem类对象,用输入流构建 workbook = new HSSFWorkbook(fs);//创建个workbook,根据POIFSFileSystem对象 Scanner keyboardScanner=new Scanner(System.in); System.out.println("====================Workbook===================="); System.out.println("Number of Sheets:" + workbook.getNumberOfSheets()); System.out.println("Sheet3's name:" + workbook.getSheetName(0)); System.out.println(); System.out.println("input sheet name:"); String Searchsheet=null; Searchsheet=keyboardScanner.nextLine(); System.out.println("input value:"); String Searchvalue=null; Searchvalue=keyboardScanner.nextLine(); HSSFSheet sheet1=workbook.getSheet("VY03"); System.out.println(Searchsheet+Searchvalue); System.out.println(sheet1.getLastRowNum()); for (int i = 0; i < sheet1.getRow(2).getLastCellNum(); i++) { System.out.print(sheet1.getRow(2).getCell(i).getRichStringCellValue()+"\t\t"); } System.out.println(); int n=sheet1.getLastRowNum(); int rowflag=0; for (int i = 0; i < n; i++) { String s=""; HSSFRow row=sheet1.getRow(i); HSSFCell cell=row.getCell(1); if (null!=cell) { s = cell.getStringCellValue().trim(); System.out.println(s); if (s.equals( "Vessel Gross Ton")) { rowflag=i; System.out.println(rowflag); } } } for (int i = 0; i < sheet1.getRow(rowflag).getLastCellNum(); i++) { System.out.print(sheet1.getRow(rowflag).getCell(i).toString()+"\t\t"); } }catch(IOException e){ System.out.println(e.toString()); }finally{ try{ in.close(); }catch (IOException e){ System.out.println(e.toString()); } } FileOutputStream out = null; try{ out = new FileOutputStream("C:\\SDV_130812_1034_log_copy.xls"); workbook.write(out); }catch(IOException e){ System.out.println(e.toString()); }finally{ try { out.close(); }catch(IOException e){ System.out.println(e.toString()); } } } }

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧