Link to home
Start Free TrialLog in
Avatar of johnmarco
johnmarco

asked on

Write to Excel file

Hi,

I am writing a spreadsheet application, I have a save button, once it is clicked, pop-up window appears and I am trying to save the file with the content of the worksheet.
I sucesfully save the file but the file is empty, the content is not there, here is my code for save action,

Could you please help?
Thanks,


public void saveFile()
    {          
          JFileChooser fc = new JFileChooser();

          int returnVal = fc.showSaveDialog(this);
            if(returnVal == JFileChooser.APPROVE_OPTION)
            {
                  try
                  {
                        FileWriter fwriter = new FileWriter(fc.getSelectedFile());
                        BufferedWriter bwriter = new BufferedWriter(fwriter);
                        bwriter.close();
                  }
                  catch (IOException ioe)
                  {
                        ioe.printStackTrace();
                  }
            }
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

But you're not writing anything to the file ...
Avatar of johnmarco
johnmarco

ASKER

So could you please help me how to write the content to the file? When it is written, it is 0 kb.
I don't know what you want to write, but you need to write *something*

FileWriter fwriter = new FileWriter(fc.getSelectedFile());
BufferedWriter bwriter = new BufferedWriter(fwriter);
bwriter.write("Hello world!");
bwriter.close();
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is a java api for reading/writting excel files.
JExcelAPI at http://www.andykhan.com/jexcelapi/index.html
Jakarta POI project at http://jakarta.apache.org/poi
I have used POI, it is very good.
Phuoc H. Nguyen
Perhaps you could pass the data of your spreadsheet to this function as a string.

public void saveFile(String data)
    {        
         JFileChooser fc = new JFileChooser();

         int returnVal = fc.showSaveDialog(this);
          if(returnVal == JFileChooser.APPROVE_OPTION)
          {
               try
               {
                    FileWriter fwriter = new FileWriter(fc.getSelectedFile());
                    BufferedWriter bwriter = new BufferedWriter(fwriter);
                    bwriter.write(data);
                    bwriter.close();
               }
               catch (IOException ioe)
               {
                    ioe.printStackTrace();
               }
          }
}