|
@@ -15,10 +15,7 @@ import org.springframework.data.mongodb.core.query.Criteria;
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
|
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.FileInputStream;
|
|
|
|
-import java.io.FileOutputStream;
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
+import java.io.*;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
@@ -47,6 +44,8 @@ public class Exporter {
|
|
if (sheet == null) {
|
|
if (sheet == null) {
|
|
sheet = wb.createSheet("sheet" + currentSheetNum);
|
|
sheet = wb.createSheet("sheet" + currentSheetNum);
|
|
}
|
|
}
|
|
|
|
+ filePath = filePath + File.separator + filename + ".csv";
|
|
|
|
+ PrintWriter writer = new PrintWriter(new FileWriter(filePath));
|
|
// 创建表头
|
|
// 创建表头
|
|
cRow = sheet.createRow(0);
|
|
cRow = sheet.createRow(0);
|
|
int hcIndex = 0;
|
|
int hcIndex = 0;
|
|
@@ -54,6 +53,12 @@ public class Exporter {
|
|
String key = entry.getKey();
|
|
String key = entry.getKey();
|
|
Cell cell = cRow.createCell(hcIndex);
|
|
Cell cell = cRow.createCell(hcIndex);
|
|
cell.setCellValue(key);
|
|
cell.setCellValue(key);
|
|
|
|
+ if (cell != null) {
|
|
|
|
+ writer.print(cell.toString());
|
|
|
|
+ if (cellnum < row.getLastCellNum() - 1) {
|
|
|
|
+ writer.print(",");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
hcIndex++;
|
|
hcIndex++;
|
|
}
|
|
}
|
|
currentRowRead++;
|
|
currentRowRead++;
|
|
@@ -68,15 +73,18 @@ public class Exporter {
|
|
Cell cell = cRow.createCell(cInsert);
|
|
Cell cell = cRow.createCell(cInsert);
|
|
Object value = doc.get(alias);
|
|
Object value = doc.get(alias);
|
|
if (value instanceof String) {
|
|
if (value instanceof String) {
|
|
- if(StringUtils.isBlank(String.valueOf(value))||"null".equals(String.valueOf(value))){
|
|
|
|
|
|
+ if (StringUtils.isBlank(String.valueOf(value)) || "null".equals(String.valueOf(value))) {
|
|
cell.setCellValue("");
|
|
cell.setCellValue("");
|
|
- }else {
|
|
|
|
|
|
+ } else {
|
|
cell.setCellValue((String) value);
|
|
cell.setCellValue((String) value);
|
|
}
|
|
}
|
|
} else if (value instanceof Number) {
|
|
} else if (value instanceof Number) {
|
|
cell.setCellType(CellType.NUMERIC);
|
|
cell.setCellType(CellType.NUMERIC);
|
|
cell.setCellValue(Double.parseDouble(String.valueOf(value)));
|
|
cell.setCellValue(Double.parseDouble(String.valueOf(value)));
|
|
}
|
|
}
|
|
|
|
+ if (cell != null) {
|
|
|
|
+ writer.print(cell.toString());
|
|
|
|
+ }
|
|
cInsert++;
|
|
cInsert++;
|
|
}
|
|
}
|
|
currentRowRead++;
|
|
currentRowRead++;
|
|
@@ -87,14 +95,15 @@ public class Exporter {
|
|
boolean created = file.mkdirs();
|
|
boolean created = file.mkdirs();
|
|
log.info(created ? "创建文件夹成功" : "创建文件夹失败");
|
|
log.info(created ? "创建文件夹成功" : "创建文件夹失败");
|
|
}
|
|
}
|
|
- filePath = filePath + File.separator + filename + ".csv";
|
|
|
|
log.info("导出路径:" + filePath);
|
|
log.info("导出路径:" + filePath);
|
|
- FileOutputStream fOut = new FileOutputStream(filePath);
|
|
|
|
- wb.write(fOut);
|
|
|
|
- fOut.close();
|
|
|
|
|
|
+ //FileOutputStream fOut = new FileOutputStream(filePath);
|
|
|
|
+ //wb.write(fOut);
|
|
|
|
+ //fOut.close();
|
|
wb.close();
|
|
wb.close();
|
|
|
|
+ writer.println();
|
|
|
|
+ writer.close();
|
|
return filePath;
|
|
return filePath;
|
|
- }
|
|
|
|
|
|
+}
|
|
|
|
|
|
public String export(MongoCursor<Document> cursor, String fileId, String filename)
|
|
public String export(MongoCursor<Document> cursor, String fileId, String filename)
|
|
throws IOException {
|
|
throws IOException {
|
|
@@ -116,16 +125,16 @@ public class Exporter {
|
|
return objectId.toHexString();
|
|
return objectId.toHexString();
|
|
}
|
|
}
|
|
|
|
|
|
- public String export(MongoCursor<Document> cursor, String fileId, String filename,String tmpdir)
|
|
|
|
|
|
+ public String export(MongoCursor<Document> cursor, String fileId, String filename, String tmpdir)
|
|
throws IOException {
|
|
throws IOException {
|
|
Fields fields =
|
|
Fields fields =
|
|
mongoTemplate.findOne(new Query(Criteria.where("fileId").is(fileId)), Fields.class);
|
|
mongoTemplate.findOne(new Query(Criteria.where("fileId").is(fileId)), Fields.class);
|
|
- String path = tmpdir + File.separator + filename + ".csv";
|
|
|
|
|
|
+ String path = tmpdir + File.separator + filename + ".csv";
|
|
if (fields == null) {
|
|
if (fields == null) {
|
|
return "";
|
|
return "";
|
|
}
|
|
}
|
|
File file1 = new File(path);
|
|
File file1 = new File(path);
|
|
- if(file1.exists()){
|
|
|
|
|
|
+ if (file1.exists()) {
|
|
file1.delete();
|
|
file1.delete();
|
|
}
|
|
}
|
|
String filePath = this.generateFile(fields, filename, tmpdir, cursor);
|
|
String filePath = this.generateFile(fields, filename, tmpdir, cursor);
|