Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Laubin729
easyexcel
提交
bd869eda
E
easyexcel
项目概览
Laubin729
/
easyexcel
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
easyexcel
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
bd869eda
编写于
9月 20, 2019
作者:
庄家钜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
支持通过模板填充数据
上级
3ea045c5
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
123 addition
and
2 deletion
+123
-2
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
+75
-2
src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java
...a/com/alibaba/excel/write/metadata/fill/AnalysisCell.java
+47
-0
update.md
update.md
+1
-0
未找到文件。
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
浏览文件 @
bd869eda
...
...
@@ -3,16 +3,19 @@ package com.alibaba.excel.write;
import
java.lang.reflect.Field
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.regex.Pattern
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.ClientAnchor
;
import
org.apache.poi.ss.usermodel.CreationHelper
;
import
org.apache.poi.ss.usermodel.Drawing
;
import
org.apache.poi.ss.usermodel.RichTextString
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.util.CellRangeAddress
;
...
...
@@ -29,10 +32,8 @@ import com.alibaba.excel.metadata.BaseRowModel;
import
com.alibaba.excel.metadata.CellData
;
import
com.alibaba.excel.metadata.Head
;
import
com.alibaba.excel.metadata.property.ExcelContentProperty
;
import
com.alibaba.excel.support.ExcelTypeEnum
;
import
com.alibaba.excel.util.CollectionUtils
;
import
com.alibaba.excel.util.FileUtils
;
import
com.alibaba.excel.util.StringUtils
;
import
com.alibaba.excel.util.WorkBookUtil
;
import
com.alibaba.excel.write.handler.CellWriteHandler
;
import
com.alibaba.excel.write.handler.RowWriteHandler
;
...
...
@@ -40,6 +41,7 @@ import com.alibaba.excel.write.handler.WriteHandler;
import
com.alibaba.excel.write.metadata.WriteSheet
;
import
com.alibaba.excel.write.metadata.WriteTable
;
import
com.alibaba.excel.write.metadata.WriteWorkbook
;
import
com.alibaba.excel.write.metadata.fill.AnalysisCell
;
import
com.alibaba.excel.write.metadata.holder.WriteHolder
;
import
com.alibaba.excel.write.metadata.holder.WriteSheetHolder
;
...
...
@@ -50,6 +52,10 @@ import net.sf.cglib.beans.BeanMap;
*/
public
class
ExcelBuilderImpl
implements
ExcelBuilder
{
private
static
final
String
FILL_PREFIX
=
"${"
;
private
static
final
String
FILL_SUFFIX
=
"}"
;
private
static
final
Pattern
FILL_PATTERN
=
Pattern
.
compile
(
"\\$\\{[^}]+}"
);
private
WriteContext
context
;
public
ExcelBuilderImpl
(
WriteWorkbook
writeWorkbook
)
{
...
...
@@ -103,6 +109,73 @@ public class ExcelBuilderImpl implements ExcelBuilder {
}
}
public
void
fill
(
Object
data
,
WriteSheet
writeSheet
)
{
try
{
if
(
context
.
writeWorkbookHolder
().
getTemplateFile
()
==
null
&&
context
.
writeWorkbookHolder
().
getTemplateInputStream
()
==
null
)
{
throw
new
ExcelGenerateException
(
"Calling the 'fill' method must use a template."
);
}
context
.
currentSheet
(
writeSheet
);
doFill
(
data
);
}
catch
(
RuntimeException
e
)
{
finish
();
throw
e
;
}
catch
(
Throwable
e
)
{
finish
();
throw
new
ExcelGenerateException
(
e
);
}
}
private
void
doFill
(
Object
data
)
{
BeanMap
beanMap
=
BeanMap
.
create
(
data
);
WriteSheetHolder
writeSheetHolder
=
context
.
writeSheetHolder
();
Sheet
sheet
=
writeSheetHolder
.
getSheet
();
Map
<
Integer
,
Integer
>
templateLastRowMap
=
context
.
writeWorkbookHolder
().
getTemplateLastRowMap
();
if
(!
templateLastRowMap
.
containsKey
(
writeSheetHolder
.
getSheetNo
()))
{
throw
new
ExcelGenerateException
(
"The corresponding table cannot be found,sheetNo:"
+
writeSheetHolder
.
getSheetNo
());
}
Map
<
String
,
AnalysisCell
>
analysisCellMap
=
new
HashMap
<
String
,
AnalysisCell
>(
16
);
for
(
int
i
=
0
;
i
<
templateLastRowMap
.
get
(
writeSheetHolder
.
getSheetNo
());
i
++)
{
Row
row
=
sheet
.
getRow
(
i
);
for
(
int
j
=
0
;
j
<
row
.
getLastCellNum
();
j
++)
{
Cell
cell
=
row
.
getCell
(
j
);
String
value
=
cell
.
getStringCellValue
();
if
(
FILL_PATTERN
.
matcher
(
value
).
matches
())
{
AnalysisCell
analysisCell
=
new
AnalysisCell
();
analysisCell
.
setRowIndex
(
i
);
analysisCell
.
setColumnIndex
(
j
);
List
<
String
>
variableList
=
new
ArrayList
<
String
>();
analysisCell
.
setVariableList
(
variableList
);
boolean
matches
=
true
;
while
(
matches
)
{
matches
=
FILL_PATTERN
.
matcher
(
value
).
matches
();
}
}
}
}
if
(
CollectionUtils
.
isEmpty
(
data
))
{
return
;
}
WriteSheetHolder
writeSheetHolder
=
context
.
writeSheetHolder
();
int
newRowIndex
=
writeSheetHolder
.
getNewRowIndexAndStartDoWrite
();
if
(
writeSheetHolder
.
isNew
()
&&
!
writeSheetHolder
.
getExcelWriteHeadProperty
().
hasHead
())
{
newRowIndex
+=
context
.
currentWriteHolder
().
relativeHeadRowIndex
();
}
// BeanMap is out of order,so use fieldList
List
<
Field
>
fieldList
=
new
ArrayList
<
Field
>();
for
(
int
relativeRowIndex
=
0
;
relativeRowIndex
<
data
.
size
();
relativeRowIndex
++)
{
int
n
=
relativeRowIndex
+
newRowIndex
;
addOneRowOfDataToExcel
(
data
.
get
(
relativeRowIndex
),
n
,
relativeRowIndex
,
fieldList
);
}
}
@Override
public
void
finish
()
{
if
(
context
!=
null
)
{
...
...
src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java
0 → 100644
浏览文件 @
bd869eda
package
com.alibaba.excel.write.metadata.fill
;
import
java.util.List
;
/**
* Read the cells of the template while populating the data.
*
* @author Jiaju Zhuang
**/
public
class
AnalysisCell
{
private
int
columnIndex
;
private
int
rowIndex
;
private
List
<
String
>
variableList
;
private
String
prepareData
;
public
int
getColumnIndex
()
{
return
columnIndex
;
}
public
void
setColumnIndex
(
int
columnIndex
)
{
this
.
columnIndex
=
columnIndex
;
}
public
int
getRowIndex
()
{
return
rowIndex
;
}
public
void
setRowIndex
(
int
rowIndex
)
{
this
.
rowIndex
=
rowIndex
;
}
public
List
<
String
>
getVariableList
()
{
return
variableList
;
}
public
void
setVariableList
(
List
<
String
>
variableList
)
{
this
.
variableList
=
variableList
;
}
public
String
getPrepareData
()
{
return
prepareData
;
}
public
void
setPrepareData
(
String
prepareData
)
{
this
.
prepareData
=
prepareData
;
}
}
update.md
浏览文件 @
bd869eda
...
...
@@ -2,6 +2,7 @@
*
降级poi为3.1.7 兼容jdk6
*
新增支持导入、导出支持公式
*
新增支持读取单元格类型、写入指定单元格类型
*
支持通过模板填充数据
# 2.0.0
*
修复当cell为空可能会抛出空指针的bug
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录