简体   繁体   English

apache poi获取默认列类型

[英]apache poi get default column type

I'm using POI to create a new row of cells in an existing spreadsheet. 我正在使用POI在现有电子表格中创建新的单元格行。 POI allows you to get the column default style, but there's no equivalent (as near as I can tell) to getting a default type. POI允许您获取列的默认样式,但是(相当于我所知的)没有等同于获取默认类型的方式。 I'm getting a String from my user interface and I don't know how to set the cell type. 我从用户界面获取字符串,并且我不知道如何设置单元格类型。 If the string is a double, then fine, it's NUMERIC. 如果字符串是双精度数,则为NUMERIC。 But if the String specifies a date, how would I best detect it so that it is also set to NUMERIC? 但是,如果字符串指定了日期,那么如何最好地检测到它,并将其也设置为NUMERIC? There are some many formatting types for a date that it is impractical to detect the type from the cell style format. 对于日期而言,有许多格式设置类型,从单元格样式格式中检测该类型是不切实际的。 Does POI support a way to parse based on a format? POI是否支持一种基于格式的解析方法?

To set the cell type you use: 要设置单元格类型,请使用:

setCellType()

as outlined in the docs: 如文档所述:

https://poi.apache.org/apidocs/ https://poi.apache.org/apidocs/

setCellType void setCellType(int cellType) Set the cells type (numeric, formula or string). setCellType void setCellType(int cellType)设置单元格类型(数字,公式或字符串)。 If the cell currently contains a value, the value will be converted to match the new type, if possible. 如果单元格当前包含一个值,则将尽可能转换该值以匹配新类型。 Formatting is generally lost in the process however. 但是,格式化通常会在此过程中丢失。

If what you want to do is get a String value for your numeric cell, stop!. 如果您要获取数字单元格的字符串值,请停止! This is not the way to do it. 这不是这样做的方法。 Instead, for fetching the string value of a numeric or boolean or date cell, use DataFormatter instead. 相反,要获取数字或布尔或日期单元格的字符串值,请使用DataFormatter。

Throws: java.lang.IllegalArgumentException - if the specified cell type is invalid java.lang.IllegalStateException - if the current value cannot be converted to the new type See Also: CELL_TYPE_NUMERIC, CELL_TYPE_STRING, CELL_TYPE_FORMULA, CELL_TYPE_BLANK, CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR 抛出:java.lang.IllegalArgumentException-如果指定的单元格类型无效java.lang.IllegalStateException-如果无法将当前值转换为新的类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM