1
我使用apache poi創建excel表格。我有像 - 337499.939437217這樣的數字,我想在沒有四捨五入的情況下在excel中顯示。單元格格式也應該是數字(對於某些列)和貨幣(對於某些列)。apache poi中的數字和單元格格式
請建議我應該使用哪個BuiltinFormat來實現此目的。
非常感謝您的幫助。
我使用apache poi創建excel表格。我有像 - 337499.939437217這樣的數字,我想在沒有四捨五入的情況下在excel中顯示。單元格格式也應該是數字(對於某些列)和貨幣(對於某些列)。apache poi中的數字和單元格格式
請建議我應該使用哪個BuiltinFormat來實現此目的。
非常感謝您的幫助。
起初,您需要知道如何使用DataFormats。那麼你需要知道guidelines for customizing a number format。
對於您的號碼-337499.939437217
將顯示四捨五入通用數字格式,您可以使用格式#.###############
。 #
表示僅在需要時顯示的數字(不是前導零和/或不是零作爲最後的十進制數字) - 請參閱準則。因此,如果需要,整個格式意味着顯示最多15個十進制數字,但只根據需要顯示。
對於貨幣,您應該真正使用貨幣的內置數字格式。所以貨幣符號取決於Excel
的區域設置。以下BuiltinFormats可與apache poi
一起使用。使用內置數字格式,您只需要十六進制格式數字。
實施例:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateNumberFormats {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("format sheet");
CellStyle style;
DataFormat format = wb.createDataFormat();
Row row;
Cell cell;
short rowNum = 0;
short colNum = 0;
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(-337499.939437217); // general format
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#.###############")); // custom number format
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(-337499.939437217);
cell.setCellStyle(style);
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(123.456789);
cell.setCellStyle(style);
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(123456789.);
cell.setCellStyle(style);
style = wb.createCellStyle();
style.setDataFormat((short)0x7); // builtin currency format
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(-1234.5678);
cell.setCellStyle(style);
sheet.autoSizeColumn(0);
FileOutputStream fileOut = new FileOutputStream("CreateNumberFormats.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
}