Spreadsheet Data Model
Return to Documentation.
Load An Excel File
the way to load an Excel file:
src Attribute
The simplest way to display Excel book file is setting Spreadsheet's src attribute to the file path which is a relative URI with respect to the web application root.
<spreadsheet src="/WEB-INF/startzss.xlsx" maxVisibleRows="150" maxVisibleColumns="40" />
API
Spreadsheet also provides API to load an Excel file. ZK Spreadsheet component's Spreadsheet.setSrc(String) can be called to display an Excel file programmatically. Similar to src attribute, this method accepts relative file path.
setSrc()
public class MyComposer extends SelectorComposer<Component> {
@Wire("spreadsheet")
Spreadsheet spreadsheet;
@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
//initialize stuff here
spreadsheet.setSrc("/WEB-INF/books/startzss.xlsx");
}
}
setBook()
In case you want to display user uploaded Excel book file or display the same Excel book file shared by multiple users, importer interface along with ZK Spreadsheet Spreadsheet.setBook(Book) API can be used. Normally one would obtain Book instance by importing an Excel book file. Use Importer interface Importer.imports(InputStream, String) API to import Excel book file. It returns Book instance which can be passed to setBook(Book) to display imported Excel book file.
public class MyComposer extends SelectorComposer<Component> {
@Wire("spreadsheet")
Spreadsheet spreadsheet;
@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
//initialize stuff here
loadBook();
}
public void loadBook() throws IOException{
Importer importer = Importers.getImporter();
InputStream inputStream = WebApps.getCurrent().getResourceAsStream("/WEB-INF/books/startzss.xlsx");
Book book = importer.imports(inputStream, "startzss");
spreadsheet.setBook(book);
}
}
This is especially powerful in multi-user collaborative scenario. For example once Excel book file is imported using Importer interface and put into application scope, it can be applied to multiple ZK Spreadsheet components each used by different user. ZK Spreadsheet will propagate any changes made to this Book instance to whichever ZK Spreadsheet component it is applied to and therefore facilitate multiple users to collaborate the same Excel book file.
component api
brief introduce some commonly-used API
- setSelectedSheet(String)
- getBook()
change sheet example
spreadsheet model
- book
- sheet
- cell
- Range