• poi导出excel设置对应格式


    1. public static List dataConversion( XSSFSheet sheet ){
    2. //便利Sheet标签页,获得每一行数
    3. int lastRowNum = sheet.getLastRowNum();
    4. ArrayList rows = new ArrayList<>();
    5. for(int i=0;i<=lastRowNum;i++) {
    6. if(i==0) continue;
    7. XSSFRow row = sheet.getRow(i);
    8. //获取当前行最后一个单元格索引
    9. short lastCellNum = row.getLastCellNum();
    10. HashMap<Integer, String> lies = new HashMap<Integer, String>();
    11. for (int j=0;j
    12. //根据单元格索引获取行
    13. XSSFCell cell = row.getCell(j);
    14. lies.put(j,getCellFormatValue(cell));
    15. // System.out.println(cell);
    16. }
    17. rows.add(lies);
    18. }
    19. return rows;
    20. }
    21. /**
    22. * 获取单元格格式化的值
    23. * @param cell 单元格
    24. * @return
    25. */
    26. public static String getCellFormatValue(Cell cell) {
    27. if (cell == null)
    28. return "";
    29. String cellvalue = "";
    30. // 判断当前单元格的type
    31. switch (cell.getCellType()) {
    32. case STRING:
    33. // /取得当前Cell的字符串
    34. cellvalue = cell.getRichStringCellValue().getString();
    35. break;
    36. // 如果当前Cell的type为NUMERIC或者_FORMULA
    37. case NUMERIC:
    38. case FORMULA:
    39. // 判断当前的Cell是否为Date
    40. if (DateUtil.isCellDateFormatted(cell)) {
    41. // 如果是在Date类型,则取得该Cell的Date值
    42. Date date = cell.getDateCellValue();
    43. //格式转换
    44. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    45. String format = sdf.format(date);
    46. //System.out.println(format);
    47. } else {
    48. //纯数字
    49. //区分整数与小数
    50. Double aDouble = cell.getNumericCellValue();
    51. if (aDouble == aDouble.intValue()) {
    52. cellvalue = aDouble.intValue() + "";
    53. } else {
    54. cellvalue = aDouble + "";
    55. }
    56. }
    57. break;
    58. case BOOLEAN:
    59. cellvalue = String.valueOf(cell.getBooleanCellValue());
    60. break;
    61. default:
    62. cellvalue = "";
    63. }
    64. return cellvalue;
    65. }
    pom文件:
    
        org.apache.poi
        poi
        4.0.1
    
    
        org.apache.poi
        poi-ooxml
        4.0.1
    
  • 相关阅读:
    分布式事务
    window.open 打开后全屏
    seata框架的atomic.jar包做什么用的
    leetcode 1586 二叉搜索树迭代器 II 与 173. 二叉搜索树迭代器
    bindtap 和 catchtap 的区别以及如何使用
    List集合
    2022年好用的电容笔有哪些?口碑好的ipad电容笔推荐
    谈谈多线程与多线程同步
    解决QT中文乱码
    margin塌陷和margin重合问题的解决方法总结
  • 原文地址:https://blog.csdn.net/weixin_42081778/article/details/126298402