Import Java.io.File;
import java.util.*;
import jxl.*;
import jxl.write.*;
/**
* <p>title: Manipulating Excel files </p>
* <p>description: This example demonstrates the use of JXL packages to implement operations on Excel files </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:myExcel.java</p>
* @version 1.0
*/
public class myexcel{
Workbook workbook;
Sheet Sheet;
/**
*<br> Method Description: Write file Operation
*<br> input Parameters:
*<br> return type:
*/
public void Write () {
try{
//Create a writable Excel file object
Writableworkbook workbook = Workbook.createworkbook (new File ("Myfile.xls"));
//Using the first sheet, name it "lunch record"
Writablesheet sheet = workbook.createsheet ("Lunch Record", 0);
//table header
Label label0 = new label (0, 0, "time");
Sheet.addcell (label0);
Label Label1 = new label (1, 0, "name");
Sheet.addcell (Label1);
Label Label2 = new label (2, 0, "Lunch standard");
Sheet.addcell (Label2);
Label label3 = new label (3, 0, "actual cost");
Sheet.addcell (LABEL3);
//Format Date
Jxl.write.DateFormat df = new Jxl.write.DateFormat ("yyyy-dd-mm hh:mm:ss");
Jxl.write.WritableCellFormat wcfdf = new Jxl.write.WritableCellFormat (DF);
jxl.write.DateTime LABELDTF = new Jxl.write.DateTime (0, 1, New Java.util.Date (), WCFDF);
Sheet.addcell (LABELDTF);
//Ordinary character
Label LABELCFC = new Label (1, 1, "Riverwind");
Sheet.addcell (LABELCFC);
//Format digital
Jxl.write.NumberFormat NF = new Jxl.write.NumberFormat ("#.##");
Writablecellformat WCFN = new Writablecellformat (NF);
jxl.write.Number labelnf = new Jxl.write.Number (2, 1, 13.1415926, WCFN);
Sheet.addcell (LABELNF);
jxl.write.Number labelnnf = new Jxl.write.Number (3, 1, 10.50001, WCFN);
Sheet.addcell (LABELNNF);
//Close objects, releasing resources
Workbook.write ();
Workbook.close ();
}catch (Exception e) {
System.out.println (e);
}
}
/**
*<br> Method Description: Reading an Excel file row of data
*<br> input parameter: The number of rows specified by int row
*<br> return type: String() result array
*/
public string[] ReadLine (int row) {
try{
//Get the number of data table columns
int colnum = Sheet.getcolumns ();
string[] rest = new String[colnum];
for (int i = 0; i < Colnum; i++) {
String stemp = Read (I,row);
if (stemp!=null)
Rest[i] = stemp;
}
return rest;
}catch (Exception e) {
System.out.println ("ReadLine err:" +e);
Workbook.close ();
return null;
}
}
/**
*<br> Method Description: Reads the specified cell data for Excel
*<br> input Parameters:
*<br> return type:
*/
public String Read (int col, int row) {
try{
//Get cell data
Cell a2 = Sheet.getcell (Col,row);
String rest = a2.getcontents ();
return rest;
}catch (Exception e) {
System.out.println ("Read err:" +e);
Workbook.close ();
return null;
}
}
/**
*<br> Method Description: Main method, demo program with
*<br> input Parameters:
*<br> return type:
*/
public static void Main (string[] arges) {
try{
myexcel me = new Myexcel ();
//Generate a readable Excel file object
Me.workbook = Workbook.getworkbook (New File ("Myfile.xls"));
//Use first worksheet
me.sheet = me.workbook.getSheet (0);
//Read a line of records and show it
string[] sstemp = me.readline (1);
for (int i=0;i<sstemp.length;i++)
System.out.println (Sstemp[i]);
//Write Data
Me.write ();
Me.workbook.close ();
}catch (Exception e) {
System.out.println (e);
}
}
}