Răsfoiți Sursa

导出excel

hankunkun 11 luni în urmă
părinte
comite
305f416eaf
2 a modificat fișierele cu 45 adăugiri și 15 ștergeri
  1. 22 1
      pom.xml
  2. 23 14
      src/main/java/ieven/server/webapp/domain/exporter/Exporter.java

+ 22 - 1
pom.xml

@@ -49,10 +49,31 @@
             <artifactId>springfox-boot-starter</artifactId>
             <version>3.0.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>ooxml-schemas</artifactId>
+            <version>1.1</version
+            <type>pom</type>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi</artifactId>
+            <version>5.3.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>ooxml-schemas</artifactId>
+            <version>1.1</version>
+        </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml</artifactId>
-            <version>4.1.2</version>
+            <version>5.3.0</version>
+        </dependency>
+        <dependency>
+            <groupId>dom4j</groupId>
+            <artifactId>dom4j</artifactId>
+            <version>1.6.1</version>
         </dependency>
         <!-- <dependency>
              <groupId>org.apache.poi</groupId>

+ 23 - 14
src/main/java/ieven/server/webapp/domain/exporter/Exporter.java

@@ -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.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.Map;
 
@@ -47,6 +44,8 @@ public class Exporter {
         if (sheet == null) {
             sheet = wb.createSheet("sheet" + currentSheetNum);
         }
+        filePath = filePath + File.separator + filename + ".csv";
+        PrintWriter writer = new PrintWriter(new FileWriter(filePath));
         // 创建表头
         cRow = sheet.createRow(0);
         int hcIndex = 0;
@@ -54,6 +53,12 @@ public class Exporter {
             String key = entry.getKey();
             Cell cell = cRow.createCell(hcIndex);
             cell.setCellValue(key);
+            if (cell != null) {
+                writer.print(cell.toString());
+                if (cellnum < row.getLastCellNum() - 1) {
+                    writer.print(",");
+                }
+            }
             hcIndex++;
         }
         currentRowRead++;
@@ -68,15 +73,18 @@ public class Exporter {
                 Cell cell = cRow.createCell(cInsert);
                 Object value = doc.get(alias);
                 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("");
-                    }else {
+                    } else {
                         cell.setCellValue((String) value);
                     }
                 } else if (value instanceof Number) {
                     cell.setCellType(CellType.NUMERIC);
                     cell.setCellValue(Double.parseDouble(String.valueOf(value)));
                 }
+                if (cell != null) {
+                    writer.print(cell.toString());
+                }
                 cInsert++;
             }
             currentRowRead++;
@@ -87,14 +95,15 @@ public class Exporter {
             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();
+        //FileOutputStream fOut = new FileOutputStream(filePath);
+        //wb.write(fOut);
+        //fOut.close();
         wb.close();
+        writer.println();
+        writer.close();
         return filePath;
-    }
+}
 
     public String export(MongoCursor<Document> cursor, String fileId, String filename)
             throws IOException {
@@ -116,16 +125,16 @@ public class Exporter {
         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 {
         Fields fields =
                 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) {
             return "";
         }
         File file1 = new File(path);
-        if(file1.exists()){
+        if (file1.exists()) {
             file1.delete();
         }
         String filePath = this.generateFile(fields, filename, tmpdir, cursor);