JavaExcelapi Guide

xiaoxiao2021-03-06  40

This article mainly introduces how JavaExcelapi is used to read and write data in the Excel format file. 1: Reading Spreadsheets JavaExcelapi can read spreads from the current file system or from an input stream; when we read data. This first step is to create a Workbook to this: import java.IO.File; import java.util.date; import jxl. *;

...

Workbook workbook = workbook.getworkbook (new file ("myfile.xls")); 2. When you visit this Workbook, you can access each Sheets like this: Sheet Sheet = Workbook.getsheet (0); Remember the first table's subscript is 0, the second is 1, The three are 2; of course, you can use the name of the table to access the table; 3. With the two steps above, you can access the Cell (cell); use a convenient function: getContents () like this: Cell A1 = Sheet.getcell (0, 0); // Access A1Cell B2 = SHEET. getcell (1, 1); // Access B2Cell C2 = Sheet.Getcell (2, 1); // Access C2

String stringa1 = a1.getContents (); // get a1String stringb2 = b2.getContents (); string stringc2 = c2.getContents ();

// do stuff with the strings etc ... However, the above approach is whether it is SRTING in the CELL; but we know that there are many other types in Excel; if we have to access Cell in accordance with type The content is stored as a data type; the above method is not affected: The following code is in the corresponding position in your spreadsheet; string stringa1 = null; double numberb2 = 0; Date Datec2 = NULL;

Cell A1 = Sheet.Getcell (0, 0); Cell B2 = Sheet.getcell (1, 1); Cell C2 = Sheet.getcell (2, 1);

IF (a1.gettype () == celltype.label) {Labelcell LC = (labelcell) A1; stringa1 = lc.getstring ();

IF (b2.gettype () == cellType.Number) {Numbercell NC = (Numbercell) B2; Numberb2 = b2.getValue ();

IF (c2.gettype () == cellType.date) {datecell DC = (datecell) C2; DATEC2 = C2.Getdate ();

// do stuff with dates and doubles ... Last: Don't look close work thin;

Workbook.close ();

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

New Post(0)