service层:
/**
* CSV文件生成方法
* @param head 文件头
* @param dataList 数据列表
* @param outPutPath 文件输出路径
* @param filename 文件名
* @return
*/
public static File createCSVFile(List<Object> head, List<List<Object>> dataList, String outPutPath, String filename) {
File csvFile = null;
BufferedWriter csvWtriter = null;
try {
csvFile = new File(outPutPath + File.separator + filename + ".csv");
File parent = csvFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
csvFile.createNewFile();
// GB2312使正确读取分隔符","
csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
csvFile), "GB2312"), 1024);
// 写入文件头部
writeRow(head, csvWtriter);
// 写入文件内容
for (List<Object> row : dataList) {
writeRow(row, csvWtriter);
}
csvWtriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvWtriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 写一行数据方法
* @param row
* @param csvWriter
* @throws IOException
*/
private static void writeRow(List<Object> row, BufferedWriter csvWriter) throws IOException {
// 写入文件头部
for (Object data : row) {
StringBuffer sb = new StringBuffer();
String rowStr = sb.append("\"").append(data).append("\",").toString();
csvWriter.write(rowStr);
}
csvWriter.newLine();
}
/**
* 定义标题的list:此处你应该查询,然后把标题返回成list
*/
public List<Object> DefinitionTitle(){
List<Object> titleList = new ArrayList<>();
titleList.add("债券票编号");
titleList.add("债券票名称");
titleList.add("发行人代码");
titleList.add("债券票种类");
titleList.add("操作日期");
titleList.add("操作时间");
titleList.add("操作状态");
return titleList;
}
/**
* 数据的list
*/
@Transactional
public List<List<Object>> dataList(){
List<List<Object>> mapList = new ArrayList<>();
//查询sql
List<Bond> bySql = (List<Bond>) basedao.findByHql("from Bond");
if(bySql !=null){
for (int i = 0; i <bySql.size() ; i++) {
List<Object> tempList = new ArrayList<>();
tempList.add(bySql.get(i).getBondNo());
tempList.add(bySql.get(i).getBondName());
tempList.add(bySql.get(i).getIssueId());
tempList.add(bySql.get(i).getBondType());
tempList.add(bySql.get(i).getSystemDate());
tempList.add(bySql.get(i).getSystemTime());
tempList.add(bySql.get(i).getModifyType());
mapList.add(tempList);
}
}
return mapList;
}
controller层:
@ResponseBody
@RequestMapping("/aaa")
public void aaa(HttpServletRequest request) {
// 1. 定义列名的list 也就是你Excel的第一行
// DefinitionTitle()就应该是你查询你的标题的方法或sql
//TODO调用的service层
List<Object> exportData = cptyCorrespService.DefinitionTitle();
// 2. 定义数据,也就是表格第二行开始。
// 正常你查出来的数据应该是 List<Map<String,Object>> 或者 List<实体类> 这样的结构。
// 需要你把以上的List形式转换成List<List<Object>>形式。
//TODO调用的service层
List<List<Object>> datalist = cptyCorrespService.dataList();
//导出文件存储目录
String path = "c:/test/";
String fileName = "文件导出";
//TODO调用的service层
File file = CptyCorrespService.createCSVFile(exportData, datalist, path, fileName);
String fileName2 = file.getName();
System.out.println("文件名称:" + fileName2);
}
转载借鉴内容:
https://blog.csdn.net/lfy609225258/article/details/89519712?utm_source=app
csv导出数字0001变为1的解决方案:
字符分割时有“\”,转变为“\t”;
https://www.cnblogs.com/ae6623/archive/2012/11/20/4416629.html