Extreme Component

JDataGrid Programmer Guide > ModelIO

ModelIO - Import and Export TableModel to Multiple Formats

ModelIO imports and exports TableModel to multiple formats, such as csv, html, xls, etc.

Import Model

Code Samples:

File f = new File("tmp.xls");
WorkBook book = ModelIO.readWorkBook(f, xls, null);

Export Model

Code Samples:

File f = new File("tmp.xls");
WorkBook book = createWorkBook();
ModelIO.writeWorkBook(book, "xls", null, f);

CSV Format

CSV format, comma separated format, used in multiple applications and databases. ModelIO can import and export csv format.

Code Samples:

// import csv model
File f = new File("tmp.csv");
Map map = new HashMap(1);
map.put(ModelIO.ENABLE_LOOSE_FORMAT, Boolean.TRUE);
TableModel model = ModelIO.readTableModel(f, "csv", map)

// export csv model
File f = new File("tmp.csv");
Map map = new HashMap(1);
map.put(ModelIO.ENABLE_LOOSE_FORMAT, Boolean.TRUE);
TableModel model = createTableModel();
ModelIO.writeTableModel(model, "csv", map, f);

TAB Separated Format

ModelIO can import and export TAB separated format.

Code Samples:

// import csv model
File f = new File("tmp.txt");
Map map = new HashMap(1);
map.put(ModelIO.ENABLE_LOOSE_FORMAT, Boolean.TRUE);
TableModel model = ModelIO.readTableModel(f, "txt", map)

// export txt model
File f = new File("tmp.txt");
Map map = new HashMap(1);
map.put(ModelIO.ENABLE_LOOSE_FORMAT, Boolean.TRUE);
TableModel model = createTableModel();
ModelIO.writeTableModel(model, "txt", map, f);

HTML Format

ModelIO can import and export web page file. It is very useful for publishing the TableModel to web page.

Code Samples:

// import csv model
File f = new File("tmp.html");
TableModel model = ModelIO.readTableModel(f, "html", null)

// export html model
File f = new File("tmp.html");
ModelIO.writeTableModel(model, "html", null, f);

XLS Format

The ModelIO provides the support for importing or exporting EXCEL file, it requires third-party Java class library. Current both the HSSF of the Apache POI and JExcelAPI are supported.

Download Apache POI HSSf from:

http://jakarta.apache.org/poi/hssf/index.html

Download JExcelAPI from:

http://jexcelapi.sourceforge.net/
Note:Please ensure putting these Java class library in the classpath.

Code Samples:

// import model
File f = new File("tmp.xls");
WorkBook book = ModelIO.readWorkBook(f, xls, null);
// export model
File f = new File("tmp.xls");
WorkBook book = createWorkBook();
ModelIO.writeWorkBook(book, "xls", null, f);


Copyright (c) zfqjava.com
All rights reserved.