例子: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