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_OPTIO N)
{
try
{
FileWriter fwriter = new FileWriter(fc.getSelectedF ile());
BufferedWriter bwriter = new BufferedWriter(fwriter);
bwriter.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
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_OPTIO
{
try
{
FileWriter fwriter = new FileWriter(fc.getSelectedF
BufferedWriter bwriter = new BufferedWriter(fwriter);
bwriter.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
But you're not writing anything to the file ...
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.getSelectedF ile());
BufferedWriter bwriter = new BufferedWriter(fwriter);
bwriter.write("Hello world!");
bwriter.close();
FileWriter fwriter = new FileWriter(fc.getSelectedF
BufferedWriter bwriter = new BufferedWriter(fwriter);
bwriter.write("Hello world!");
bwriter.close();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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_OPTIO N)
{
try
{
FileWriter fwriter = new FileWriter(fc.getSelectedF ile());
BufferedWriter bwriter = new BufferedWriter(fwriter);
bwriter.write(data);
bwriter.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public void saveFile(String data)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTIO
{
try
{
FileWriter fwriter = new FileWriter(fc.getSelectedF
BufferedWriter bwriter = new BufferedWriter(fwriter);
bwriter.write(data);
bwriter.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}