JAVAEXCELAPI Guide (continued)

Source: Internet
Author: User
Tags memory usage first row
Excel creates an Excel file
1: Basic principle: with reading spreadsheet phase; The first step is to create a writable workbook object; like this.
Import Java.io.File;
Import Java.util.Date;
Import jxl.*;
Import jxl.write.*;

...

Writableworkbook workbook = Workbook.createworkbook (new File ("Output.xls"));
The next step is to create a sheets for workbook:
Writablesheet sheet = Workbook.createsheet ("A-sheet", 0); A table named first sheet was created at the beginning of the position.
The rest of the task now is to add elements to the sheet.
It's easy to say: To add 3.14159 to the D5:
Number number = new Number (3, 4, 3.1459);
Sheet.addcell (number);
In this way, you can add as much data as you want, but you need to know that 1th, when you construct a cell, the position of the cell on the worksheet is determined.
Once created, the cell's position cannot be changed, although the contents of the cell can be changed.
2nd, cell positioning is based on the following rules (column, row), and the subscript is starting from 0,
For example, A1 are stored in (0, 0), and B1 are stored in (1, 0).
At last
Don't forget to close the open Excel workbook to free up memory usage, see the following code fragment:
Write to Exel Worksheet
Wwb.write ();

Close an Excel Workbook object
Wwb.close ();
Example:
Code (CREATEXLS.JAVA):

Classes that generate Excel
Import java.io.*;
Import jxl.*;
Import jxl.write.*;

public class Createxls
{
public static void Main (String args[])
{
Try
{
Open File
Writableworkbook book=
Workbook.createworkbook (New File ("Zsa.xls"));

Generates a worksheet named "first page", with parameter 0 indicating that this is the first page
Writablesheet sheet=book.createsheet ("first page", 0);

The named cell position in the constructor of a label object is the first row in the first column (0,0)
and the contents of the cell is test
Label Label=new label (0,0, "Zsa");

Add a defined cell to a worksheet
Sheet.addcell (label);

/* Generate a cell that holds numbers
You must use the full package path of number, otherwise there is a syntax ambiguity
The cell position is the second column, the first row, and the value is 1000.000*/
Jxl.write.Number number = new Jxl.write.Number (1,0,1000.000);
Sheet.addcell (number);

Write data and close file
Book.write ();
Book.close ();

}catch (Exception e)
{
System.out.println (e);
}
}
}

When compiled, an Excel file is generated at the current location.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.