Apache POI支持hindi excel阅读

时间:2014-01-24 06:30:43

标签: java excel apache-poi

有人知道如何使用apache poi阅读excel文件有印地语(语言)吗? 我可以阅读有英文的文件但我在阅读印地文件时遇到了错误。

Exception in thread "main" org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:41)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:204)
    at astral.excelreader.ExcelReader.readExcel(ExcelReader.java:20)
    at astral.excelreader.Main.main(Main.java:30)
Caused by: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]
    at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:178)
    at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:662)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:269)
    at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
    ... 3 more

以下是我的java代码

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.google.gson.Gson;

public class ExcelReader {
    public List<Question>  readExcel(long examid,long catid,String url) throws IOException {
        if (url != null && !url.equals("")) {
            FileInputStream file = new FileInputStream(new File(url));
            if (file != null) {
                XSSFWorkbook workbook = new XSSFWorkbook(file);
                if (workbook != null) {
                    XSSFSheet sheet = workbook.getSheetAt(0);
                    if (sheet != null) {
                        Iterator<Row> rowIterator = sheet.iterator();
                        List<Question> questions = new ArrayList<Question>();
                        if (rowIterator != null) {
                            while (rowIterator.hasNext()) {
                                Row row = rowIterator.next();
                                if (row != null) {
                                    Question question = new Question();
                                    // System.out.println(row.getCell(2).toString());

                                    if (row.getCell(0) != null
                                            && row.getCell(1) != null
                                            && row.getCell(2) != null
                                            && row.getCell(3) != null
                                            && row.getCell(4) != null
                                            && row.getCell(5) != null
                                            && row.getCell(6) != null) {
                                        question.setQuestion(row.getCell(0)
                                                .toString());
                                        String[] splitanswer = row.getCell(2)
                                                .toString().split("---");
                                        List<String> answers = new ArrayList<String>();
                                        for (int i = 0; i < splitanswer.length; i++) {
                                            String[] answer = splitanswer[i]
                                                    .split("\\)");
                                            System.out.println(splitanswer[i]
                                                    .toString());
                                            answers.add(splitanswer[i]);
                                        }
                                        question.setSelected(Integer
                                                .parseInt(row.getCell(3)
                                                        .toString()
                                                        .split("\\.")[0]));
                                        question.setQuestionset(Integer
                                                .parseInt(row.getCell(5)
                                                        .toString()
                                                        .split("\\.")[0]));
                                        if (Integer.parseInt(row.getCell(6)
                                                .toString().split("\\.")[0]) == 0) {
                                            question.setLanguage("EN");
                                        } else {
                                            question.setLanguage("HIN");
                                        }

                                        question.setAnswers(answers);
                                        questions.add(question);
                                    }
                                }
                            }

                            Gson gson = new Gson();
                            return questions;
                        }
                    }
                }
            }
        }
        return null;
    }
}

0 个答案:

没有答案