记录0x5B的初始化剩余1个字节仍有待读取。ApachePOIJava

记录0x5B的初始化剩余1个字节仍有待读取。ApachePOIJava,java,apache-poi,Java,Apache Poi,我正在使用以下代码将xls转换为csv,并收到错误信息: 初始化记录0x5B后,剩余1个字节仍需读取。我想可能是这个文件,我试过不同的文件,但事实并非如此 import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import org.apache.poi.op

我正在使用以下代码将xls转换为csv,并收到错误信息: 初始化记录0x5B后,剩余1个字节仍需读取。我想可能是这个文件,我试过不同的文件,但事实并非如此

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;


import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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.ss.usermodel.WorkbookFactory;

public class Excel2Txt{

    public static void convertExcelToCSV(String fileName) throws InvalidFormatException, IOException {


        BufferedWriter output = new BufferedWriter(new FileWriter(fileName.substring(0, fileName.lastIndexOf(".")) + ".csv"));

        InputStream is = new FileInputStream(fileName);

       Workbook wb = WorkbookFactory.create(is);

     Sheet sheet = wb.getSheetAt(0);

       // hopefully the first row is a header and has a full compliment of
        // cells, else you'll have to pass in a max (yuck)
        int maxColumns = sheet.getRow(0).getLastCellNum();

        for (Row row : sheet) {

           // row.getFirstCellNum() and row.getLastCellNum() don't return the
            // first and last index when the first or last column is blank
         int minCol = 0; // row.getFirstCellNum()
           int maxCol = maxColumns; // row.getLastCellNum()

           for (int i = minCol; i < maxCol; i++) {

                Cell cell = row.getCell(i);
                String buf = "";
             if (i > 0) {
                    buf = ",";
               }

             if (cell == null) {
                    output.write(buf);
                 //System.out.print(buf);
               } else {

                  String v = null;

                  switch (cell.getCellType()) {
                  case Cell.CELL_TYPE_STRING:
                        v = cell.getRichStringCellValue().getString();
                     break;
                 case Cell.CELL_TYPE_NUMERIC:
                       if (DateUtil.isCellDateFormatted(cell)) {
                          v = cell.getDateCellValue().toString();
                        } else {
                           v = String.valueOf(cell.getNumericCellValue());
                        }
                      break;
                 case Cell.CELL_TYPE_BOOLEAN:
                       v = String.valueOf(cell.getBooleanCellValue());
                        break;
                 case Cell.CELL_TYPE_FORMULA:
                       v = cell.getCellFormula();
                     break;
                 default:
                   }

                 if (v != null) {
                       buf = buf + toCSV(v);
                  }
                  output.write(buf);
                 //System.out.print(buf);
               }
          }

         output.write("\n");
          //System.out.println();
        }
      is.close();
        output.close();

   }


    public static String toCSV(String value) {

       String v = null;
       boolean doWrap = false;

       if (value != null) {

           v = value;

           if (v.contains("\"")) {
             v = v.replace("\"", "\"\""); // escape embedded double quotes
               doWrap = true;
         }

           if (v.contains(",") || v.contains("\n")) {
             doWrap = true;
         }

           if (doWrap) {
              v = "\"" + v + "\""; // wrap with double quotes to hide the comma
            }
      }

       return v;

   }


    public static void main(String[] args) throws IOException {
        try {
            convertExcelToCSV("\\C:\\Data\\test.xls");
        } catch (InvalidFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}
导入java.io.BufferedWriter;
导入java.io.FileInputStream;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.InputStream;
导入org.apache.poi.openxml4j.exceptions.InvalidFormatException;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.DateUtil;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.ss.usermodel.WorkbookFactory;
公共类Excel2Text{
公共静态void convertExcelToCSV(字符串文件名)引发InvalidFormatException,IOException{
BufferedWriter输出=新的BufferedWriter(新的FileWriter(fileName.substring(0,fileName.lastIndexOf(“.”)和“.csv”);
InputStream is=新文件InputStream(文件名);
工作簿wb=WorkbookFactory.create(is);
Sheet Sheet=wb.getSheetAt(0);
//希望第一行是一个标题,并有一个完整的赞扬
//细胞,否则你将不得不通过一个最大(恶心)
int maxColumns=sheet.getRow(0.getLastCellNum();
用于(行:页){
//row.getFirstCellNum()和row.getLastCellNum()不返回
//第一列或最后一列为空时的第一个和最后一个索引
int minCol=0;//row.getFirstCellNum()
int maxCol=maxColumns;//row.getLastCellNum()
for(int i=minCol;i0){
buf=“,”;
}
if(单元格==null){
输出。写入(buf);
//系统输出打印(buf);
}否则{
字符串v=null;
开关(cell.getCellType()){
case Cell.Cell\u类型\u字符串:
v=cell.getRichStringCellValue().getString();
打破
case Cell.Cell\u类型\u数值:
if(DateUtil.isCellDateFormatted(单元格)){
v=cell.getDateCellValue().toString();
}否则{
v=String.valueOf(cell.getNumericCellValue());
}
打破
case Cell.Cell\u类型\u布尔值:
v=String.valueOf(cell.getBooleanCellValue());
打破
case Cell.Cell_类型_公式:
v=cell.getCellFormula();
打破
违约:
}
如果(v!=null){
buf=buf+toCSV(v);
}
输出。写入(buf);
//系统输出打印(buf);
}
}
输出。写入(“\n”);
//System.out.println();
}
is.close();
output.close();
}
公共静态字符串toCSV(字符串值){
字符串v=null;
布尔doWrap=false;
if(值!=null){
v=值;
如果(v.contains(“\”)){
v=v.replace(“\”,“\”\”);//转义嵌入的双引号
doWrap=真;
}
如果(v.contains(“,”)| | v.contains(“\n”)){
doWrap=真;
}
if(doWrap){
v=“\”“+v+”\”“;//用双引号换行以隐藏逗号
}
}
返回v;
}
公共静态void main(字符串[]args)引发IOException{
试一试{
convertExcelToCSV(“\\C:\\Data\\test.xls”);
}捕获(无效格式){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}

加载excel文件并将其保存到某个较新版本的excel中。然后使用这个新文件进行测试。
并使用最新版本的POI。

谷歌搜索者可能感兴趣的另一个解决方案是使用libre office headless convert,它将在Linux上工作。粘贴和修改代码

                    String[] commandArray = new String[]{
                            "libreoffice"
                            ,"--headless"
                            ,"--convert-to"
                            ,"xlsx:Calc MS Excel 2007 XML"
                            ,"--outdir"
                            ,Paths.get(file.getPath()).getParent().toString()
                            ,file.getPath()
                    };
    Runtime rt = Runtime.getRuntime();

    Process proc = rt.exec(commandArray);
    InputStream stin = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(stin);
    BufferedReader br = new BufferedReader(isr);
    String line;
    System.out.println("input");
    while ((line = br.readLine()) != null) {
            System.out.println(line);
    }
    stin = proc.getErrorStream();
    isr = new InputStreamReader(stin);
    br = new BufferedReader(isr);
    while ((line = br.readLine()) != null) {
            System.out.println(line);
    }
    int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);
                    OPCPackage opcPackage = OPCPackage.open(new File(file.getPath() + "x"));
                    book = new XSSFWorkbook(opcPackage);

这是一条异常消息还是什么?@LuiggiMendoza yea这是一个异常,我想是因为xls格式比较旧,因为当我将其保存为97-2003工作簿时,它可以工作..好的,工作正常,但我将拥有的这些xls文件将是旧格式。。有什么方法可以使用这些格式吗?因为POI无法打开旧格式,请尝试进行一些自动转换。选中此项: