There
are number of formats provided by jxl pakage.
Example
WritableCellFormat
integerFormat = new WritableCellFormat (NumberFormats.INTEGER);
Number
number1 = new Number(1, 0, 3.1459, integerFormat);
sheet.addCell(number1);
WritableCellFormat
floatFormat = new WritableCellFormat (NumberFormats.FLOAT);
Number
number2 = new Number(1, 1, 3.1459, floatFormat);
sheet.addCell(number2);
public class WorkBookNotInitializedException extends RuntimeException{ WorkBookNotInitializedException(){ super("work book is not initialized"); } }
import java.io.IOException; import jxl.*; import jxl.write.*; import java.io.File; public class XLSWritableUtils { WritableWorkbook workbook = null; public XLSWritableUtils(String fileName){ try { workbook = Workbook.createWorkbook(new File(fileName)); } catch (IOException ex) { ex.printStackTrace(); } } private void checkWorkbookInitialized(){ if(workbook == null) throw new WorkBookNotInitializedException(); } public void createSheet(String sheetName, int index){ checkWorkbookInitialized(); workbook.createSheet(sheetName, index); } public void addString(WritableSheet sheet, int column, int row, String data){ Label label = new Label(column, row, data); try{ sheet.addCell(label); } catch(Exception e){ e.printStackTrace(); } } public void addString(String sheetName, int column, int row, String data){ checkWorkbookInitialized(); WritableSheet sheet = workbook.getSheet(sheetName); addString(sheet, column, row, data); } public void addString(int sheetIndex, int column, int row, String data){ checkWorkbookInitialized(); WritableSheet sheet = workbook.getSheet(sheetIndex); addString(sheet, column, row, data); } public void addNumber(WritableSheet sheet, jxl.write.Number number){ try{ sheet.addCell(number); } catch(Exception e){ e.printStackTrace(); } } public void addNumber(String sheetName, jxl.write.Number number){ checkWorkbookInitialized(); WritableSheet sheet = workbook.getSheet(sheetName); addNumber(sheet, number); } public void addNumber(int sheetIndex, jxl.write.Number number){ checkWorkbookInitialized(); WritableSheet sheet = workbook.getSheet(sheetIndex); addNumber(sheet, number); } public void close(){ checkWorkbookInitialized(); try{ workbook.write(); workbook.close(); } catch(Exception e){ } } }
import jxl.write.Number; import jxl.write.NumberFormats; import jxl.write.WritableCellFormat; public class TestUtils { public static void main(String[] args) { String fileName = "data.xls"; XLSWritableUtils utils = new XLSWritableUtils(fileName); utils.createSheet("constants", 0); WritableCellFormat integerFormat = new WritableCellFormat (NumberFormats.INTEGER); WritableCellFormat floatFormat = new WritableCellFormat (NumberFormats.FLOAT); Number number1 = new Number(1, 0, 3.1459, integerFormat); Number number2 = new Number(1, 1, 3.1459, floatFormat); utils.addString("constants", 0, 0, "PI"); utils.addNumber("constants", number1); utils.addString("constants", 0, 1, "PI"); utils.addNumber("constants", number2); utils.close(); } }
No comments:
Post a Comment