Giter Club home page Giter Club logo

Comments (8)

SargerasWang avatar SargerasWang commented on July 16, 2024

哪个方法呢?

from excelutil.

Hlingoes avatar Hlingoes commented on July 16, 2024

我是觉得,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.

Hlingoes avatar Hlingoes commented on July 16, 2024

还有一个疑惑,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.

SargerasWang avatar SargerasWang commented on July 16, 2024

因为map存储是无序的,无法用来排序

from excelutil.

SargerasWang avatar SargerasWang commented on July 16, 2024

已根据你的建议,提取出setCellValue方法,谢谢

from excelutil.

Hlingoes avatar Hlingoes commented on July 16, 2024

谢楼主好意,刚回来,公司不让外网。
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.

SargerasWang avatar SargerasWang commented on July 16, 2024

不使用 LinkedHashMap 的原因有两点:
1.导出时方法的参数类型已经是Map而不是 LinkedHashMap ,修改后原先已经在使用的项目会编译失败
2.更多的时候,用于导出的List 是从数据库中查出的,顺序可能取决于你SQL中 select a,b,c 或者无法确定顺序
所以此处无法使用 LinkedHashMap

from excelutil.

Hlingoes avatar Hlingoes commented on July 16, 2024

多谢解释。

from excelutil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.