生成CVS文件,分页查询写入文件

本文介绍了一个报表任务处理方法,该方法通过分页查询的方式从数据库中获取数据,并将这些数据导出为CSV文件。文章详细展示了如何使用反射机制获取对象属性,并根据属性上的注解来判断是否需要导出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * 
 * @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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值