在我们的日常工作,可能更多的都是导出xlsx类型的表格文件,但是在有某些时候,在项目需要的时候也要我们能够导出csv类型的文件,下面我们就要一种比较原生的方式来进行csv文件的导出,废话不多说,直接上代码:
1.自定义注解(后续使用注解动态导出表头与查询数据字段对应)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CsvHeader {
String value();
}
2.创建实体类(实体类上需要导出的表头上添加注解@CsvHeader)
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.galaxy.reinsurance.infrastructure.general.utils.CsvHeader;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
@Data
public class RealityBillPOJO {
@TableId(value = "id", type = IdType.AUTO)
@CsvHeader("id")
private Integer id;
@CsvHeader("年")
private String accountingYear;
@CsvHeader("月")
private String accountingMonth;
@CsvHeader("号码")
private String reinsuranceContNo;
@CsvHeader("期间")
private String periodBill;
@CsvHeader("机构")
private String manageCom;
//............
//..............其他字段比较多,我不一一展示了,请结合实际情况创建
}
3.创建获取表头的方法
/**
* 结合@CsvHeader注解实现通过实体类获取表头(csv)
* @param clazz
* @return
* @throws IllegalAccessException
*/
public static String getTableHead(Class<?> clazz) throws IllegalAccessException {
Field[] fields = clazz.getDeclaredFields();
StringBuilder headerBuilder = new StringBuilder();
Arrays.stream(fields)
.filter(field -> field.isAnnotationPresent(CsvHeader.class))
.forEach(field -> {
CsvHeader annotation = field.getAnnotation(CsvHeader.class);
headerBuilder.append(annotation.value()).append(",");
});
headerBuilder.setLength(headerBuilder.length() - 1); // 移除最后一个逗号
headerBuilder.append(";\n");
return headerBuilder.toString();
}
4.创建实际表头与数据对应的方法
/**
* 获取具体的数据(csv)
* @param entity
* @return
* @throws IllegalAccessException
*/
public static String getDataLine(Object entity) throws IllegalAccessException {
StringBuilder dataBuilder = new StringBuilder();
Class<?> clazz = entity.getClass();
Field[] fields = clazz.getDeclaredFields();
Arrays.stream(fields)
.filter(field -> field.isAnnotationPresent(CsvHeader.class))
.forEach(field -> {
field.setAccessible(true);
try {
Object value = field.get(entity);
if (value != null) {
if (field.getType() == Date.class) { // 判断是否为 Date 类型
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
value = dateFormat.format(value);
}
dataBuilder.append(value).append(",");
} else {
dataBuilder.append(","); // 添加空字符串
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
});
dataBuilder.setLength(dataBuilder.length() - 1); // 移除最后一个逗号
dataBuilder.append("\n");
return dataBuilder.toString();
}
5.准备工作完毕,实际使用导出csv文件
//获取具体数据进行对应的导出
public void actualBillExport(HttpServletResponse response) {
String fileName = filename + ".csv";
ServletOutputStream fileOut = null;
try {
fileOut = response.getOutputStream();
response.setContentType("text/csv");// 设置文本内容
response.setCharacterEncoding("utf-8");// 设置字符编码
String name = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename=" + name);
// 获取表头,这个是传你对应实体类(xxx.get(Class))
String tableHead = getTableHead(realityBillPOJOS.get(0).getClass());
fileOut.write(tableHead.getBytes(StandardCharsets.UTF_8));
//这里循环的数据到时候请替换成自己实现的数据即可
for (RealityBillPOJO entity : realityBillPOJOS) {
String line = getDataLine(entity);
fileOut.write(line.getBytes(StandardCharsets.UTF_8));
}
fileOut.flush();
} catch (Exception e) {
log.error(e.getMessage());
} finally {
try {
if (fileOut != null) {
fileOut.close();
}
} catch (Exception e) {
log.error("文件流关闭异常!");
}
}
}
6.前端代码(请按照实际使用,不可照搬,因为地址链接,判断返回等可能不相同,但可以进行借鉴!)
billDownload() {
const flag = this.verifyMethod();
if(flag) {
this.$axios.run('/api/v1/billManagement/actualBillDownload', { billGuarter: this.billGuarter, isDownLoad: true }, (res) => {
if(res.type == 'text/csv') {
const aBlob = new Blob([res]);
let url = window.URL.createObjectURL(aBlob);
let link = document.createElement('a');
link.href = url;
let year = this.billGuarter.substring(0,4);
let quarter = this.billGuarter.substring(5,6);
link.setAttribute('download', year+'年第'+ quarter+'季度_账单' +'.csv');
link.click();
} else if (res.type == 'application/json') {
let reader = new FileReader();
var that = this
reader.onload = function (e) {
let readerres = reader.result;
var parseObj = JSON.parse(readerres);
if(res.code == 0 ) {
that.$message({
type: 'success',
message: parseObj.message
});
} else {
that.$message({
type: 'error',
message: parseObj.message
});
}
}
reader.readAsText(res, 'utf-8');
} else {
this.$message({
type: 'success',
message: res.message
});
this.billGuarter = ''
}
});
}
}
7.页面实际效果,下载csv文件成功!