Apache POI单元格类型错误

时间:2016-08-20 10:14:57

标签: java apache-poi

我正在尝试使用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();
    }
}

我得到的错误是

变量标题可能尚未初始化

2 个答案:

答案 0 :(得分:1)

对于检查布尔值的第一个错误,保持“对象”类的可变性,例如

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:maxLines="1" 
    android:scrollHorizontally="true"
    android:layout_gravity="center_horizontal"/>

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

Object title;

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

希望这会对你有所帮助......

感谢

答案 1 :(得分:0)

我认为您在某些情况下会遇到问题 现在在更高版本中 poi 4.0.1 CELL_TYPE_NUMERIC现在NUMERIC删除CELL_TYPE_

        switch (cell.getCellType()) {
        case STRING:
                    title = cell.getStringCellValue();
                    break;
        case BOOLEAN:
                   title = formatter.formatCellValue(cell);
                   break;
        case NUMERIC:

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