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 setZebraLine(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<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 |
addContentType(ContentType.Type type)
Add a content-type
|
Workbook |
addContentTypeRel(Relationship rel)
Add a content-type refer
|
Workbook |
addSheet(List<?> data,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
addSheet(PreparedStatement ps,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
addSheet(PreparedStatement ps,
ParamProcessor pp,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
addSheet(ResultSet rs,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
addSheet(Sheet sheet)
Add a
Sheet to the tail |
Workbook |
addSheet(String sql,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
addSheet(String name,
List<?> data,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
addSheet(String sql,
ParamProcessor pp,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
addSheet(String name,
PreparedStatement ps,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
addSheet(String name,
PreparedStatement ps,
ParamProcessor pp,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
addSheet(String name,
ResultSet rs,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
addSheet(String name,
String sql,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
addSheet(String name,
String sql,
ParamProcessor pp,
Column... columns)
已过时。
use
addSheet(Sheet) |
Workbook |
cancelOddFill()
已过时。
rename to
cancelZebraLine() |
Workbook |
cancelZebraLine()
Cancel the zebra-line style
|
protected void |
checkAndInitWriter()
Check and Create
IWorkbookWriter |
Workbook |
defaultZebraLine()
Setting zebra-line style, the default fill color is #EFF5EB
|
Workbook |
forceExport()
Force export of attributes without
ExcelColumn annotations |
int |
getAutoOdd()
已过时。
replace with
getZebraFill() != null |
Object |
getBind()
Returns the replacement object
|
String |
getCompany()
Returns the company name where the author is employed
|
ContentType |
getContentType()
Returns the global ContentType
|
Core |
getCore()
Returns the basic information about workbook
|
String |
getCreator()
Returns the excel author
|
int |
getDrawingCounter()
Returns count of drawing object
|
int |
getForceExport()
Returns the force export
|
I18N |
getI18N()
Returns the
I18N util |
int |
getMediaCounter()
Returns count of media object
|
String |
getName()
Returns the workbook name
|
Fill |
getOddFill()
已过时。
rename to
getZebraFill() |
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 |
Fill |
getZebraFill()
Returns the zebra-line fill style
|
boolean |
hasZebraFill()
Returns current workbook has zebra-line
|
int |
incrementDrawingCounter()
Increment and returns drawing-counter
|
int |
incrementMediaCounter()
Increment and returns media-counter
|
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)
已过时。
insecurity
|
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)
已过时。
rename to
setZebraLine(Fill) |
Workbook |
setStyles(Styles styles)
Specify a custom global
Styles |
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
|
Workbook |
setZebraLine(Fill fill)
Setting the zebra-line fill style
|
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 nameWorkbook@Deprecated public int getAutoOdd()
getZebraFill() != nullpublic String getCreator()
public String getCompany()
@Deprecated public Fill getOddFill()
getZebraFill()Fill stylepublic 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 markWorkbook@Deprecated public 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 setStyles(Styles styles)
Stylesstyles - custom StylesWorkbookpublic 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 nameWorkbook@Deprecated public Workbook cancelOddFill()
cancelZebraLine()Workbook@Deprecated public Workbook setOddFill(Fill fill)
setZebraLine(Fill)public Workbook defaultZebraLine()
Workbookpublic boolean hasZebraFill()
public IWorkbookWriter getWorkbookWriter()
IWorkbookWriterpublic Workbook addSheet(Sheet sheet)
Sheet to the tailsheet - a WorksheetWorkbook@Deprecated public Workbook addSheet(List<?> data, Column... columns)
addSheet(Sheet)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 columnsWorkbook@Deprecated public Workbook addSheet(String name, List<?> data, Column... columns)
addSheet(Sheet)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 columnsWorkbook@Deprecated public Workbook addSheet(ResultSet rs, Column... columns)
addSheet(Sheet)ResultSetSheet to the tail with header Column setting.
Also you can use addSheet(new ResultSetSheet(rs, columns)
to achieve the same effect.@Deprecated public Workbook addSheet(String name, ResultSet rs, Column... columns)
addSheet(Sheet)ResultSetSheet to the tail with worksheet name
and header Column setting. Also you can use
addSheet(new ResultSetSheet(name, rs, columns)@Deprecated public Workbook addSheet(String sql, Column... columns) throws SQLException
addSheet(Sheet)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 occurs@Deprecated public Workbook addSheet(String name, String sql, Column... columns) throws SQLException
addSheet(Sheet)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 occurs@Deprecated public Workbook addSheet(String sql, ParamProcessor pp, Column... columns) throws SQLException
addSheet(Sheet)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 occurs@Deprecated public Workbook addSheet(String name, String sql, ParamProcessor pp, Column... columns) throws SQLException
addSheet(Sheet)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 occurs@Deprecated public Workbook addSheet(PreparedStatement ps, Column... columns) throws SQLException
addSheet(Sheet)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 occurs@Deprecated public Workbook addSheet(String name, PreparedStatement ps, Column... columns) throws SQLException
addSheet(Sheet)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 occurs@Deprecated public Workbook addSheet(PreparedStatement ps, ParamProcessor pp, Column... columns) throws SQLException
addSheet(Sheet)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 occurs@Deprecated public Workbook addSheet(String name, PreparedStatement ps, ParamProcessor pp, Column... columns) throws SQLException
addSheet(Sheet)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()
IWorkbookWriterpublic Workbook addContentType(ContentType.Type type)
type - ContentType.TypeWorkbookpublic Workbook addContentTypeRel(Relationship rel)
rel - RelationshipWorkbookpublic ContentType getContentType()
ContentTypepublic int incrementDrawingCounter()
public int getDrawingCounter()
public int incrementMediaCounter()
public int getMediaCounter()
Copyright © 2023. All rights reserved.