【POI】——获得单元格的值,并转化成字符串
2016-05-26 21:34
239 查看
本篇文章分享一些在做导入导出EXCEL功能时用到的工具类的一些代码。
如果直接使用
当单元格的格式为数字或其他格式时,这句代码就会报错,在开发时一定要注意。
/** * @param cell * @return */ public static String getStringValueFromCell(Cell cell) { SimpleDateFormat sFormat = new SimpleDateFormat("MM/dd/yyyy"); DecimalFormat decimalFormat = new DecimalFormat("#.#"); String cellValue = ""; if(cell == null) { return cellValue; } else if(cell.getCellType() == Cell.CELL_TYPE_STRING) { cellValue = cell.getStringCellValue(); } else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { if(HSSFDateUtil.isCellDateFormatted(cell)) { double d = cell.getNumericCellValue(); Date date = HSSFDateUtil.getJavaDate(d); cellValue = sFormat.format(date); } else { cellValue = decimalFormat.format((cell.getNumericCellValue())); } } else if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { cellValue = ""; } else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { cellValue = String.valueOf(cell.getBooleanCellValue()); } else if(cell.getCellType() == Cell.CELL_TYPE_ERROR) { cellValue = ""; } else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { cellValue = cell.getCellFormula().toString(); } return cellValue; }
如果直接使用
cell.getStringCellValue();
当单元格的格式为数字或其他格式时,这句代码就会报错,在开发时一定要注意。
相关文章推荐
- 单线程爬虫
- Basic Calculator
- Linux下的TCP/IP编程----基础篇
- PHP 算术运算符
- table的bottom无端多条边线
- service
- Deviceone:站在移动互联时代的十字路口上
- java中使用 正则 抓取邮箱
- 队列及栈相关题目的实现
- Android RecyclerView 实现瀑布流效果
- 输入(0,400,300)(地图名,x坐标,y坐标)这样的字符串数据,按照','分隔符提取,分别放到char [10],int a,int b中
- Latex-tikz
- 剑指Offer之用两个栈实现队列(题7)
- 使用IDEA部署Myeclipse项目
- Set的原理浅析 HashSet, TreeSet(NavigableMap)
- 我是如何向老婆解释MapReduce的?
- 团队加分作业
- Nginx-进程模型
- 多态(Java)
- 找一个温暖的爱人有多重要