ExcelUtils.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package org.maxkey.util;
  2. import java.text.DecimalFormat;
  3. import java.text.SimpleDateFormat;
  4. import org.apache.poi.ss.usermodel.Cell;
  5. import org.apache.poi.ss.usermodel.CellType;
  6. import org.apache.poi.ss.usermodel.Row;
  7. public class ExcelUtils {
  8. /**
  9. * 根据数据格式返回数据
  10. *
  11. * @param cell
  12. * @return
  13. */
  14. public static String getValue(Cell cell) {
  15. if (cell == null) {
  16. return "";
  17. } else if (cell.getCellType() == CellType.BOOLEAN) {
  18. return String.valueOf(cell.getBooleanCellValue());
  19. } else if (cell.getCellType() == CellType.NUMERIC) {
  20. if ("General".equals(cell.getCellStyle().getDataFormatString())) {
  21. return new DecimalFormat("0").format(cell.getNumericCellValue());
  22. } else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) {
  23. return new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
  24. } else {
  25. return new DecimalFormat("0").format(cell.getNumericCellValue());
  26. }
  27. } else {
  28. return String.valueOf(cell.getStringCellValue().trim());
  29. }
  30. }
  31. public static String getValue(Row row,int i) {
  32. return getValue(row.getCell(i));
  33. }
  34. }