例子:
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(",");
}
}
}
注:String value = cell.getRichStringCellValue().getString();//得到每一列的值不管是什么类型都转换成字符串