Java按行读取Excel内容

ゝ一纸荒年。 2022-05-29 02:17 443阅读 0赞

一. Java对Excel文件读取,通常用poi包来实现

二.代码如下

public class ExcelUtils {

public static final String OFFICE_EXCEL_XLS = “xls”;

  1. public static final String OFFICE\_EXCEL\_XLSX = "xlsx";
  2. /\*\*
  3. \* 读取指定Sheet也的内容
  4. \* @param filepath filepath 文件全路径
  5. \* @param sheetNo sheet序号,从0开始,如果读取全文sheetNo设置null
  6. \*/
  7. public static String readExcel(String filepath, Integer sheetNo)
  8. throws EncryptedDocumentException, InvalidFormatException, IOException \{
  9. StringBuilder sb = new StringBuilder();
  10. Workbook workbook = getWorkbook(filepath);
  11. if (workbook != null) \{
  12. if (sheetNo == null) \{
  13. int numberOfSheets = workbook.getNumberOfSheets();
  14. for (int i = 0; i < numberOfSheets; i++) \{
  15. Sheet sheet = workbook.getSheetAt(i);
  16. if (sheet == null) \{
  17. continue;
  18. \}
  19. sb.append(readExcelSheet(sheet));
  20. \}
  21. \} else \{
  22. Sheet sheet = workbook.getSheetAt(sheetNo);
  23. if (sheet != null) \{
  24. sb.append(readExcelSheet(sheet));
  25. \}
  26. \}
  27. \}
  28. return sb.toString();
  29. \}
  30. /\*\*
  31. \* 根据文件路径获取Workbook对象
  32. \* @param filepath 文件全路径
  33. \*/
  34. public static Workbook getWorkbook(String filepath)
  35. throws EncryptedDocumentException, InvalidFormatException, IOException \{
  36. InputStream is = null;
  37. Workbook wb = null;
  38. if (StringUtils.isBlank(filepath)) \{
  39. throw new IllegalArgumentException("文件路径不能为空");
  40. \} else \{
  41. String suffiex = getSuffiex(filepath);
  42. if (StringUtils.isBlank(suffiex)) \{
  43. throw new IllegalArgumentException("文件后缀不能为空");
  44. \}
  45. if (OFFICE\_EXCEL\_XLS.equals(suffiex) || OFFICE\_EXCEL\_XLSX.equals(suffiex)) \{
  46. try \{
  47. is = new FileInputStream(filepath);
  48. wb = WorkbookFactory.create(is);
  49. \} finally \{
  50. if (is != null) \{
  51. is.close();
  52. \}
  53. if (wb != null) \{
  54. wb.close();
  55. \}
  56. \}
  57. \} else \{
  58. throw new IllegalArgumentException("该文件非Excel文件");
  59. \}
  60. \}
  61. return wb;
  62. \}
  63. /\*\*
  64. \* 获取后缀
  65. \* @param filepath filepath 文件全路径
  66. \*/
  67. private static String getSuffiex(String filepath) \{
  68. if (StringUtils.isBlank(filepath)) \{
  69. return "";
  70. \}
  71. int index = filepath.lastIndexOf(".");
  72. if (index == -1) \{
  73. return "";
  74. \}
  75. return filepath.substring(index + 1, filepath.length());
  76. \}

//返回的内容用#按行分隔,便于在后台解析字符串
private static String readExcelSheet(Sheet sheet) {
StringBuilder sb = new StringBuilder();
if(sheet != null){
int rowNos = sheet.getLastRowNum();// 得到excel的总记录条数
for (int i = 0; i <= rowNos; i++) {// 遍历行
Row row = sheet.getRow(i);
if(row != null){
int columNos = row.getLastCellNum();// 表头总共的列数
for (int j = 0; j < columNos; j++) {
Cell cell = row.getCell(j);
if(cell != null){
cell.setCellType(CellType.STRING);
if(j == (columNos-1)){
sb.append(cell.getStringCellValue());
}else{
sb.append(cell.getStringCellValue() + “ “);
}
}
}
}
sb.append(“#“);
}
}
return sb.toString();

  1. \}

}

发表评论

表情:
评论列表 (有 0 条评论,443人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java读取文件内容

    java中按行来读取文件内容,一般对文件也是又要求的,比如文件编码utf-8,内容是按行可读,而不是一堆字节码。这类文件,我们按行读取,主要是方便快速查看内容,并且用这些内容来