USING JAVA TO Process Excel file

xiaoxiao2021-03-06  64

Java Excel API Is A Open Source Project. We can use it to read, create, update excel file.

1. Read Data Sheet From A Excel FileStep 1. Create a Workbook

Import java.io. *;

Import jxl. *;

... ...

Try

{

// Create a Workbook Object from an exist excel file

InputStream IS = New FileInputStream (Sourcefile);

JXL.Workbook RWB = Workbook.getWorkbook (IS);

}

Catch (Exception E)

{

E.PrintStackTrace ();

}

Step 2. Get The Excel Sheet with The Workbook Object.

// Get the first sheet.

Sheet RS = rwb.getsheet (0);

We Also Can Get Heieet with ITS Name.

Step 3. Get the Cell Content from the Sheet.

// Get The Content from First Row, First Col.

Cell C00 = rs.getcell (0, 0);

String strc00 = c00.getContents ();

// Get the content from first row, second col.

Cell C10 = rs.getcell (1, 0);

String strc10 = c10.getContents ();

// Get The Content from Second Row, Second Col.

Cell C11 = rs.getcell (1, 1);

String strc11 = c11.getContents ();

System.out.println ("Cell (0, 0)" "Value:" STRC00 "; TYPE:" C00.GetType ());

System.out.println ("Cell (1, 0)" "Value:" STRC10 "; TYPE:" C10.Gettype ());

System.out.println ("Cell (1, 1)" "Value:" STRC11 "; TYPE:" C11.GETTYPE ());

THE Content and Date. The Return IS ALWAYS A STRING.

IF you know exactly the Type of Content, You May The Following Code.

String strc00 = NULL;

Double STRC10 = 0.00;

Date strc11 = NULL;

Cell C00 = rs.getcell (0, 0);

Cell C10 = rs.getcell (1, 0);

Cell C11 = rs.getcell (1, 1);

IF (c00.gettype () == celltype.label) {

Labelcell labelc00 = (labelcell) C00;

STRC00 = labelc00.getstring ();

}

IF (c10.gettype () == CellType.Number)

{

NMBERCELL NUMC10 = (Numbercell) C10;

STRC10 = Numc10.getValue ();

}

IF (c11.gettype () == CellType.date)

{

Datecell Datec11 = (Datecell) C11;

STRC11 = DATEC11.Getdate ();

}

System.out.println ("Cell (0, 0)" "Value:" STRC00 "; TYPE:" C00.GetType ());

System.out.println ("Cell (1, 0)" "Value:" STRC10 "; TYPE:" C10.Gettype ());

System.out.println ("Cell (1, 1)" "Value:" STRC11 "; TYPE:" C11.GETTYPE ());

WHEN You GE The Cell Object, You May Get The Type of The Cell Through GetType () Method.

WHEN You Finish The Operation with Excel File, You Must Close All The Objects About The Excel, for Example:

Rwb.close ();

Method provided by workbook

Int getnumberofsheets ()

Return The Number of Sheets in Workbook.

JXL.Workbook RWB = jxl.Workbook.getWorkbook (new file (sourcefile);

Int sheets = rwb.getnumberofsheets ();

2. Sheet [] getsheets ()

Return the array of shop.

JXL.Workbook RWB = jxl.Workbook.getWorkbook (new file (sourcefile);

Sheet [] sheets = rwb.getsheets ();

3. String getversion ()

Get the Version The API you are using.

JXL.Workbook RWB = jxl.Workbook.getWorkbook (new file (sourcefile);

String apiversion = rwb.getversion ();

Method provided by Sheet Object.

String getName ()

Return the sheet name.

JXL.Workbook RWB = jxl.Workbook.getWorkbook (new file (sourcefile);

JXL.SHEET RS = rwb.getsheet (0);

String sheetname = rs.getname (); 2. Int getColumns ()

Return the Number of Columns in the Sheet

JXL.Workbook RWB = jxl.Workbook.getWorkbook (new file (sourcefile);

JXL.SHEET RS = rwb.getsheet (0);

INT RSCOLUMNS = rs.getcolumns ();

3. Cell [] getColumn (int Column)

Return all the cells in a column.

JXL.Workbook RWB = jxl.Workbook.getWorkbook (new file (sourcefile);

JXL.SHEET RS = rwb.getsheet (0);

Cell [] Cell = rs.getColumn (0);

4. INT getRows ()

Return the rows count in the sheet

JXL.Workbook RWB = jxl.Workbook.getWorkbook (new file (sourcefile);

JXL.SHEET RS = rwb.getsheet (0);

Int rsRows = rs.getrows ();

5. Cell [] GetRow (int Row)

Return all the cells in a row ..

JXL.Workbook RWB = jxl.Workbook.getWorkbook (new file (sourcefile);

JXL.SHEET RS = rwb.getsheet (0);

Cell [] Cell = rs.getrow (0);

6. Cell getcell (int column, int in)

Return a specific Cell According to the row and colording to the row and col.

JXL.Workbook RWB = jxl.Workbook.getWorkbook (new file (sourcefile);

JXL.SHEET RS = rwb.getsheet (0);

Cell Cell = rs.getcell (0, 0);

2. Create a New Excel Workbook

Import java.io. *;

Import jxl. *;

Import jxl.write. *;

... ...

Try

{

// Create a Workbook Object

//Method 1: Create a Writable Workbook

Jxl.write.writableworkbook wwb = workbook.createworkbook (new file (targetfile);

//Method 2: Pub The Writable Workbook in a Output Stream

/ *

OutputStream OS = New FileoutputStream (TargetFile);

JXL.WRITE.WRITABLEWORK WWB = Workbook.createworkbook (OS);

* /

}

Catch (Exception E)

{

E.PrintStackTrace ();

}

// CREATE Sheet Object.

JXL.WRITE.WRITABLESHEET WS = WWB.CREATESHEET ("Test Sheet 1", 0);

//1.add Label Object

Jxl.write.label labelc = new jxl.write.label (0, 0, "this is a label cell"); ws.addcell (labelc);

// Add a Object with fond formatting

Jxl.write.writablefont wf = new jxl.write.writablefont (WritableFont.Times, 18, Writablefont.bold, true);

Jxl.write.writablecellformat wcff = new jxl.write.writablecellformat (wf);

JXL.WRITE.Label labelcf = new jxl.write.label (1, 0, "this is a label cell", WCFF);

WS.Addcell (labelcf);

// add a oject with color formatting

JXL.WRITE.WRITABLEFONT WFC = New jxl.write.writablefont (WritableFont.Arial, 10, WritableFont.no_Bold, False,

Underlinestyle.no_underline, jxl.format.colour.red;

JXL.WRITE.WRITABECECELLFORMAT WCFFC = New JXL.WRITE.WRITABECECELLFORMAT (WFC);

JXL.WRITE.Label labelcfc = new jxl.write.label (1, 0, "this is a label cell", WCFFC);

WS.Addcell (labelcf);

//2.Add Numeric Object

Jxl.write.number labeln = new jxl.write.Number (0, 1, 3.1415926);

WS.Addcell (labeln);

// Add Numeric Object with a formatting.

JXL.WRITE.NUMBERFORMAT NF = New jxl.write.Numberformat ("#. ##");

JXL.WRITE.WRITABLECELLFORMAT WCFN = New JXL.WRITE.WRITABECELLFORMAT (NF);

Jxl.write.number labelnf = new jxl.write.Number (1, 1, 3.1415926, wcfn);

WS.Addcell (labelnf);

//3.Add Boolean Object

Jxl.write.Boolean labelb = new jxl.write.Boolean (0, 2, false);

WS.Addcell (labelb);

//4.Add DateTime Object

Jxl.write.datetime labeldt = new jxl.write.datetime (0, 3, new java.util.date ());

WS.Addcell (labeldt);

// Add a datetime Object with a formatting

JXL.WRITE.DATEFORMAT DF = New JXL.WRITE.DATEFORMAT ("DD MM YYYY HH: MM: SS");

JXL.WRITE.WRITABECECELLFORMAT WCFDF = New JXL.WRITE.WRITABECELLFORMAT (DF);

Jxl.write.datetime labeldtf = new jxl.write.datetime (1, 3, new java.util.date (), wcfdf); ws.addcell (labeldtf);

// Write the Sheet Object

Wwb.write ();

// Close the Object

WWB.CLOSE ();

3. Copy and update the Sheet // CREATE A WORKBOOK OBJECT

JXL.Workbook RW = jxl.workbook.getWorkbook (New file (sourcefile);

JXL.WRITE.WRITABLEWORKBOOK WWB = Workbook.createworkbook (New File (Targetfile), RW);

// Get the first sheet

JXL.WRITE.WRITABLESHEET WS = WWB.GETSHEET (0);

// Get the first Cell

JXL.WRITE.WRITABECECELL WC = Ws.GetwritableCell (0, 0);

// judget the Type of Cell

IF (wc.gettype () == CellType.label

{

Label L = (label) WC;

L.Setstring ("The Value Has Been Modified.");

}

// Write the workbook

Wwb.write ();

// Close the workbook

WWB.CLOSE ();

Rw.close ();

转载请注明原文地址:https://www.9cbs.com/read-89488.html

New Post(0)