0

请看下面的代码。我在用JExcel API

import java.io.*;
import jxl.*;
import java.util.*;

class  ConvertCSV
{
  public static void main(String[] args) 
  {
    try
    {
      //File to store data in form of CSV
      File f = new File("input.csv");

      OutputStream os = (OutputStream)new FileOutputStream(f);
      String encoding = "UTF8";
      OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
      BufferedWriter bw = new BufferedWriter(osw);

      //Excel document to be imported
      String filename = "C:/Users/yohan/Documents/NetBeansProjects/ExcelTest/input.xlsx";
      WorkbookSettings ws = new WorkbookSettings();
      ws.setLocale(new Locale("en", "EN"));
      Workbook w = Workbook.getWorkbook(new File(filename),ws);

      // Gets the sheets from workbook
      for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++)
      {
        Sheet s = w.getSheet(sheet);

        bw.write(s.getName());
        bw.newLine();

        Cell[] row = null;

        // Gets the cells from sheet
        for (int i = 0 ; i < s.getRows() ; i++)
        {
          row = s.getRow(i);

          if (row.length > 0)
          {
            bw.write(row[0].getContents());
            for (int j = 1; j < row.length; j++)
            {
              bw.write(',');
              bw.write(row[j].getContents());
            }
          }
          bw.newLine();
        }
      }
      bw.flush();
      bw.close();
    }
    catch (UnsupportedEncodingException e)
    {
      System.err.println(e.toString());
    }
    catch (IOException e)
    {
      System.err.println(e.toString());
    }
    catch (Exception e)
    {
      System.err.println(e.toString());
    }
  }
}

我不想将数据写入 CSV 文件,但我必须将“字段”加载到名为“filedsBuffer”的 StringBuffer 中,并将数据作为逗号分隔值加载到名为“dataBuffer”的 StringBuffer 中。

但是,当我运行这个程序时,我得到了以下异常。

jxl.read.biff.BiffException: Unable to recognize OLE stream

我该如何解决这个问题?如果我不能用这个 API 做到这一点,请随时使用不同的 API 来回答。请帮忙

4

1 回答 1

2

您可以考虑使用apache-poi。它支持最新版本的 ms-excel。这是可以帮助入门的代码-

于 2012-11-28T05:29:38.680 回答