Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating an excel file using java??

Status
Not open for further replies.

scimatic

Technical User
Aug 30, 2004
19
US
I want to create an excel file with java and write data to it, here is what I have so far. How do I create new worksheets and add data to them? Is there a better way of doing it?


import java.io.*;
import java.text.DateFormat;
import java.util.Date;
import java.io.File;

public class DiskspaceRpt
{
public static void main(String args[])
throws java.io.IOException
{
Date now = new Date();
String fileName = ("Diskspace_" + DateFormat.getDateInstance(DateFormat.SHORT).format(now)).replace('/', '-') + ".xls";

System.out.println(fileName);

try
{

/* Set the second parameter
true to append, false to overwrite */

FileWriter outfile = new FileWriter(fileName, true);
BufferedWriter out = new BufferedWriter(outfile);

/* Best to write TAB-separated
values so Excel can easily format the
values as cell-contents */

out.write("Some\tExcel\tCell\tValues");
out.newLine();
out.close();
outfile.close();
}
catch (IOException e)
{
e.printStackTrace();
};

System.in.read();
}
}
 
If you want to generate .xls files, you'll need to use an external API like POI, google Java Excel and you will find a bunch.

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top