Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Write to Excel file

Posted on 2006-05-23
6
Medium Priority
?
890 Views
Last Modified: 2007-12-19
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();
                  }
            }
}
0
Comment
Question by:johnmarco
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16745292
But you're not writing anything to the file ...
0
 

Author Comment

by:johnmarco
ID: 16745731
So could you please help me how to write the content to the file? When it is written, it is 0 kb.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16746019
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();
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 16746433
you need to pass what you want to save to your save() method
for example if you pass it an arry containing the cell values it would look soemthing like:

public void saveFile(String[][] spreadsheet)
    {        
         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);
                    for (int i=0; i<spreadsheet.length; i++) {
                      for (int j=0; j<spreadsheet[i].length; j++) {
                         bwriter.write(spreadsheet[i][j]);
                         if (j==spreadsheet.length-1)
                           bwriter.write("\n");
                         else
                           bwriter.write("\t");
                    bwriter.close();
               }
               catch (IOException ioe)
               {
                    ioe.printStackTrace();
               }
          }
}

If you want to save it in excel format then see the following:

http://www.rgagnon.com/javadetails/java-0516.html


0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16747316
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
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 16749930
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();
               }
          }
}
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question