jxl.jar "%JREHOME%\lib\ext" 。
jxl.jar
Installation
JExcelApi comes packaged as a zipped tar file, called something like jexcelapi_2_0.tar.gz.
To unpack on UNIX systems, at the command line type
gunzip jexcelapi_2_0.tar.gz followed by
tar xf jexcelapi_2_0.tar
On Linux systems this can be accomplished within the single command
tar zxf jexcelapi_2_0.tar.gz
On Windows/NT systems, the archive may be unpacked visually using a utility such as Winzip.
:
● Excel 95-2000
● Excel 2000
● 、 、
●
●
。 API Java , Windows , Linux , Excel 。 , API , PNG 。
, jxl.jar, classpath, 。
、
“ .xls” Excel , “ ”, :
(CreateXLS.java):
// Excel
import java.io.*;
import jxl.*;
import jxl.write.*;
public class CreateXLS {
public static void main(String[] args){
try{
//
WritableWorkbook book = Workbook.createWorkbook(new File(" .xls"));
// “ ” , 0
WritableSheet sheet = book.createSheet(" " ,0);
// Label (0,0) test
Label label = new Label(0,0,"test");
//
sheet.addCell(label);
/* Number ,
, , , 789.123 */
//
jxl.write.Number number = new jxl.write.Number(1,0,789.123);
sheet.addCell(number);
book.write();
book.close();
}catch(Exception e){
System.out.println(e);
}
}
}
, Excel 。
、
Excel , , :
// Excel
import java.io.*;
import jxl.*;
public class ReadXLS
{
public static void main(String args[])
{
try
{
Workbook book=
Workbook.getWorkbook(new File(“ .xls”));
//
Sheet sheet=book.getSheet(0);
//
Cell cell1=sheet.getCell(0,0);
String result=cell1.getContents();
System.out.println(result);
book.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
:test
、
jExcelAPI Excel , Excel , , Excel 。 Excel :
// Excel ,
import java.io.*;
import jxl.*;
import jxl.write.*;
public class UpdateXLS
{
public static void main(String args[])
{
try
{
//Excel
Workbook wb=Workbook.getWorkbook(new File(“ .xls”));
// ,
WritableWorkbook book=
Workbook.createWorkbook(new File(“ .xls”),wb);
//
WritableSheet sheet=book.createSheet(“ ”,1);
sheet.addCell(new Label(0,0,” ”));
book.write();
book.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
、
Excel , 、 。
1、
、 、 , WritableFont WritableCellFormat 。 , , , :
WritableFont font1=
new WritableFont(WritableFont.TIMES,16,WritableFont.BOLD); // excel WritableFont font3=new WritableFont(WritableFont.createFont(" _GB2312"),12,WritableFont.NO_BOLD );① WritableCellFormat format1=new WritableCellFormat(font1); ② Label label=new Label(0,0,”data 4 test”,format1) ③ ① : TIMES, 16, 。WritableFont , ,jExcelAPI java-doc , 。 ② WritableCellFormat , , , 。 ③ Label , 。 WritableCellFormat , , , :
//
format1.setAlignment(jxl.format.Alignment.CENTRE);
//
format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
//
format1.setWrap(true);
--------------------------------------------------------------------------------
、
Excel , 、 、 , jExcelAPI 。 , API。
1、
WritableSheet.mergeCells(int m,int n,int p,int q);
(m,n) (p,q) , :
WritableSheet sheet=book.createSheet(“ ”,0);
//
sheet.mergeCells(0,0,5,0);
, 。 , 。
2、
WritableSheet.setRowView(int i,int height);
i+1 , :
// 200
sheet.setRowView(0,200);
WritableSheet.setColumnView(int i,int width);
i+1 , :
// 30
sheet.setColumnView(0,30);
--------------------------------------------------------------------------------
、
public static void write()throws Exception{
WritableWorkbook wwb=Workbook.createWorkbook(new File("c:/1.xls"));
WritableSheet ws=wwb.createSheet("Test Sheet 1",0);
File file=new File("C:\\jbproject\\PVS\\WebRoot\\weekhit\\1109496996281.png");
WritableImage image=new WritableImage(1, 4, 6, 18,file);
ws.addImage(image);
wwb.write();
wwb.close();
}
, ,WritableImage Draw, , , double, x, y, width, height, , , , Draw double, :) , , 。 。
--------------------------------------------------------------------------------
, (InputStream) Excel , jxl Workbook , Sheet , Cell .
InputStream->Workbook->Sheet->Cell, excel
:
String path="c:\\excel.xls";//Excel URL
InputStream is = new FileInputStream(path);// FileInputStream
jxl.Workbook wb = Workbook.getWorkbook(is); //
jxl.Sheet st = wb.getSheet(0);//
Cell cell=st.getCell(0,0);// , A1
String content=cell.getContents();//getContents() Cell
wb.close();//
is.close();//
Sheet getCell(x,y) ,x,y excel .
A1 (0,0),A2 (0,1),D3 (3,2).Excel A,1 ,jxl 0 .
Sheet getRows(),getColumns() , , sheet .
--------------------------------------------------------------------------------
:
Excel jxl.write .
:
OutputStream<-WritableWorkbook<-WritableSheet<-Label
Label Sheet Cell .
:
OutputStream os=new FileOutputStream("c:\\test.xls");// Excel URL
WritableWorkbook wwb = Workbook.createWorkbook(os);//
WritableSheet ws = wwb.createSheet("sheet1", 0);//
Label labelCF=new Label(0, 0, "hello");//
ws.addCell(labelCF);// Label sheet
Label Label(int x, int y,String aString)xy xy,aString .
WritableFont wf = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD, false);//
WritableCellFormat wcfF = new WritableCellFormat(wf);// CellFormat
Label labelCF=new Label(0, 0, "hello");// ,
Label Label(int c, int r, String cont, CellFormat st) , .
wwb.write();//
wwb.close();//
os.close();
OK, , N Excel Excel , .