java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow JExcel arrow How to create an Excel file
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4100
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to create an Excel file E-mail
User Rating: / 575
PoorBest 

JExcel API provides various classes to create, read, write data to Excel documents at runtime. The required platform is JVM which means the code developed with JExcel can be run on Windows and Linux without any modification.

The example below creates a new document and writes data into different sheets of the new Excel document.

import java.io.*;
import jxl.*;
import java.util.*;
import jxl.Workbook;
import jxl.write.DateFormat;
import jxl.write.Number;

import jxl.write.*;
import java.text.SimpleDateFormat;

class  create
{
  public static void main(String[] args
  {
    try
    {
      String filename = "input.xls";
      WorkbookSettings ws = new WorkbookSettings();
      ws.setLocale(new Locale("en""EN"));
      WritableWorkbook workbook = 
        Workbook.createWorkbook(new File(filename), ws);
      WritableSheet s = workbook.createSheet("Sheet1"0);
      WritableSheet s1 = workbook.createSheet("Sheet1"0);
      writeDataSheet(s);
      writeImageSheet(s1);
      workbook.write();
      workbook.close();      
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    catch (WriteException e)
    {
      e.printStackTrace();
    }
  }

  private static void writeDataSheet(WritableSheet s
    throws WriteException
  {

    /* Format the Font */
    WritableFont wf = new WritableFont(WritableFont.ARIAL, 
      10, WritableFont.BOLD);
    WritableCellFormat cf = new WritableCellFormat(wf);
    cf.setWrap(true);

    /* Creates Label and writes date to one cell of sheet*/
    Label l = new Label(0,0,"Date",cf);
    s.addCell(l);
    WritableCellFormat cf1 = 
      new WritableCellFormat(DateFormats.FORMAT9);

    DateTime dt = 
      new DateTime(0,1,new Date(), cf1, DateTime.GMT);

    s.adCell(dt);
    
    /* Creates Label and writes float number to one cell of sheet*/
    l = new Label(2,0,"Float", cf);
    s.addCell(l);
    WritableCellFormat cf2 = new WritableCellFormat(NumberFormats.FLOAT);
    Number n = new Number(2,1,3.1415926535,cf2);
    s.addCell(n);

    n = new Number(2,2,-3.1415926535, cf2);
    s.addCell(n);

    /* Creates Label and writes float number upto 3 
       decimal to one cell of sheet */
    l = new Label(3,0,"3dps",cf);
    s.addCell(l);
    NumberFormat dp3 = new NumberFormat("#.###");
    WritableCellFormat dp3cell = new WritableCellFormat(dp3);
    n = new Number(3,1,3.1415926535,dp3cell);
    s.addCell(n);

    /* Creates Label and adds 2 cells of sheet*/
    l = new Label(40"Add 2 cells",cf);
    s.addCell(l);
    n = new Number(4,1,10);
    s.addCell(n);
    n = new Number(4,2,16);
    s.addCell(n);
    Formula f = new Formula(4,3"E1+E2");
    s.addCell(f);

    /* Creates Label and multipies value of one cell of sheet by 2*/
    l = new Label(5,0"Multipy by 2",cf);
    s.addCell(l);
    n = new Number(5,1,10);
    s.addCell(n);
    f = new Formula(5,2"F1 * 3");
    s.addCell(f);

    /* Creates Label and divide value of one cell of sheet by 2.5 */
    l = new Label(6,0"Divide",cf);
    s.addCell(l);
    n = new Number(6,112);
    s.addCell(n);
    f = new Formula(6,2"F1/2.5");
    s.addCell(f);
  }

  private static void writeImageSheet(WritableSheet s
    throws WriteException
  {
    /* Creates Label and writes image to one cell of sheet*/    
    Label l = new Label(00"Image");
    s.addCell(l);
    WritableImage wi = new WritableImage(0357new File("image.png"));
    s.addImage(wi);

    /* Creates Label and writes hyperlink to one cell of sheet*/
    l = new Label(0,15"HYPERLINK");
    s.addCell(l);
    Formula f = new Formula(115
      "HYPERLINK(\"http://www.andykhan.com/jexcelapi\", "+
      "\"JExcelApi Home Page\")");
    s.addCell(f);
    
    }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.