POI 单元格类型CellType

x33g5p2x  于2022-04-02 转载在 其他  
字(6.1k)|赞(0)|评价(0)|浏览(651)

1. 单元格类型

单元格的内容决定了单元格的类型,POI中定义的7种单元格类型:

  1. 日期数据对应的单元格类型是CellType.NUMERIC,默认以浮点型数显示,显示为日期格式需要设置单元格样式DataFormat
  2. 字符型单元格内容也可以为富文本RichTextString,可以对文本多部分设置字体Font

2. 错误单元格

Excel中存在错误单元格,在POI中是怎么表现的呢

  1. org.apache.poi.ss.usermodel.FormulaError1
  1. package org.apache.poi.ss.usermodel;
  2. import java.util.Map;
  3. import org.apache.poi.util.Internal;
  4. import java.util.HashMap;
  5. /**
  6. * Enumerates error values in SpreadsheetML formula calculations.
  7. *
  8. * See also OOO's excelfileformat.pdf (2.5.6)
  9. */
  10. public enum FormulaError {
  11. @Internal
  12. _NO_ERROR(-1, "(no error)"),
  13. /**
  14. * Intended to indicate when two areas are required to intersect, but do not.
  15. * <p>Example:
  16. * In the case of SUM(B1 C1), the space between B1 and C1 is treated as the binary
  17. * intersection operator, when a comma was intended. end example]
  18. * </p>
  19. */
  20. NULL(0x00, "#NULL!"),
  21. /**
  22. * Intended to indicate when any number, including zero, is divided by zero.
  23. * Note: However, any error code divided by zero results in that error code.
  24. */
  25. DIV0(0x07, "#DIV/0!"),
  26. /**
  27. * Intended to indicate when an incompatible type argument is passed to a function, or
  28. * an incompatible type operand is used with an operator.
  29. * <p>Example:
  30. * In the case of a function argument, text was expected, but a number was provided
  31. * </p>
  32. */
  33. VALUE(0x0F, "#VALUE!"),
  34. /**
  35. * Intended to indicate when a cell reference is invalid.
  36. * <p>Example:
  37. * If a formula contains a reference to a cell, and then the row or column containing that cell is deleted,
  38. * a #REF! error results. If a worksheet does not support 20,001 columns,
  39. * OFFSET(A1,0,20000) will result in a #REF! error.
  40. * </p>
  41. */
  42. REF(0x17, "#REF!"),
  43. /**
  44. * Intended to indicate when what looks like a name is used, but no such name has been defined.
  45. * <p>Example:
  46. * XYZ/3, where XYZ is not a defined name. Total is & A10,
  47. * where neither Total nor is is a defined name. Presumably, "Total is " & A10
  48. * was intended. SUM(A1C10), where the range A1:C10 was intended.
  49. * </p>
  50. */
  51. NAME(0x1D, "#NAME?"),
  52. /**
  53. * Intended to indicate when an argument to a function has a compatible type, but has a
  54. * value that is outside the domain over which that function is defined. (This is known as
  55. * a domain error.)
  56. * <p>Example:
  57. * Certain calls to ASIN, ATANH, FACT, and SQRT might result in domain errors.
  58. * </p>
  59. * Intended to indicate that the result of a function cannot be represented in a value of
  60. * the specified type, typically due to extreme magnitude. (This is known as a range
  61. * error.)
  62. * <p>Example: FACT(1000) might result in a range error. </p>
  63. */
  64. NUM(0x24, "#NUM!"),
  65. /**
  66. * Intended to indicate when a designated value is not available.
  67. * <p>Example:
  68. * Some functions, such as SUMX2MY2, perform a series of operations on corresponding
  69. * elements in two arrays. If those arrays do not have the same number of elements, then
  70. * for some elements in the longer array, there are no corresponding elements in the
  71. * shorter one; that is, one or more values in the shorter array are not available.
  72. * </p>
  73. * This error value can be produced by calling the function NA
  74. */
  75. NA(0x2A, "#N/A"),
  76. // These are POI-specific error codes
  77. // It is desirable to make these (arbitrary) strings look clearly different from any other
  78. // value expression that might appear in a formula. In addition these error strings should
  79. // look unlike the standard Excel errors. Hence tilde ('~') was used.
  80. /**
  81. * POI specific code to indicate that there is a circular reference
  82. * in the formula
  83. */
  84. CIRCULAR_REF(0xFFFFFFC4, "~CIRCULAR~REF~"),
  85. /**
  86. * POI specific code to indicate that the funcition required is
  87. * not implemented in POI
  88. */
  89. FUNCTION_NOT_IMPLEMENTED(0xFFFFFFE2, "~FUNCTION~NOT~IMPLEMENTED~");
  90. private final byte type;
  91. private final int longType;
  92. private final String repr;
  93. private FormulaError(int type, String repr) {
  94. this.type = (byte)type;
  95. this.longType = type;
  96. this.repr = repr;
  97. }
  98. /**
  99. * @return numeric code of the error
  100. */
  101. public byte getCode() {
  102. return type;
  103. }
  104. /**
  105. * @return long (internal) numeric code of the error
  106. */
  107. public int getLongCode() {
  108. return longType;
  109. }
  110. /**
  111. * @return string representation of the error
  112. */
  113. public String getString() {
  114. return repr;
  115. }
  116. private static final Map<String, FormulaError> smap = new HashMap<String, FormulaError>();
  117. private static final Map<Byte, FormulaError> bmap = new HashMap<Byte, FormulaError>();
  118. private static final Map<Integer, FormulaError> imap = new HashMap<Integer, FormulaError>();
  119. static{
  120. for (FormulaError error : values()) {
  121. bmap.put(error.getCode(), error);
  122. imap.put(error.getLongCode(), error);
  123. smap.put(error.getString(), error);
  124. }
  125. }
  126. public static final boolean isValidCode(int errorCode) {
  127. for (FormulaError error : values()) {
  128. if (error.getCode() == errorCode) return true;
  129. if (error.getLongCode() == errorCode) return true;
  130. }
  131. return false;
  132. }
  133. public static FormulaError forInt(byte type) throws IllegalArgumentException {
  134. FormulaError err = bmap.get(type);
  135. if(err == null) throw new IllegalArgumentException("Unknown error type: " + type);
  136. return err;
  137. }
  138. public static FormulaError forInt(int type) throws IllegalArgumentException {
  139. FormulaError err = imap.get(type);
  140. if(err == null) err = bmap.get((byte)type);
  141. if(err == null) throw new IllegalArgumentException("Unknown error type: " + type);
  142. return err;
  143. }
  144. public static FormulaError forString(String code) throws IllegalArgumentException {
  145. FormulaError err = smap.get(code);
  146. if(err == null) throw new IllegalArgumentException("Unknown error code: " + code);
  147. return err;
  148. }
  149. }

3. 实例

  1. package hssf.sheet.cell;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  9. import org.apache.poi.ss.usermodel.CellType;
  10. import org.apache.poi.ss.usermodel.Sheet;
  11. import org.apache.poi.ss.usermodel.Workbook;
  12. public class ExportDataCell {
  13. public static void main(String[] args) throws Exception {
  14. File file = new File("C:\\Users\\Administrator\\Desktop\\test.xls");
  15. if (file.exists()) {
  16. file.delete();
  17. }
  18. BufferedOutputStream out = null;
  19. try {
  20. out = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.xls"));
  21. exportExcel(out);
  22. } finally {
  23. out.close();
  24. }
  25. }
  26. private static void exportExcel(BufferedOutputStream out) throws IOException {
  27. Workbook wb = new HSSFWorkbook();
  28. //Workbook wb = new XSSFWorkbook();
  29. Sheet sheet = wb.createSheet("各种类型单元格");
  30. sheet.createRow(0).createCell(0).setCellValue(1.1);
  31. sheet.createRow(1).createCell(0).setCellValue(new Date());
  32. sheet.createRow(2).createCell(0).setCellValue(Calendar.getInstance());
  33. sheet.createRow(3).createCell(0).setCellValue("字符串");
  34. sheet.createRow(4).createCell(0).setCellValue(true);
  35. sheet.createRow(5).createCell(0).setCellType(CellType.ERROR);
  36. wb.write(out);
  37. }
  38. }

相关文章