ColdFusion - POI Excel 工作簿格式为数字
ColdFusion - POI Excel Workbook Format as Number
我正在使用 ColdFusion 和 POI 工作簿创建一个 Excel 文件。该文件创建得很好,但是我有包含数值的单元格,需要存储为数字才能使 SUM 和其他函数正常工作。
我想在为 excel sheet 添加数据后调用代码,这将基本上循环数据,如果值为数字,则将数据格式设置为数字。这样当我打开 Excel 文件时,我可以对该列数据执行 SUM 或其他操作。
下面是我到目前为止尝试过的方法,它确实 "work" 因为所有单元格都有#EBEBEB 背景色,但是它们仍然有警告说 "Number stored as text" 如下所示.我该如何解决这个问题?
// Create our dataFormat object
df = poiWorkbook.createDataFormat();
// Create our new style
formatNumber = poiWorkbook.createCellStyle();
formatNumber.setFillPattern( formatEvenRowRightAlignStyle.SOLID_FOREGROUND );
formatNumber.setAlignment( formatNumber.ALIGN_RIGHT );
XSSFColor = createObject("java", "org.apache.poi.xssf.usermodel.XSSFColor");
formatNumber.setFillForegroundColor( XSSFColor.init(Color.decode("##EBEBEB")) );
formatNumber.setDataFormat(df.getFormat("0.00"));
// Loop over the data and apply the format
for (x=0;x<rowCount;x++) {
for (y=0;y<colCount;y++) {
poiSheet.getRow(x).getCell(y).setCellStyle(formatNumber);
}
}
(从评论中提升...)
call code after the data has been added
有什么原因吗?虽然有可能,但在填充 sheet 时进行格式化更简单且更不容易出错。假设查询列的数据类型是数字(不是 varchar 或其他),您应该能够在添加查询数据后为该列调用 SpreadsheetFormatColumn。
话虽如此,您首先收到 "Number stored as text" 警告这一事实让我想知道您的查询列的数据类型。 IIRC,更高版本的 CF 应该在使用 SpreadsheetAddRows 时检测数字列,并自动将数字格式应用到这些列 。您的查询列是否可能被格式化为字符串而不是某种数字类型?因为这可以解释结果。解决方案是在数据库查询中将该列转换为数字类型。
我正在使用 ColdFusion 和 POI 工作簿创建一个 Excel 文件。该文件创建得很好,但是我有包含数值的单元格,需要存储为数字才能使 SUM 和其他函数正常工作。
我想在为 excel sheet 添加数据后调用代码,这将基本上循环数据,如果值为数字,则将数据格式设置为数字。这样当我打开 Excel 文件时,我可以对该列数据执行 SUM 或其他操作。
下面是我到目前为止尝试过的方法,它确实 "work" 因为所有单元格都有#EBEBEB 背景色,但是它们仍然有警告说 "Number stored as text" 如下所示.我该如何解决这个问题?
// Create our dataFormat object
df = poiWorkbook.createDataFormat();
// Create our new style
formatNumber = poiWorkbook.createCellStyle();
formatNumber.setFillPattern( formatEvenRowRightAlignStyle.SOLID_FOREGROUND );
formatNumber.setAlignment( formatNumber.ALIGN_RIGHT );
XSSFColor = createObject("java", "org.apache.poi.xssf.usermodel.XSSFColor");
formatNumber.setFillForegroundColor( XSSFColor.init(Color.decode("##EBEBEB")) );
formatNumber.setDataFormat(df.getFormat("0.00"));
// Loop over the data and apply the format
for (x=0;x<rowCount;x++) {
for (y=0;y<colCount;y++) {
poiSheet.getRow(x).getCell(y).setCellStyle(formatNumber);
}
}
(从评论中提升...)
call code after the data has been added
有什么原因吗?虽然有可能,但在填充 sheet 时进行格式化更简单且更不容易出错。假设查询列的数据类型是数字(不是 varchar 或其他),您应该能够在添加查询数据后为该列调用 SpreadsheetFormatColumn。
话虽如此,您首先收到 "Number stored as text" 警告这一事实让我想知道您的查询列的数据类型。 IIRC,更高版本的 CF 应该在使用 SpreadsheetAddRows 时检测数字列,并自动将数字格式应用到这些列 。您的查询列是否可能被格式化为字符串而不是某种数字类型?因为这可以解释结果。解决方案是在数据库查询中将该列转换为数字类型。