java读取excel

例子:spring

org.springframework.core.io.Resource resource = new ClassPathResource("");
JSONObject json = new JSONObject();
//获取excel对象
XSSFWorkbook xssfWorkbook = null;
try {
  Properties p = PropertiesLoaderUtils.loadProperties(resource);
  //获取excel对象
  xssfWorkbook = new XSSFWorkbook(new FileInputStream(p.getProperty("")));
  //用于拼接全部列名
  StringBuffer names= new StringBuffer();
  //开始解析excel的数据
  XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
  for(int rowNum = 1 ; rowNum <= xssfSheet.getLastRowNum() ; rowNum ++){//循环遍历行数
    XSSFRow xssfRow = xssfSheet.getRow(rowNum);//获得每一行
    XSSFCell xssfCell= xssfRow.getCell(0);;//获得每一行的每一列
    if (StringUtils.isNotBlank(xssfCell.toString()))
    {
      if (rowNum == xssfSheet.getLastRowNum())
      {
        names.append("'"+xssfCell.toString()+"'");
      }
    else
    {
      names.append("'"+xssfCell.toString()+"'");
      names.append(",");
    }
  } json

}app

注:String value = cell.getRichStringCellValue().getString();//获得每一列的值不论是什么类型都转换成字符串xss

相关文章
相关标签/搜索