gpt4 book ai didi

java - Apache POI 单元格类型错误

转载 作者:行者123 更新时间:2023-12-01 09:35:06 28 4
gpt4 key购买 nike

我正在尝试使用 Apache POI 读取一个 excel 文件,该文件将有两列:标题和语言。标题将包含一些某种语言的句子,语言栏将为空。 Apache POI 读取标题中的句子后,应将其保存在变量中,然后调用语言检测库 ( https://code.google.com/archive/p/language-detection/ )。我特别在有 case 语句的行上遇到错误

import java.util.ArrayList; 
import com.cybozu.labs.langdetect.Detector;
import com.cybozu.labs.langdetect.DetectorFactory;
import com.cybozu.labs.langdetect.Language;
import java.util.Scanner;
import com.cybozu.labs.langdetect.LangDetectException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class LangDetectSample {
public static void main(String[] args) throws IOException, LangDetectException {
String excelFilePath = "C:\\LD\\Books.xlsx";
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));

Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0); // Assuming that the data is sheet in one
Iterator<Row> iterator = firstSheet.iterator();
DataFormatter formatter = new DataFormatter();

LangDetectSample lang = new LangDetectSample();

//creating variables
String title;
String language;
int rowNumber;

//Blank workbook
XSSFWorkbook wb = new XSSFWorkbook(); //new workbook //fixed

//Create a blank sheet
Sheet sheet1 = wb.createSheet("Predicted language"); //fixed
while (iterator.hasNext())
{
Row nextRow = iterator.next();
rowNumber = nextRow.getRowNum();
Cell cell = nextRow.getCell(2); // title is in column 2
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
title = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:

title = formatter.formatCellValue(cell);
break;


case Cell.CELL_TYPE_NUMERIC:

title = formatter.formatCellValue(cell);
break;
}

System.out.print(title);

//Title should now have the title.
// Call the language detector:
language = lang.detect(title);
System.out.println(lang);

// if language detected, attempt to output the result to the new excel file with the following commands:

// Write the title, language

Row row = sheet1.createRow(rowNumber); //changed var
Cell cell2 = row.createCell(2); //changed variable name
cell.setCellValue(title);
Cell cell3 = row.createCell(3);
cell.setCellValue(language);
}
try {
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("title-language.xlsx"));
workbook.write(out);
out.close();
} catch (Exception e)

{
e.printStackTrace();
}
workbook.close();
inputStream.close();
}

public void init(String profileDirectory) throws LangDetectException {
DetectorFactory.loadProfile(profileDirectory);
}

public String detect(String text) throws LangDetectException {
DetectorFactory.loadProfile("C:\\LD\\profiles");
Detector detector = DetectorFactory.create();
detector.append(text);
return detector.detect();
}

public ArrayList detectLangs(String text) throws LangDetectException {
Detector detector = DetectorFactory.create();
detector.append(text);
return detector.getProbabilities();
}
}

我收到的错误是

变量标题可能尚未初始化

最佳答案

对于检查 boolean 值的第一个错误,请保留“Object”类的可用值,例如

Object title;

switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
title = cell.getBooleanCellValue();
break;
}

对于第二个错误,java读取“double”中的默认单元格值 数据类型格式,因此您需要使用以下方法将其转换为文本/字符串...

Object title="";

title = new DecimalFormat("0").format(Cell.getNumericCellValue());

希望这对您有帮助...

谢谢

关于java - Apache POI 单元格类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39052942/

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