Comments (8)
哪个方法呢?
from excelutil.
我是觉得,write2Sheet()中赋值的那一块,可以部份提取出来:(下面是我提取的)
private static void setCellValue(HSSFCell cell, Object value, String pattern) {
String textValue = null;
if (value instanceof Integer) {
int intValue = (Integer) value;
cell.setCellValue(intValue);
} else if (value instanceof Float) {
float fValue = (Float) value;
cell.setCellValue(fValue);
} else if (value instanceof Double) {
double dValue = (Double) value;
cell.setCellValue(dValue);
} else if (value instanceof Long) {
long longValue = (Long) value;
cell.setCellValue(longValue);
} else if (value instanceof Boolean) {
boolean bValue = (Boolean) value;
cell.setCellValue(bValue);
} else if (value instanceof Date) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
textValue = sdf.format(date);
} else {
// 其它数据类型都当作字符串简单处理
String empty = StringUtils.EMPTY;
textValue = value == null ? empty : value.toString();
}
if (textValue != null) {
HSSFRichTextString richString = new HSSFRichTextString(textValue);
cell.setCellValue(richString);
}
if (value instanceof Integer) {
int intValue = (Integer) value;
cell.setCellValue(intValue);
} else if (value instanceof Float) {
float fValue = (Float) value;
cell.setCellValue(fValue);
} else if (value instanceof Double) {
double dValue = (Double) value;
cell.setCellValue(dValue);
} else if (value instanceof Long) {
long longValue = (Long) value;
cell.setCellValue(longValue);
} else if (value instanceof Boolean) {
boolean bValue = (Boolean) value;
cell.setCellValue(bValue);
} else if (value instanceof Date) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
textValue = sdf.format(date);
} else {
// 其它数据类型都当作字符串简单处理
String empty = StringUtils.EMPTY;
textValue = value == null ? empty : value.toString();
}
if (textValue != null) {
HSSFRichTextString richString = new HSSFRichTextString(textValue);
cell.setCellValue(richString);
}
}
from excelutil.
还有一个疑惑,TestExportBean.java中
//用排序的Map且Map的键要与ExcelCell注解的index对应
Map<String,String> map1 = new LinkedHashMap<>();
map1.put("a","姓名");
map1.put("b","年龄");
map1.put("c","性别");
map1.put("d","出生日期");
这里本来就有序了, 为什么还要在Model.java中使用注解index来排序?
看到write2Sheet()方法中是用来反射来取值, 想到map1中本来的key值就是model中的成员变量,可以直接反射直接取值,如map1.put("a","姓名"), 可以通过keySet后得到''a", 然后通过getA()方法取值。
不知道你原始的想法是怎么样的。 我的issue取错标题了,这个才是我的疑惑。-><-
from excelutil.
因为map存储是无序的,无法用来排序
from excelutil.
已根据你的建议,提取出setCellValue方法,谢谢
from excelutil.
谢楼主好意,刚回来,公司不让外网。
Map<String,String> map1 = new LinkedHashMap<>();
map1.put("a","姓名");
map1.put("b","年龄");
map1.put("c","性别");
map1.put("d","出生日期");
这个地方使用的是LinkedHashMap,而且必须使用,我测试过使用hashmap会出现乱序,但是LinkedHashMap的KeySet集合是原序的,引用:Java中keySet()返回值的排序问题。所以建议楼主在write2Sheet()中,直接遍历keyset,使用key反射取值。 楼主可以再看看,我思考再三了。
from excelutil.
不使用 LinkedHashMap 的原因有两点:
1.导出时方法的参数类型已经是Map而不是 LinkedHashMap ,修改后原先已经在使用的项目会编译失败
2.更多的时候,用于导出的List 是从数据库中查出的,顺序可能取决于你SQL中 select a,b,c 或者无法确定顺序
所以此处无法使用 LinkedHashMap
from excelutil.
多谢解释。
from excelutil.
Related Issues (20)
- 导出Excel时,Model对象可以不使用注解指定index
- 应该支持2007以上excel导出 HOT 1
- 能提供一个使用案例吗? HOT 4
- 依赖太多了 HOT 1
- 没有任何使用方法介绍,看不懂怎么用 HOT 1
- 为嘛不把sheet设置成参数 HOT 1
- 表格类型为date,但无值时会报空指针 HOT 1
- 涉及到单元格合并的这个工具可以用吗? HOT 2
- ExcelCell注解中增加cellType HOT 2
- 1.2.4找不到 HOT 4
- 导出时设置表头很变态,为什么不能直接放在注解里?
- 处理数字的时候有点问题 HOT 1
- ExcelUtil.importExcel标题顺序乱了
- 1.2.4的包还没有传到**库吧?
- @ExcelCell.Valid(allowNull = false)
- 如果類型不在指定範圍內,並且沒有默認值
- 可以在excel裡面追加数据么? HOT 2
- Excel里面是0和1,实体类里是Boolean类型
- 在引入到安卓项目后,发生依赖冲突
- 引入安卓项目:Duplicate class org.apache.commons.collections.ArrayStack found in modules commons-beanutils-1.8.3 (commons-beanutils:commons-beanutils:1.8.3) and commons-collections-3.2.1 (commons-collections:commons-collections:3.2.1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from excelutil.