|
@@ -23,92 +23,110 @@ import java.util.Map;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
public class Exporter {
|
|
public class Exporter {
|
|
- private String rootPath = "temp";
|
|
|
|
- private MongoTemplate mongoTemplate;
|
|
|
|
- private GridFsTemplate gridFsTemplate;
|
|
|
|
|
|
+ private String rootPath = "temp";
|
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
|
+ private GridFsTemplate gridFsTemplate;
|
|
|
|
|
|
- public Exporter() {
|
|
|
|
- this.mongoTemplate = SpringContextUtil.getBean(MongoTemplate.class);
|
|
|
|
- this.gridFsTemplate = SpringContextUtil.getBean(GridFsTemplate.class);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public String generateFile(
|
|
|
|
- Fields fields, String filename, String filePath, MongoCursor<Document> cursor)
|
|
|
|
- throws IOException {
|
|
|
|
- LinkedHashMap<String, String> headers = fields.getFields();
|
|
|
|
- LinkedHashMap<String, String> headersReverse = fields.getFieldsReverse();
|
|
|
|
- int currentRowRead = 0;
|
|
|
|
- int currentSheetNum = 1;
|
|
|
|
- Workbook wb = new SXSSFWorkbook(1000); // 关键语句
|
|
|
|
- CreationHelper createHelper = wb.getCreationHelper();
|
|
|
|
- Sheet sheet = null; // 工作表对象
|
|
|
|
- Row cRow = null; // 行对象
|
|
|
|
- // 创建sheet
|
|
|
|
- if (sheet == null) {
|
|
|
|
- sheet = wb.createSheet("sheet" + currentSheetNum);
|
|
|
|
- }
|
|
|
|
- // 创建表头
|
|
|
|
- cRow = sheet.createRow(0);
|
|
|
|
- int hcIndex = 0;
|
|
|
|
- for (Map.Entry<String, String> entry : headersReverse.entrySet()) {
|
|
|
|
- String key = entry.getKey();
|
|
|
|
- Cell cell = cRow.createCell(hcIndex);
|
|
|
|
- cell.setCellValue(key);
|
|
|
|
- hcIndex++;
|
|
|
|
|
|
+ public Exporter() {
|
|
|
|
+ this.mongoTemplate = SpringContextUtil.getBean(MongoTemplate.class);
|
|
|
|
+ this.gridFsTemplate = SpringContextUtil.getBean(GridFsTemplate.class);
|
|
}
|
|
}
|
|
- currentRowRead++;
|
|
|
|
- // 创建内容
|
|
|
|
- Document doc;
|
|
|
|
- while (cursor.hasNext()) {
|
|
|
|
- doc = cursor.next();
|
|
|
|
- cRow = sheet.createRow(currentRowRead);
|
|
|
|
- int cInsert = 0;
|
|
|
|
- // 遍历表头并且插入数据,默认全部为字符串
|
|
|
|
- for (String alias : headers.keySet()) {
|
|
|
|
- Cell cell = cRow.createCell(cInsert);
|
|
|
|
- Object value = doc.get(alias);
|
|
|
|
- if (value instanceof String) {
|
|
|
|
- cell.setCellValue((String) value);
|
|
|
|
- } else if (value instanceof Number) {
|
|
|
|
- cell.setCellType(CellType.NUMERIC);
|
|
|
|
- cell.setCellValue(Double.parseDouble(String.valueOf(value)));
|
|
|
|
|
|
+
|
|
|
|
+ public String generateFile(
|
|
|
|
+ Fields fields, String filename, String filePath, MongoCursor<Document> cursor)
|
|
|
|
+ throws IOException {
|
|
|
|
+ LinkedHashMap<String, String> headers = fields.getFields();
|
|
|
|
+ LinkedHashMap<String, String> headersReverse = fields.getFieldsReverse();
|
|
|
|
+ int currentRowRead = 0;
|
|
|
|
+ int currentSheetNum = 1;
|
|
|
|
+ Workbook wb = new SXSSFWorkbook(1000); // 关键语句
|
|
|
|
+ Sheet sheet = null; // 工作表对象
|
|
|
|
+ Row cRow = null; // 行对象
|
|
|
|
+ // 创建sheet
|
|
|
|
+ if (sheet == null) {
|
|
|
|
+ sheet = wb.createSheet("sheet" + currentSheetNum);
|
|
|
|
+ }
|
|
|
|
+ // 创建表头
|
|
|
|
+ cRow = sheet.createRow(0);
|
|
|
|
+ int hcIndex = 0;
|
|
|
|
+ for (Map.Entry<String, String> entry : headersReverse.entrySet()) {
|
|
|
|
+ String key = entry.getKey();
|
|
|
|
+ Cell cell = cRow.createCell(hcIndex);
|
|
|
|
+ cell.setCellValue(key);
|
|
|
|
+ hcIndex++;
|
|
|
|
+ }
|
|
|
|
+ currentRowRead++;
|
|
|
|
+ // 创建内容
|
|
|
|
+ Document doc;
|
|
|
|
+ while (cursor.hasNext()) {
|
|
|
|
+ doc = cursor.next();
|
|
|
|
+ cRow = sheet.createRow(currentRowRead);
|
|
|
|
+ int cInsert = 0;
|
|
|
|
+ // 遍历表头并且插入数据,默认全部为字符串
|
|
|
|
+ for (String alias : headers.keySet()) {
|
|
|
|
+ Cell cell = cRow.createCell(cInsert);
|
|
|
|
+ Object value = doc.get(alias);
|
|
|
|
+ if (value instanceof String) {
|
|
|
|
+ cell.setCellValue((String) value);
|
|
|
|
+ } else if (value instanceof Number) {
|
|
|
|
+ cell.setCellType(CellType.NUMERIC);
|
|
|
|
+ cell.setCellValue(Double.parseDouble(String.valueOf(value)));
|
|
|
|
+ }
|
|
|
|
+ cInsert++;
|
|
|
|
+ }
|
|
|
|
+ currentRowRead++;
|
|
}
|
|
}
|
|
- cInsert++;
|
|
|
|
- }
|
|
|
|
- currentRowRead++;
|
|
|
|
|
|
+ // 创建文件夹防止报错
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
+ if (!file.exists()) {
|
|
|
|
+ boolean created = file.mkdirs();
|
|
|
|
+ log.info(created ? "创建文件夹成功" : "创建文件夹失败");
|
|
|
|
+ }
|
|
|
|
+ filePath = filePath + File.separator + filename + ".csv";
|
|
|
|
+ log.info("导出路径:" + filePath);
|
|
|
|
+ FileOutputStream fOut = new FileOutputStream(filePath);
|
|
|
|
+ wb.write(fOut);
|
|
|
|
+ fOut.close();
|
|
|
|
+ wb.close();
|
|
|
|
+ return filePath;
|
|
}
|
|
}
|
|
- // 创建文件夹防止报错
|
|
|
|
- File file = new File(filePath);
|
|
|
|
- if (!file.exists()) {
|
|
|
|
- boolean created = file.mkdirs();
|
|
|
|
- log.info(created ? "创建文件夹成功" : "创建文件夹失败");
|
|
|
|
|
|
+
|
|
|
|
+ public String export(MongoCursor<Document> cursor, String fileId, String filename)
|
|
|
|
+ throws IOException {
|
|
|
|
+ Fields fields =
|
|
|
|
+ mongoTemplate.findOne(new Query(Criteria.where("fileId").is(fileId)), Fields.class);
|
|
|
|
+ String date = DataUtils.currentDate().replaceAll(":", "_");
|
|
|
|
+ filename = filename + date + ".csv";
|
|
|
|
+ if (fields == null) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ String tmpdir = System.getProperty("java.io.tmpdir");
|
|
|
|
+ String filePath = this.generateFile(fields, filename, tmpdir, cursor);
|
|
|
|
+ FileInputStream inputStream = new FileInputStream(filePath);
|
|
|
|
+ ObjectId objectId = gridFsTemplate.store(inputStream, filename);
|
|
|
|
+ inputStream.close();
|
|
|
|
+ // 删除文件
|
|
|
|
+ File file1 = new File(filePath);
|
|
|
|
+ file1.delete();
|
|
|
|
+ return objectId.toHexString();
|
|
}
|
|
}
|
|
- filePath = filePath + File.separator + filename + ".xlsx";
|
|
|
|
- log.info("导出路径:" + filePath);
|
|
|
|
- FileOutputStream fOut = new FileOutputStream(filePath);
|
|
|
|
- wb.write(fOut);
|
|
|
|
- fOut.close();
|
|
|
|
- wb.close();
|
|
|
|
- return filePath;
|
|
|
|
- }
|
|
|
|
|
|
|
|
- public String export(MongoCursor<Document> cursor, String fileId, String filename)
|
|
|
|
- throws IOException {
|
|
|
|
- Fields fields =
|
|
|
|
- mongoTemplate.findOne(new Query(Criteria.where("fileId").is(fileId)), Fields.class);
|
|
|
|
- String date = DataUtils.currentDate().replaceAll(":", "_");
|
|
|
|
- filename = filename + date + ".xlsx";
|
|
|
|
- if (fields == null) {
|
|
|
|
- return "";
|
|
|
|
|
|
+ public String export(MongoCursor<Document> cursor, String fileId, String filename,String tmpdir)
|
|
|
|
+ throws IOException {
|
|
|
|
+ Fields fields =
|
|
|
|
+ mongoTemplate.findOne(new Query(Criteria.where("fileId").is(fileId)), Fields.class);
|
|
|
|
+ String path = tmpdir + File.separator + filename + ".csv";
|
|
|
|
+ if (fields == null) {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ File file1 = new File(path);
|
|
|
|
+ if(file1.exists()){
|
|
|
|
+ file1.delete();
|
|
|
|
+ }
|
|
|
|
+ String filePath = this.generateFile(fields, filename, tmpdir, cursor);
|
|
|
|
+ FileInputStream inputStream = new FileInputStream(filePath);
|
|
|
|
+ ObjectId objectId = gridFsTemplate.store(inputStream, filename);
|
|
|
|
+ inputStream.close();
|
|
|
|
+ return objectId.toHexString();
|
|
}
|
|
}
|
|
- String tmpdir = System.getProperty("java.io.tmpdir");
|
|
|
|
- String filePath = this.generateFile(fields, filename, tmpdir, cursor);
|
|
|
|
- FileInputStream inputStream = new FileInputStream(filePath);
|
|
|
|
- ObjectId objectId = gridFsTemplate.store(inputStream, filename);
|
|
|
|
- inputStream.close();
|
|
|
|
- // 删除文件
|
|
|
|
- File file1 = new File(filePath);
|
|
|
|
- file1.delete();
|
|
|
|
- return objectId.toHexString();
|
|
|
|
- }
|
|
|
|
}
|
|
}
|