位置
//在類 org.apache.poi.ss.usermodel.BuiltinFormats 中的私有成員變量_formats中
private static final String[] _formats = new String[]{"General", "0", "0.00", "#,##0", "#,##0.00", "\"$\"#,##0_);(\"$\"#,##0)", "\"$\"#,##0_);[Red](\"$\"#,##0)", "\"$\"#,##0.00_);(\"$\"#,##0.00)", "\"$\"#,##0.00_);[Red](\"$\"#,##0.00)", "0%", "0.00%", "0.00E+00", "# ?/?", "# ??/??", "m/d/yy", "d-mmm-yy", "d-mmm", "mmm-yy", "h:mm AM/PM", "h:mm:ss AM/PM", "h:mm", "h:mm:ss", "m/d/yy h:mm", "reserved-0x17", "reserved-0x18", "reserved-0x19", "reserved-0x1A", "reserved-0x1B", "reserved-0x1C", "reserved-0x1D", "reserved-0x1E", "reserved-0x1F", "reserved-0x20", "reserved-0x21", "reserved-0x22", "reserved-0x23", "reserved-0x24", "#,##0_);(#,##0)", "#,##0_);[Red](#,##0)", "#,##0.00_);(#,##0.00)", "#,##0.00_);[Red](#,##0.00)", "_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)", "_(\"$\"* #,##0_);_(\"$\"* (#,##0);_(\"$\"* \"-\"_);_(@_)", "_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)", "_(\"$\"* #,##0.00_);_(\"$\"* (#,##0.00);_(\"$\"* \"-\"??_);_(@_)", "mm:ss", "[h]:mm:ss", "mm:ss.0", "##0.0E+0", "@"};
數據格式列舉
0, "General"
1, "0"
2, "0.00"
3, "#,##0"
4, "#,##0.00"
5, "$#,##0_);($#,##0)"
6, "$#,##0_);[Red]($#,##0)"
7, "$#,##0.00);($#,##0.00)"
8, "$#,##0.00_);[Red]($#,##0.00)"
9, "0%"
0xa, "0.00%"
0xb, "0.00E+00"
0xc, "# ?/?"
0xd, "# ??/??"
0xe, "m/d/yy"
0xf, "d-mmm-yy"
0x10, "d-mmm"
0x11, "mmm-yy"
0x12, "h:mm AM/PM"
0x13, "h:mm:ss AM/PM"
0x14, "h:mm"
0x15, "h:mm:ss"
0x16, "m/d/yy h:mm"
// 0x17 - 0x24 reserved for international and undocumented
0x25, "#,##0_);(#,##0)"
0x26, "#,##0_);[Red](#,##0)"
0x27, "#,##0.00_);(#,##0.00)"
0x28, "#,##0.00_);[Red](#,##0.00)"
0x29, "_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)"
0x2a, "_($* #,##0_);_($* (#,##0);_($* \"-\"_);_(@_)"
0x2b, "_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)"
0x2c, "_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)"
0x2d, "mm:ss"
0x2e, "[h]:mm:ss"
0x2f, "mm:ss.0"
0x30, "##0.0E+0"
0x31, "@" - This is text format.
0x31 "text" - Alias for "@"
數據格式對應的簡要說明
- 0,“General”:通用格式,Excel會根據輸入的數據自動選擇最合適的格式(如數字、日期或文本)。
- 1,“0”:顯示沒有小數點的數字。
- 2,“0.00”:顯示帶有兩位小數的數字。
- 3,“#,##0”:顯示帶有千位分隔符的整數。
- 4,“#,##0.00”:顯示帶有千位分隔符和兩位小數的數字。
- 5,“#,##0_);(#,##0)”:顯示正數帶有千位分隔符的貨幣格式,負數在括號中。
- 6,“#,##0_);[Red](#,##0)”:與上一個格式相同,但負數顯示為紅色。
- 7,“#,##0.00);(#,##0.00)”:與格式5類似,但包括兩位小數。
- 8,“#,##0.00_);[Red](#,##0.00)”:與格式6和7相同,但包括兩位小數,且負數顯示為紅色。
- 9,“0%”:顯示數字為百分比,沒有小數位。
- 0xa,“0.00%”:顯示數字為百分比,包括兩位小數。
- 0xb,“0.00E+00”:科學記數法,總是顯示兩位小數和指數。
- 0xc,“# ?/?”:分數格式,顯示一個分數。
- 0xd,“# ??/??”:分數格式,顯示帶有兩位整數的分數。
- 0xe?-?0x16:這些都是日期和時間的格式,顯示月/日/年、小時:分鐘:秒等不同的組合。
- 0x17 - 0x25:為保留格式,暫時用不到
- 0x26: 顯示正數為正常顏色,負數為紅色。沒有小數部分。
- 0x27: 正數格式與上相同,但包括兩位小數。負數也包括兩位小數。
- 0x28: 與0x27類似,但負數顯示為紅色。
- 0x29: 用于顯示與零值有關的數字,帶有一些自定義的文本和符號。通常用于顯示正負值和零值,以及某些自定義文本。
- 0x2a: 與0x29類似,但包括貨幣符號。
- 0x2b: 與0x2a類似,同時包括兩位小數。
- 0x2c: 與0x2b相同,但應用于貨幣值。
- 0x2d: 時間格式,顯示分鐘和秒。
- 0x2e: 顯示小時、分鐘和秒的時間格式。
- 0x2f: 與0x2d相同,但秒的小數部分顯示一位。
- 0x30: 科學記數法表示,但只顯示一位小數。
- 0x31: 表示文本格式。當單元格設置為這種格式時,單元格中的內容會被當作純文本處理,不會進行數字運算。
?其中文本格式既可以用 @也可以用TEXT作為輸入表示
代碼中如何設置單元格的數據格式
代碼演示,設置單元格數據格式為文本
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class SetCellToTextFormatExample { public static void main(String[] args) throws Exception { // 創建新的Excel工作簿 Workbook workbook = new XSSFWorkbook(); // 創建工作表 Sheet sheet = workbook.createSheet("Sheet1"); // 創建CellStyle并設置數據格式為文本 CellStyle textStyle = workbook.createCellStyle(); DataFormat dataFormat = workbook.createDataFormat(); textStyle.setDataFormat(dataFormat.getFormat("@")); // "@"代表文本格式 // 創建行和單元格,并應用文本樣式 Row row = sheet.createRow(0); // 創建行 Cell cell = row.createCell(0); // 創建單元格 cell.setCellValue("This is text format"); // 設置單元格值 cell.setCellStyle(textStyle); // 應用樣式到單元格 // 輸出到文件,這里為了演示輸出到控制臺 System.out.println(workbook); // 實際應用中,你可能需要保存到文件 // FileOutputStream fileOut = new FileOutputStream("workbook.xlsx"); // workbook.write(fileOut); // fileOut.close(); workbook.close(); }
}