第一种 : 导出csv文件
依赖
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
工具类ExportFileUtils
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
public class ExportFileUtils {
private static final Logger logger = Logger.getLogger(ExportFileUtils.class);
/**
* 导出csv文件
* @param head 字段名称
* @param maps 数据源
* @param os 输出流
* @throws Exception
*/
public static void exportCsv(String[] head, List<Map<String, Object>> maps, OutputStream os)
throws Exception {
try {
StringBuffer headLine = new StringBuffer();
for (String key : head){
headLine.append("\"").append(key).append("\"");
headLine.append(",");
}
headLine.append("\r\n");
os.write(headLine.toString().getBytes("GBK"));
for (int i = 0; i < maps.size(); i++) {
Map<String, Object> map = maps.get(i);
StringBuffer line = new StringBuffer();
for (String key : head) {
Object obj = map.get(key);
line.append("\"").append(obj).append("\"");
line.append(",");
}
line.append("\r\n");
os.write(line.toString().getBytes("GBK"));
}
os.flush();
}
catch (Exception e) {
throw e;
}
finally {
if (os != null) {
try {
os.close();
}
catch (IOException e) {
logger.error(e);
}
}
}
}
}
main方法测试
public static void main(String[] args) {
//标题
String[] head = {"一", "二", "三", "四"};
//测试数据
List<Map<String, Object>> maps = new ArrayList<>();
Map<String, Object> map1 = new HashMap<>();
map1.put("一", 1);
map1.put("二", 2);
map1.put("三", 3);
map1.put("四", 4);
Map<String, Object> map2 = new HashMap<>();
map2.put("一", 11);
map2.put("二", 22);
map2.put("三", 33);
map2.put("四", 44);
Map<String, Object> map3 = new HashMap<>();
map3.put("一", 111);
map3.put("二", 222);
map3.put("三", 333);
map3.put("四", 444);
maps.add(map1);
maps.add(map2);
maps.add(map3);
//获取当前路径
String webAppRootPath = System.getProperty("user.dir");
String fileName = "信息导出_" + DateHelper.getToday() + ".csv";
String path = webAppRootPath +"\\" + fileName;
logger.info("# 导出文件路径 " + path + " ########");
File f = new File(path);
try {
OutputStream out = new FileOutputStream(f);
ExportFileUtils.exportCsv(head, maps, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
第二种 : 导出excle文件
依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
工具类
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
/**
* @author wyc
* @date 2019/7/30 20:17
*/
public class ExportUtil {
private static final Logger logger = Logger.getLogger(ExportUtil.class);
public static void export(String[] head,List<Map<String,Object>> maps,OutputStream os){
HSSFWorkbook workBook = new HSSFWorkbook();// 创建一个Excel工作薄
HSSFSheet sheet = workBook.createSheet("sheet1");
HSSFRow headerRow = sheet.createRow(0);// 创建首行,并赋值
HSSFFont headFont = workBook.createFont();
headFont.setFontName("仿宋_GB2312");
headFont.setFontHeightInPoints((short) 12);
HSSFCellStyle style = workBook.createCellStyle();
style.setFont(headFont);
headerRow.setRowStyle(style);
for (int i = 0; i < head.length; i++) {//给首行赋值(设置标题)
headerRow.createCell(i).setCellValue(head[i]);
sheet.setColumnWidth(i, 6000);
}
for (int j = 0; j < maps.size(); j++) {//给数据行赋值
HSSFRow row = sheet.createRow(j+1);
row.createCell(0).setCellValue(maps.get(j).get("一").toString());
row.createCell(1).setCellValue(maps.get(j).get("二").toString());
row.createCell(2).setCellValue(maps.get(j).get("三").toString());
row.createCell(3).setCellValue(maps.get(j).get("四").toString());
}
try {
workBook.write(os);
} catch (IOException e) {
logger.error("",e);
}finally {
try {
if (os != null){
os.close();
}
} catch (IOException e) {
logger.error("",e);
}
}
}
}
main方法测试
public static void main(String[] args) {
String[] head = {"一", "二", "三", "四"};
List<Map<String, Object>> maps = new ArrayList<>();
Map<String, Object> map1 = new HashMap<>();
map1.put("一", 1);
map1.put("二", 2);
map1.put("三", 3);
map1.put("四", 4);
Map<String, Object> map2 = new HashMap<>();
map2.put("一", 11);
map2.put("二", 22);
map2.put("三", 33);
map2.put("四", 44);
Map<String, Object> map3 = new HashMap<>();
map3.put("一", 111);
map3.put("二", 222);
map3.put("三", 333);
map3.put("四", 444);
maps.add(map1);
maps.add(map2);
maps.add(map3);
String webAppRootPath = System.getProperty("user.dir");
String fileName = "信息导出_" + DateHelper.getToday() + ".csv";
String path = webAppRootPath +"\\" + fileName;
logger.info("# 导出文件路径 " + path + " ########");
File f = new File(path);
try {
OutputStream out = new FileOutputStream(f);
ExportUtil.export(head,maps,out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}