public class Workbook extends Object implements Storable
When writing an Excel file, you must setting the property first and
then add Sheet into Workbook, finally call the writeTo(java.nio.file.Path)
method to perform the write operation. The default file format is open-xml(xlsx).
You can also call the saveAsExcel2003() method to output as 'xls'
format (only supported BIFF8 format, ie excel 97~2003).
The property contains setName(String), setCreator(String),
setCompany(String), setAutoSize(boolean) and setOddFill(Fill)
You can also call setWorkbookWriter(IWorkbookWriter) method to setting
a custom WorkbookWriter to achieve special demand.
The writeTo(java.nio.file.Path) method is a terminating statement, and all settings
placed after this statement will not be reflected in the final Excel file.
A typical example as follow:
new Workbook("{name}", "{author}")
// Print debug logs
.watch(System.out::println)
// Auto size the column width
.setAutoSize(true)
// Add a Worksheet
.addSheet(new ListSheet<ListObjectSheetTest.Item>("{worksheet name}").setData(new ArrayList<>()))
// Add an other Worksheet
.addSheet(new ListMapSheet("{worksheet name}").setData(new ArrayList<>()))
// Write to absolute path '/tmp/{name}.xlsx'
.writeTo(Paths.get("/tmp/"));
Some referer links: POI | Office 365 | DocumentFormat.OpenXml.Spreadsheet Namespace | 介绍 Microsoft Office (2007) Open XML 文件格式
| 构造器和说明 |
|---|
Workbook()
Create a unnamed workbook
EEC finds the 'non-name-file' keyword under the
resources/I18N/message.XXX.properties
file first. |
Workbook(String name)
Create a workbook with the specified name.
|
Workbook(String name,
String creator)
Create a workbook with the specified name and author.
|
| 限定符和类型 | 方法和说明 |
|---|---|
Workbook |
addSheet(List<?> data,
Column... columns)
|
Workbook |
addSheet(PreparedStatement ps,
Column... columns)
Add a
StatementSheet to the tail with header Column setting. |
Workbook |
addSheet(PreparedStatement ps,
ParamProcessor pp,
Column... columns)
Add a
StatementSheet to the tail with header Column setting. |
Workbook |
addSheet(ResultSet rs,
Column... columns)
Add a
ResultSetSheet to the tail with header Column setting. |
Workbook |
addSheet(Sheet sheet)
Add a
Sheet to the tail |
Workbook |
addSheet(String sql,
Column... columns)
Add a
StatementSheet to the tail with header Column setting. |
Workbook |
addSheet(String name,
List<?> data,
Column... columns)
|
Workbook |
addSheet(String sql,
ParamProcessor pp,
Column... columns)
Add a
StatementSheet to the tail with header Column
setting. |
Workbook |
addSheet(String name,
PreparedStatement ps,
Column... columns)
Add a
StatementSheet to the tail with worksheet name
and header Column setting. |
Workbook |
addSheet(String name,
PreparedStatement ps,
ParamProcessor pp,
Column... columns)
Add a
StatementSheet to the tail with worksheet name
and header Column setting. |
Workbook |
addSheet(String name,
ResultSet rs,
Column... columns)
Add a
ResultSetSheet to the tail with worksheet name
and header Column setting. |
Workbook |
addSheet(String name,
String sql,
Column... columns)
Add a
StatementSheet to the tail with worksheet name
and header Column setting. |
Workbook |
addSheet(String name,
String sql,
ParamProcessor pp,
Column... columns)
Add a
StatementSheet to the tail with worksheet name
and header Column setting. |
Workbook |
cancelOddFill()
Cancel the odd-fill style
|
protected void |
checkAndInitWriter()
Check and Create
IWorkbookWriter |
Workbook |
forceExport()
Force export of attributes without
ExcelColumn annotations |
int |
getAutoOdd()
Returns the autoOdd setting
|
Object |
getBind()
Returns the replacement object
|
String |
getCompany()
Returns the company name where the author is employed
|
Core |
getCore()
Returns the basic information about workbook
|
String |
getCreator()
Returns the excel author
|
int |
getForceExport()
Returns the force export
|
I18N |
getI18N()
Returns the
I18N util |
String |
getName()
Returns the workbook name
|
Fill |
getOddFill()
Returns the odd-fill style
|
Sheet |
getSheet(String sheetName)
Return a
Sheet with the specified name
Note: This method can only return the Sheet which name specified when created. |
Sheet |
getSheetAt(int index)
Returns the Sheet of the specified index
|
Sheet[] |
getSheets()
Returns all
Sheet in this workbook |
int |
getSize()
Returns the size of
Sheet in this workbook |
SharedStrings |
getSst()
Returns the Shared String Table
|
Styles |
getStyles()
Returns the global
Styles |
InputStream |
getTemplate()
Returns the template io-stream
|
WaterMark |
getWaterMark()
Returns the
WaterMark |
IWorkbookWriter |
getWorkbookWriter()
Returns the
IWorkbookWriter |
protected void |
init()
Create some global entry.
|
Workbook |
insertSheet(int index,
Sheet sheet)
Insert a
Sheet at the specified index |
boolean |
isAutoSize()
Returns whether to auto-adjust the column width
|
Workbook |
remove(int index)
Remove the
Sheet from the specified index |
Workbook |
saveAsCSV()
Save file as Comma-Separated Values.
|
Workbook |
saveAsExcel2003()
Save as excel97~2003
You mast add eec-e3-support.jar into class path to support excel97~2003
|
Workbook |
setAutoSize(boolean autoSize)
Setting auto-adjust the column width
|
Workbook |
setCompany(String company)
Setting the name of the company where the author is employed
|
Workbook |
setConnection(Connection con)
Setting the database
Connection
EEC does not actively close the database connection,
and needs to be manually closed externally. |
Workbook |
setCore(Core core)
Setting basic information,such as title, subject, keyword, category...
|
Workbook |
setCreator(String creator)
Setting the excel author name.
|
Workbook |
setName(String name)
Setting the workbook name
|
Workbook |
setOddFill(Fill fill)
Setting the odd-fill style, default fill color is #E2EDDA
|
Workbook |
setWaterMark(WaterMark waterMark)
Setting
WaterMark
Use WaterMark.of(java.lang.String) method to create a water mark. |
Workbook |
setWorkbookWriter(IWorkbookWriter workbookWriter)
Setting a customize workbook writer
|
protected Path |
template() |
Workbook |
watch(Watch watch)
Setting a
Watch |
void |
what(String code)
Output the export detail info
|
void |
what(String code,
String... args)
Output export detail info
|
Workbook |
withTemplate(InputStream is,
Object o)
Bind a excel template and set an object to replace the
placeholder character in template
|
void |
writeTo(File file)
Export the workbook to the specified folder
|
void |
writeTo(OutputStream os)
Export the workbook to the specified
OutputStream. |
void |
writeTo(Path path)
Export the workbook to the specified folder
If the path is a folder, save the workbook to that folder,
if there has duplicate file name, add '(n)' after the output
file name to avoid overwriting the original file.
|
public Workbook()
resources/I18N/message.XXX.properties
file first. If there contains the keyword, use this value as the default file name,
otherwise use 'Non name' as the name.public Workbook(String name)
name - the workbook namepublic String getName()
public Workbook setName(String name)
name - the workbook nameWorkbookpublic int getAutoOdd()
public String getCreator()
public String getCompany()
public int getSize()
Sheet in this workbookpublic Workbook setCore(Core core)
core - the Core instancepublic SharedStrings getSst()
SharedStringspublic Workbook setWaterMark(WaterMark waterMark)
WaterMark
Use WaterMark.of(java.lang.String) method to create a water mark.
waterMark - the water markWorkbookpublic Workbook setConnection(Connection con)
Connection
EEC does not actively close the database connection,
and needs to be manually closed externally. The Statement
and ResultSet generated inside this EEC will
be actively closed.
con - the database connectionWorkbookpublic Workbook setAutoSize(boolean autoSize)
autoSize - boolean valueWorkbookpublic boolean isAutoSize()
public Workbook forceExport()
ExcelColumn annotationsWorkbookpublic int getForceExport()
public Workbook setCreator(String creator)
If you do not set the creator it will get the current OS login user name, usually this is not a good idea. Applications are usually publish on server or cloud server, getting the system login user name doesn't make sense. If you don't want to set it and default setting the system login user name, you can set it to an empty string ("").
Does anyone agree with this idea? If anyone agrees, I will consider removing this setting.
creator - the author nameWorkbookpublic Workbook setCompany(String company)
company - the company nameWorkbookpublic Workbook setOddFill(Fill fill)
public IWorkbookWriter getWorkbookWriter()
IWorkbookWriterpublic Workbook addSheet(Sheet sheet)
Sheet to the tailsheet - a WorksheetWorkbookpublic Workbook addSheet(List<?> data, Column... columns)
ListSheet to the tail with header Column setting.
Also you can use addSheet(new ListSheet<>(data, columns)
to achieve the same effect.data - List<?> datacolumns - the header columnsWorkbookpublic Workbook addSheet(String name, List<?> data, Column... columns)
ListSheet to the tail with Worksheet name
and header Column setting. Also you can use
addSheet(new ListSheet<>(name, data, columns)
to achieve the same effect.name - the name of worksheetdata - List<?> datacolumns - the header columnsWorkbookpublic Workbook addSheet(ResultSet rs, Column... columns)
ResultSetSheet to the tail with header Column setting.
Also you can use addSheet(new ResultSetSheet(rs, columns)
to achieve the same effect.public Workbook addSheet(String name, ResultSet rs, Column... columns)
ResultSetSheet to the tail with worksheet name
and header Column setting. Also you can use
addSheet(new ResultSetSheet(name, rs, columns)public Workbook addSheet(String sql, Column... columns) throws SQLException
StatementSheet to the tail with header Column setting.
Also you can use addSheet(new StatementSheet(connection, sql, columns)
to achieve the same effect.sql - the query SQL stringcolumns - the header columnsWorkbookSQLException - if a database access error occurspublic Workbook addSheet(String name, String sql, Column... columns) throws SQLException
StatementSheet to the tail with worksheet name
and header Column setting. Also you can use
addSheet(new StatementSheet(name, connection, sql, columns)
to achieve the same effect.name - the worksheet namesql - the query SQL stringcolumns - the header columnsWorkbookSQLException - if a database access error occurspublic Workbook addSheet(String sql, ParamProcessor pp, Column... columns) throws SQLException
StatementSheet to the tail with header Column
setting. The ParamProcessor is a sql parameter replacement
function-interface to replace "?" in the sql string.
Also you can use addSheet(new StatementSheet(connection, sql, paramProcessor, columns)
to achieve the same effect.
workbook.addSheet("users", "select id, name from users where `class` = ?"
, ps -> ps.setString(1, "middle") ...sql - the query SQL stringpp - the sql parameter replacement function-interfacecolumns - the header columnsWorkbookSQLException - if a database access error occurspublic Workbook addSheet(String name, String sql, ParamProcessor pp, Column... columns) throws SQLException
StatementSheet to the tail with worksheet name
and header Column setting. The ParamProcessor
is a sql parameter replacement function-interface to replace "?" in
the sql string.
Also you can use addSheet(new StatementSheet(name, connection, sql, paramProcessor, columns)
to achieve the same effect.
workbook.addSheet("users", "select id, name from users where `class` = ?"
, ps -> ps.setString(1, "middle") ...name - the worksheet namesql - the query SQL stringpp - the sql parameter replacement function-interfacecolumns - the header columnsWorkbookSQLException - if a database access error occurspublic Workbook addSheet(PreparedStatement ps, Column... columns) throws SQLException
StatementSheet to the tail with header Column setting.
Also you can use addSheet(new StatementSheet(null, columns).setPs(ps)
to achieve the same effect.ps - the PreparedStatementcolumns - the header columnsWorkbookSQLException - if a database access error occurspublic Workbook addSheet(String name, PreparedStatement ps, Column... columns) throws SQLException
StatementSheet to the tail with worksheet name
and header Column setting. Also you can use
addSheet(new StatementSheet(name, columns).setPs(ps)
to achieve the same effect.name - the worksheet nameps - the PreparedStatementcolumns - the header columnsWorkbookSQLException - if a database access error occurspublic Workbook addSheet(PreparedStatement ps, ParamProcessor pp, Column... columns) throws SQLException
StatementSheet to the tail with header Column setting.ps - the PreparedStatementpp - the sql parameter replacement function-interfacecolumns - the header columnsWorkbookSQLException - if a database access error occurspublic Workbook addSheet(String name, PreparedStatement ps, ParamProcessor pp, Column... columns) throws SQLException
StatementSheet to the tail with worksheet name
and header Column setting.
workbook.addSheet("users", ps, ps -> ps.setString(1, "middle") ...
name - the worksheet nameps - PreparedStatementpp - the sql parameter replacement function-interfacecolumns - the header columnsWorkbookSQLException - if a database access error occurspublic Workbook insertSheet(int index, Sheet sheet)
Sheet at the specified indexindex - the index (zero-base) to insert atsheet - a worksheetWorkbookpublic Workbook remove(int index)
Sheet from the specified indexindex - the index (zero-base) to be deleteWorkbookpublic Sheet getSheetAt(int index)
index - the index (zero-base)SheetIndexOutOfBoundsException - if index is negative number
or greater than the worksheet size in current workbookpublic Sheet getSheet(String sheetName)
Sheet with the specified name
Note: This method can only return the Sheet which name specified when created.
sheetName - the sheet nameSheet, returns null if not foundpublic Workbook saveAsExcel2003() throws OperationNotSupportedException
You mast add eec-e3-support.jar into class path to support excel97~2003
WorkbookOperationNotSupportedException - if eec-e3-support not import into class pathpublic Workbook saveAsCSV()
Workbookpublic void what(String code)
code - the message code in message properties filepublic void what(String code, String... args)
code - the message code in message properties fileargs - the placeholder valuespublic void writeTo(Path path) throws IOException
If the path is a folder, save the workbook to that folder, if there has duplicate file name, add '(n)' after the output file name to avoid overwriting the original file. If it is a file, overwrite the file.
writeTo 在接口中 Storablepath - the output pat, It can be a directory or a
full file pathIOException - if I/O error occurpublic void writeTo(OutputStream os) throws IOException, ExcelWriteException
OutputStream.
It mostly used for small excel file export and download
public void export(HttpServletResponse response) throws IOException {
String fileName = java.net.URLEncoder.encode("{name}.xlsx", "UTF-8");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"; filename*=utf-8''" + fileName);
new Workbook("{name}", "{author}")
.setAutoSize(true)
.addSheet(new ListSheet<ListObjectSheetTest.Item>("{worksheet name}", new ArrayList<>()))
// Write to HttpServletResponse
.writeTo(response.getOutputStream());
}os - the OutputStreamIOException - if I/O error occurExcelWriteException - other runtime errorpublic void writeTo(File file) throws IOException, ExcelWriteException
file - the output file nameIOException - if I/O error occurExcelWriteException - other runtime errorpublic InputStream getTemplate()
public Object getBind()
public Workbook withTemplate(InputStream is, Object o)
is - the template io-streamo - bind a replacement objectWorkbookprotected Path template() throws IOException
IOExceptionpublic Workbook setWorkbookWriter(IWorkbookWriter workbookWriter)
workbookWriter - a customize IWorkbookWriterWorkbookprotected void init()
protected void checkAndInitWriter()
IWorkbookWriterCopyright © 2022. All rights reserved.