/**
*
* @param infoReportTask 报表任务(根据任务(登记生成报表条件)所对应类进行分页查询并写入文件)
* @param file 生成文件
*/
private void createCSVFile(InfoReportTask infoReportTask, File file) {
Class cls = Class.forName(infoDdptbtaxReportTask.getReportClass());//生成报表所对应类
MapObject map = BeanUtil.beanToMapObject(infoReportTask);
map.put("reportTime", TimeHelper.parse(infoReportTask.getReportTime(),"yyyyMMdd"));
//查询总记录数,进行分页查询(每页1w行)
int total = sqlSessionTemplate.selectOne(infoReportTask.getReportClass().replace(".entity.", ".mapper.")+"Mapper.countByMap", map);
int limit = Integer.parseInt(this.limit);
int pages = (int) (Math.ceil((double)total/limit));
for (int page = 1; page <= pages; page++) {
PageBounds pageBounds = new PageBounds(page, limit);
List list = sqlSessionTemplate.selectList(infoReportTask.getReportClass().replace(".entity.", ".mapper.")+"Mapper.findByMap", map, pageBounds);
BufferedWriter fileOutputStream = null;
try {
fileOutputStream = new BufferedWriter(new FileWriterWithEncoding(file,"UTF-8",true));
Field[] superFields = cls.getSuperclass().getDeclaredFields();
Field[] fields = cls.getDeclaredFields();
if(page == 1){
boolean first = true;
for (int i = 0; i < superFields.length; i++) {
// 获取属性上的指定类型的注解
Field field = superFields[i];
Annotation annotation = field.getAnnotation(AttributeName.class);
// 有该类型的注解存在(是数据表对应属性)
if (annotation != null && !isIgnored(field.getName())) {
if(!first){
fileOutputStream.write(",");
}else{
first = false;
}
AttributeName attributeName = (AttributeName) annotation;
fileOutputStream.write(attributeName.value());
}
}
for (int i = 0; i < fields.length; i++) {
// 获取属性上的指定类型的注解
Field field = fields[i];
Annotation annotation = field.getAnnotation(AttributeName.class);
// 有该类型的注解存在(是数据表对应属性)
if (annotation != null && !isIgnored(field.getName())) {
if(!first){
fileOutputStream.write(",");
}else{
first = false;
}
AttributeName attributeName = (AttributeName) annotation;
fileOutputStream.write(attributeName.value());
}
}
fileOutputStream.write("\n");
}else{
fileOutputStream.write("\n");
}
// 写入文件内容
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Object obj = iterator.next();
boolean first = true;
for (int i = 0; i < superFields.length; i++) {
// 获取属性上的指定类型的注解
Field field = superFields[i];
field.setAccessible(true);
Annotation annotation = field.getAnnotation(AttributeName.class);
// 有该类型的注解存在(是数据表对应属性)
if (annotation != null && !isIgnored(field.getName())) {
if(!first){
fileOutputStream.write(",");
}else{
first = false;
}
String value = getStringData(field.get(obj),field.getName());
fileOutputStream.write(value);
}
}
for (int i = 0; i < fields.length; i++) {
// 获取属性上的指定类型的注解
Field field = fields[i];
field.setAccessible(true);
Annotation annotation = field.getAnnotation(AttributeName.class);
// 有该类型的注解存在(是数据表对应属性)
if (annotation != null && !isIgnored(field.getName())) {
if(!first){
fileOutputStream.write(",");
}else{
first = false;
}
String value = getStringData(field.get(obj),field.getName());
fileOutputStream.write(value);
}
}
if (iterator.hasNext()) {
fileOutputStream.write("\n");
}
}
fileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
BusinessException.ASSERT.fail(e.getMessage());
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private boolean isIgnored(String fieldName) {
boolean flag = fieldName.equals("id") || fieldName.equals("fileId") || fieldName.equals("headId") || fieldName.equals("version");
return flag;
}
private static String getStringData(Object object, String field) {
String value = "";
if (object == null) {
return value;
} else if (object instanceof String) {
value = (String) object;
} else if (object instanceof Date) {
value = TimeHelper.getDate((Date) object, "yyyy-MM-dd HH:mm:ss");
} else if (object instanceof Integer) {
value = Integer.toString((Integer) object);
} else if (object instanceof Double) {
value = Double.toString((Double) object);
} else {
value = (String) object;
}
value = value.trim();
//防止数字类型变科学计数法或以0开始的数字去除0
if(StringUtils.isNumeric(value.replaceAll(".", "")) || object instanceof Date){
value += "\t";
}
return value;
}
生成CVS文件,分页查询写入文件
最新推荐文章于 2022-11-25 14:57:08 发布