gpt4 book ai didi

java - 除了 getCellTypeEnum() 之外,poi 3.17 中 getcelltype 的替代方案

转载 作者:行者123 更新时间:2023-12-02 07:08:03 33 4
gpt4 key购买 nike

我正在读取一个 Excel 文件并使用 apache poi 3.17 将其转换为 XML 文件。我正在使用 getCelltype 来获取当前单元格的 CELLTYPE。但我收到“getCellType() 已贬值”。我搜索了其他问题,但我发现我应该使用“CellType getCellTypeEnum() ”。但这也显示出同样的警告。我在 Apache POI 文档中搜索,但它写道 "getCellTypeEnum()"已折旧并“使用 getCellType 代替”。我不明白应该用什么来删除警告。如果有人可以帮助getCellType()还有什么其他选择在这种情况下可用。

try {
log.info("Getting the path for Input Excel File");
// String excelPath = "D:\\Tools\\BaNCS_Dictionary_Specification_1.0_Latest.xlsx";
String excelPath=InputExcel.inputExcel();
FileInputStream fileInputStream = new FileInputStream(new File(excelPath));

// Create Workbook instance holding .xls file
log.info("Creating workbook instance for XSSFWorkbook");
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);

// Get the first worksheet
log.info("Creating the sheet of XSSFSheet");
XSSFSheet sheet = workbook.getSheet("DictionarySpecification");
int i=InputExcelValidator.DictionarySpecificationRowsValidator(sheet);

// Iterate through each rows
log.info("Creating RowIterator to iterate through the rows");
Iterator<Row> rowIterator = sheet.iterator();

while (rowIterator.hasNext()) {
// Get Each Row
log.info("Getting each row one by one");
Row row = rowIterator.next();
log.info("Assigning the value of tempForEmptyRowCount2 to tempForEmptyRowCount1");
int tempForEmptyRowCount2=tempForEmptyRowCount1;
log.info("Getting the rowNumber of Current Iteration");
tempForEmptyRowCount1=row.getRowNum();
if(row.getRowNum()==0){
log.info("Skipping The first row of the Excel File");
continue;
}
if (row.getRowNum() <= 25 && row.getRowNum()>0) {
log.info("Adding the Dictionary Specification Values and calling addDictionarySpecifications");
addDictionarySpecifications(tempForEmptyRowCount2, rowIterator, row);
continue;
}
if(row.getRowNum()-tempForEmptyRowCount2>2){
log.error("Please correct the format to input table details");
throw new TableInputFormatException("Please correct the format to input table details");
}

if (row.getRowNum()-tempForEmptyRowCount2>1) {
log.info("Skipping Empty Row");
tempForAddingValuesToTable1 = 1;
tempForAddingValuesToTable2 = false;
tempForTableCount++;
continue;
}
log.info("Creating CellIterator inatance");
Iterator<Cell> cellIterator = row.cellIterator();
if (tempForAddingValuesToTable1 == 1 && tempForAddingValuesToTable2 == false) {
log.info("Adding the table values");
Table table = new Table();
log.info("Creating an empty arrayList in Table for fields");
table.setFieldList(new ArrayList<Field>());
while (cellIterator.hasNext()) {
log.info("Iterating the cells for table values");
Cell cell = cellIterator.next();
int columnIndex = cell.getColumnIndex();
switch (columnIndex + 1) {
case 1:
log.info("Adding the table name");
table.setName(cell.getStringCellValue().trim());
break;
case 2:
log.info("Adding the table annotation");
table.setAnnotation(cell.getStringCellValue().trim());
break;
case 3:
log.info("Adding the table Identifier");
table.setIdentifier(cell.getStringCellValue().trim());
break;
case 4:
log.info("Adding the table FieldPosition");
switch (cell.getCellType()){
case Cell.CELL_TYPE_STRING:
table.setFieldPosition(Integer.parseInt( cell.getStringCellValue().trim()));
break;
case Cell.CELL_TYPE_NUMERIC:
table.setFieldPosition((int) cell.getNumericCellValue());
break;
}

break;
case 5:
log.info("Adding the table RecordType");
switch (cell.getCellType()){
case Cell.CELL_TYPE_STRING:
table.setRecordType( cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
table.setRecordType( String.valueOf(cell.getNumericCellValue()));
break;
}
break;
}
}
log.info("Adding the table to Tables");
tables.getTableList().add(table);
tempForAddingValuesToTable2=true;
continue;
}
if (tempForAddingValuesToTable1 == 1 && tempForAddingValuesToTable2 == true) {
log.info("Skipping Empty Row");
tempForAddingValuesToTable1=0;
continue;
}
if (tempForAddingValuesToTable1 == 0 && tempForAddingValuesToTable2 == true) {
log.info("Creating Field Instance");
Field field = new Field();
while (cellIterator.hasNext()) {
log.info("Adding the field values one by one");
Cell cell = cellIterator.next();
int columnIndex = cell.getColumnIndex();
int tempColumnIndex = columnIndexIndicator;
if (tempColumnIndex - columnIndexIndicator > 1) {
field.setDefaultValue("null");
}
switch (columnIndex + 1) {
case 1:
log.info("Adding name for field");
field.setName(cell.getStringCellValue().trim());
break;
case 2:
log.info("Adding Annotation for field");
field.setAnnotation(cell.getStringCellValue().trim());
break;
case 3:
log.info("Adding DataType for field");
field.setDataType(cell.getStringCellValue());
break;

case 4:
log.info("Adding Size for field");
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
field.setSize(cell.getStringCellValue().trim());
break;
case Cell.CELL_TYPE_NUMERIC:
Double doubleValue=cell.getNumericCellValue();
DecimalFormat format = new DecimalFormat("0.#");
field.setSize(format.format(doubleValue));
break;
}
break;

case 5:
log.info("Adding pKey for field");
if (cell.getCellType() == cell.CELL_TYPE_STRING) {
String s2 = cell.getStringCellValue();

if (s2.equals("Yes"))
tempForAddingBooleanValuesToTable = true;
else
tempForAddingBooleanValuesToTable = false;
field.setpKey(tempForAddingBooleanValuesToTable);
}
break;
case 6:
log.info("Adding Mandatory for field");
if (cell.getStringCellValue().equals("Yes"))
tempForAddingBooleanValuesToTable = true;
else
tempForAddingBooleanValuesToTable = false;
field.setMandatory(tempForAddingBooleanValuesToTable);
break;
case 7:
log.info("Adding DefaultValue for field");
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
field.setDefaultValue(cell.getStringCellValue().trim());

break;
case Cell.CELL_TYPE_NUMERIC:
Double doubleValue=cell.getNumericCellValue();
DecimalFormat format = new DecimalFormat("0.####");
field.setDefaultValue(format.format(doubleValue));
break;
}
break;
case 8:
log.info("Adding Rec_Identifier for field");
if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
int i2 = (int) cell.getNumericCellValue();
String s = String.valueOf(i2);
if (s.equals("Yes"))
tempForAddingBooleanValuesToTable = true;
else
tempForAddingBooleanValuesToTable = false;
field.setRec_Identifier(tempForAddingBooleanValuesToTable);
}
break;
case 9:
log.info("Adding Visible for field");
if (cell.getStringCellValue().equals("Yes"))
tempForAddingBooleanValuesToTable = true;
else
tempForAddingBooleanValuesToTable = false;
field.setVisible(tempForAddingBooleanValuesToTable);
break;

}
columnIndexIndicator = cell.getColumnIndex();


}
if(field.getSize()==null){
log.info("Setting the DateFormat");
field.setDateFormat("DDMMYYYY");
}
log.info("Adding the field to specific table");
tables.getTableList().get(tempForTableCount).getFieldList().add(field);
}
}
log.info("Setting properties to respective lists");
recordProperties.setProperties(propertyList1);
fileProperties.setProperties(propertyList2);
dataDictionary.setDictionary(dictionary);
dataDictionary.setFileProperties(fileProperties);
dataDictionary.setRecordProperties(recordProperties);
dataDictionary.setTables(tables);
}
catch (IOException ie) {
log.error("Exception Occured: IOException");
ie.printStackTrace();
}
log.info("Calling the createXML to generate the xml file");
CreateXML.createXML(dataDictionary);
}
public static int addDictionarySpecifications(int j, Iterator<Row> rowIterator,Row row){
log.info("Getting the CellIterator");
Iterator<Cell> cellIterator = row.cellIterator();


if(row.getRowNum()-j>1){
log.info("Getting Empty Row");
rowIndicator++;
tempForSkippingRow=true;
// return 0;
}
if(tempForSkippingRow){
log.info("Skipping Row");
tempForSkippingRow=false;
return 0;

}
log.info("Creating instance of Property Object");
Property property=new Property();
while (cellIterator.hasNext()) {
log.info("Getting Cells one by one");
Cell cell = cellIterator.next();
log.info("Getting the columnIndex of Current Row");
int columnIndex = cell.getColumnIndex();

if(rowIndicator==1) {
log.info("Adding the Dictionary Nsme and annotation and type");
switch (columnIndex + 1) {
case 2:
dictionary.setName(cell.getStringCellValue().trim());
dictionary.setAnnotation(dictionary.getName());
dictionary.setType("DATABASE_TABLES");
break;
}
}
if(rowIndicator>1){
log.info("Adding the Properties for FileProperties and Record Properties");
switch(columnIndex+1){
case 1:
log.info("GEtting the annotation");
String annotation=cell.getStringCellValue().trim();
String propertyName;
if(annotation.equals("Record Composition")) {
log.info("Property is Record Composition");
property.setName("COMPOSITION");
}
else if(annotation.equals("Record Heirarchy")) {
log.info("Property is Record Heirarchy");
property.setName("HEIRARCHY");
}
else if(annotation.equals("Set Feed Quality Standards")) {
log.info("Property is Set Feed Quality Standards");
property.setName("FQD");
property.setAnnotation("FQD");
break;
}
else if(annotation.equals("Quality Percentage")) {
log.info("Property is Quality Percentage");
property.setName("FQD_PERCENT");
property.setAnnotation("FQD Percent");
break;
}
else if(annotation.equals("Header Record")) {
log.info("Property is Header Record");
property.setName("HEADER");
}
else if(annotation.equals("Trailer Record")) {
log.info("Property is Trailer Record");
property.setName("TRAILER");
}
else {
log.info("Creating exact name for Property");
String[] split = annotation.split("\\s+");
String s1=split[0].toUpperCase();
String s2=split[1].toUpperCase();
propertyName=s1+"_"+s2;
log.info("Property is"+propertyName);
property.setName(propertyName);
}
property.setAnnotation(annotation);
break;
case 2:
log.info("Setting the valuedescription");
String valueDesscription=null;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
valueDesscription=cell.getStringCellValue().trim();

break;
case Cell.CELL_TYPE_NUMERIC:
try {
valueDesscription=String.valueOf(cell.getNumericCellValue()).trim();
} catch (Exception e) {
e.printStackTrace();
}
break;
}

if(valueDesscription.equals("Multiple Type of Records"))
property.setDefaultValue("HETEROGENOUS");
else if(valueDesscription.equals("Single Type of Records"))
property.setDefaultValue("HOMOGENOUS");
else if(valueDesscription.equals("Record Group Without heirarchy"))
property.setDefaultValue("GROUPED");
else if(valueDesscription.equals("Record Group With heirarchy"))
property.setDefaultValue("HEIRARCHY");
else if(valueDesscription.equals("Variable Length"))
property.setDefaultValue("VARIABLE_LENGTH");
else if(valueDesscription.equals("Carriage return line feed(CRLF)"))
property.setDefaultValue("CRLF");
else if(valueDesscription.equals("Comma(,)"))
property.setDefaultValue(",");
else if(valueDesscription.equals("Semicolon(;"))
property.setDefaultValue(";");
else if(valueDesscription.equals("Tilda(~)"))
property.setDefaultValue("~");
else if(valueDesscription.equals("Pipe(|)"))
property.setDefaultValue("|");
else if(valueDesscription.equals("Horizontal Tab(HT)"))
property.setDefaultValue("HT");
else if(valueDesscription.equals("Hash(#)"))
property.setDefaultValue("#");
else if(valueDesscription.equals("No"))
property.setDefaultValue("false");
else if(valueDesscription.equals("FAILED"))
property.setDefaultValue("FAILED");
else if(valueDesscription.equals("File Content"))
property.setDefaultValue("HASH_DUP");
else if(valueDesscription.equals("File Name"))
property.setDefaultValue("FILE_NAME");
else if(valueDesscription.equals("Yes"))
property.setDefaultValue("true");
else
property.setDefaultValue(valueDesscription);
property.setValueDescription(valueDesscription);
break;
case 3:
log.info("Setting the annotation valueDescription and default Value");
property.setAnnotation("Duplicate Duration");
property.setName("DUPLICATE_DURATION");
property.setValueDescription(cell.getStringCellValue().trim());
property.setDefaultValue(property.getAnnotation());
break;
}
}
}
if(rowIndicator>1) {
log.info("Adding the property to PropertyList");
if (property.getDefaultValue().equals("HETEROGENOUS") || property.getDefaultValue().equals("HOMOGENOUS") || property.getDefaultValue().equals("GROUPED") || property.getDefaultValue().equals("HEIRARCHY"))
propertyList1.add(property);
else
propertyList2.add(property);
}
return 0;
}

最佳答案

您应该能够使用 getCellTypeEnumn() 应用更改。 Cell.CELL_TYPE_* 枚举在 Poi 3.17 中已弃用,但您可以使用 Celltype.*,例如:

                if(cell.getCellTypeEnum() == CellType.STRING){
//your code
}else if(cell.getCellTypeEnum() == CellType.NUMERIC){
//your code
}

细胞类型的值为:

_NONE(-1),
NUMERIC(0),
STRING(1),
FORMULA(2),
BLANK(3),
BOOLEAN(4),
ERROR(5);

希望这可以帮助你。

谢谢。

关于java - 除了 getCellTypeEnum() 之外,poi 3.17 中 getcelltype 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47068697/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com