0
votes

I have a quick question. I made program which generates jtable, than you can fill this table with some values, and than save whole table to excel.

At the begging beginning I defined file:

File f = new File("C:\Users\Gregory\Desktop\test.xls");

Here is class which export JTable to excel:

public void exportTable(JTable table, File file) throws IOException { TableModel model = table.getModel(); FileWriter out = new FileWriter(file);

  for (int i = 0; i < model.getColumnCount(); i++) {
      out.write(table.getColumnModel().getColumn(i).getHeaderValue() + \t");
  }
  out.write("\n");
  for (int i = 0; i < model.getRowCount(); i++) {
      for (int j = 0; j < model.getColumnCount(); j++) {
          if (model.getValueAt(i, j) != null) {
              out.write(model.getValueAt(i, j).toString() + "\t");
          } else if (model.getValueAt(i, j) == null) {
              out.write("\t");
          }
         }
      out.write("\n");
  }out.close();

I cannot import this table to java program. I receive the error: jxl.read.biff.BiffException: Unable to recognize OLE stream

When I open this file I have message, that the format of the file is different than its extension (.xls). So I click "save as" and suggested extension is .txt. I save file as .xls (seems to be the same file as saved by java program) and than I can import JTable from that excel to java program without any errors. Can anyone suggest how to save file with right extension?

1
An file with an XLS extension has a custom format. It does not store the data as text. That is why you have the "SaveAs" option. You can convert the custom format to a text format. Try saving the file as a ".csv" (you may need to change your delimiter, or just save it as ".txt". - camickr
But my vision of this program is to export JTable to Excel in the same format as it is in JTable - cell by cell, and than export this cells to program and create JTable. Is it possible? In general - it works as i want but i have to manually click on the file "Save as" and save file as ".xls" - than i can create jtable from this file. - Grzegorz
Have you ever used an editor to look at a ".xls" file??? It does not contain text as you would see in an ".cvs" or ".txt" file. If you want to read the ".xls" spec produced by Microsoft and then create the file, good luck. - camickr

1 Answers

0
votes

In parameter path pass the path of your file that was generated and at the parameter name pass the desired name for your file. If your interest the method signature can be changed to receive the extension dynamically file. Just replace the ".xls" for the parameter.

private void download(HttpServletRequest request, HttpServletResponse response, String path,
        String name) throws IOException
{
    OutputStream out = null;
    try
    {
        out = response.getOutputStream();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    File arquivo = new File(path);
    String sContentType = null;
    sContentType = "application/pdf";
    response.setContentType(sContentType);
    response.addHeader("content-disposition", "attachment; filename=" + name + ".xls");
    InputStream in = null;
    in = new BufferedInputStream(new FileInputStream(arquivo));
    if (in != null)
    {
        response.setContentLength((int) in.available());
        int l;
        byte b[] = new byte[4096];
        while ((l = in.read(b, 0, b.length)) != -1)
        {
            out.write(b, 0, l);
        }
        out.flush();
        in.close();
    }
    out.close();

}