Skip to content

Instantly share code, notes, and snippets.

@codethereforam
Last active January 11, 2019 09:02
Show Gist options
  • Save codethereforam/499b8b64af60169d4cdfeed654bece6f to your computer and use it in GitHub Desktop.
Save codethereforam/499b8b64af60169d4cdfeed654bece6f to your computer and use it in GitHub Desktop.
POI读取Excel单元格工具。由于读取时间、数值等存在精度等坑,因此约定Excel单元格只能为文本格式,否则报错。
/**
* Excel工具类
*
* @author yanganyu
* @date 2019/1/11 11:42
*/
public class ExcelUtils {
private ExcelUtils() {
}
/**
* 获取类型为"String"的单元格的值
* <p>
* 如果获取errorMsg是字符串拼接等其他耗性能操作,慎用该方法
* </p>
* <ul>
* <li>cell == null, return ""</li>
* <li>cell类型不是Cell.CELL_TYPE_STRING,并且不是Cell.CELL_TYPE_BLANK,throw BusinessException</li>
* </ul>
*
* @param cell 单元格
* @param errorMsg 错误信息
* @return trim操作后的单元格的值
* @author yanganyu
*/
public static String getTrimStringCellValue(Cell cell, String errorMsg) {
if (cell == null) {
return "";
}
CellType cellType = cell.getCellTypeEnum();
if (cellType != CellType.STRING && cellType != CellType.BLANK) {
throw new BusinessException(errorMsg);
}
return cell.getStringCellValue().trim();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment