Apache POI 仅当 Number 为十进制时才应用浮点格式
Apache POI apply foating point format only if Number is decimal
目前我使用格式“#,##0.00”将从 oracle DB 检索到的所有数字类型值保存在 excel 文件中,但我不想保存所有如果 DB
上没有浮点值,则为浮点值
例如
12 should be 12
12.1 should be 12.10
12.123 should be 12.12
如果没有为单元格设置特殊的数字格式,则使用“常规”数字格式。这将显示 12 或 12.1 或 12.123。因此,为了获得您想要的,需要两种不同的数字格式(整数为“0”(如果使用千位分隔符则为“#,##0”),小数为“#,##0.00”)。
所以主要问题是如何判断一个数是整数还是小数。以下代码使用 Number
的方法完成此任务。如果数字的整数值等于数字的十进制值,则数字为整数。
它创建一个 sheet,在 A1
中显示 12,在 A2
中显示 12.10,在 A3
中显示 12.12。
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.List;
import java.util.ArrayList;
public class CreateExcelDifferentNumberformats {
public static void main(String[] args) throws Exception {
//Workbook workbook = new HSSFWorkbook();
Workbook workbook = new XSSFWorkbook();
// Create CellStyles
DataFormat format = workbook.createDataFormat();
CellStyle integerCellStyle = workbook.createCellStyle();
//integerCellStyle.setDataFormat(format.getFormat("0"));
integerCellStyle.setDataFormat(format.getFormat("#,##0"));
CellStyle decimalCellStyle = workbook.createCellStyle();
decimalCellStyle.setDataFormat(format.getFormat("#,##0.00"));
Sheet sheet = workbook.createSheet();
List<Number> numbers = new ArrayList<Number>();
numbers.add(12); numbers.add(12.1); numbers.add(12.123);
int r = 0;
for (Number number : numbers) {
Row row = sheet.createRow(r++);
Cell cell = row.createCell(0);
cell.setCellValue(number.doubleValue());
if (number.intValue() == number.doubleValue()) {
cell.setCellStyle(integerCellStyle);
} else {
cell.setCellStyle(decimalCellStyle);
}
}
FileOutputStream out = null;
if (workbook instanceof HSSFWorkbook) {
out = new FileOutputStream("./CreateExcelDifferentNumberformats.xls");
} else if (workbook instanceof XSSFWorkbook) {
out = new FileOutputStream("./CreateExcelDifferentNumberformats.xlsx");
}
workbook.write(out);
out.close();
workbook.close();
}
}
目前我使用格式“#,##0.00”将从 oracle DB 检索到的所有数字类型值保存在 excel 文件中,但我不想保存所有如果 DB
上没有浮点值,则为浮点值例如
12 should be 12
12.1 should be 12.10
12.123 should be 12.12
如果没有为单元格设置特殊的数字格式,则使用“常规”数字格式。这将显示 12 或 12.1 或 12.123。因此,为了获得您想要的,需要两种不同的数字格式(整数为“0”(如果使用千位分隔符则为“#,##0”),小数为“#,##0.00”)。
所以主要问题是如何判断一个数是整数还是小数。以下代码使用 Number
的方法完成此任务。如果数字的整数值等于数字的十进制值,则数字为整数。
它创建一个 sheet,在 A1
中显示 12,在 A2
中显示 12.10,在 A3
中显示 12.12。
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.List;
import java.util.ArrayList;
public class CreateExcelDifferentNumberformats {
public static void main(String[] args) throws Exception {
//Workbook workbook = new HSSFWorkbook();
Workbook workbook = new XSSFWorkbook();
// Create CellStyles
DataFormat format = workbook.createDataFormat();
CellStyle integerCellStyle = workbook.createCellStyle();
//integerCellStyle.setDataFormat(format.getFormat("0"));
integerCellStyle.setDataFormat(format.getFormat("#,##0"));
CellStyle decimalCellStyle = workbook.createCellStyle();
decimalCellStyle.setDataFormat(format.getFormat("#,##0.00"));
Sheet sheet = workbook.createSheet();
List<Number> numbers = new ArrayList<Number>();
numbers.add(12); numbers.add(12.1); numbers.add(12.123);
int r = 0;
for (Number number : numbers) {
Row row = sheet.createRow(r++);
Cell cell = row.createCell(0);
cell.setCellValue(number.doubleValue());
if (number.intValue() == number.doubleValue()) {
cell.setCellStyle(integerCellStyle);
} else {
cell.setCellStyle(decimalCellStyle);
}
}
FileOutputStream out = null;
if (workbook instanceof HSSFWorkbook) {
out = new FileOutputStream("./CreateExcelDifferentNumberformats.xls");
} else if (workbook instanceof XSSFWorkbook) {
out = new FileOutputStream("./CreateExcelDifferentNumberformats.xlsx");
}
workbook.write(out);
out.close();
workbook.close();
}
}