Răsfoiți Sursa

已知问题修改

y 2 ani în urmă
părinte
comite
e750a5bce1

+ 30 - 1
src/main/java/com/ydtech/modules/inv/utils/EasyExcelUtils.java

@@ -14,7 +14,9 @@ import lombok.SneakyThrows;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
@@ -230,12 +232,22 @@ public class EasyExcelUtils {
      */
     @SneakyThrows
     public String downAccountExcel(String uploadFilePath, List<InvAccountExcel> invAccountExcel, List<InvAccountProfit> invAccountProfit) {
+
         // 加载模板
         InputStream inputStream = getClass().getResourceAsStream("/template/资金申请单模板.xlsx");
 
+        byte[] bytes = saveaIns(inputStream);
+
+        if (inputStream != null) {
+            inputStream.close();
+        }
+
+
+
         List<InvAccountExcel> list = new ArrayList<>();
         // 获取工作表并填充
         for (InvAccountExcel excel : invAccountExcel) {
+            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
             // 写入文件
             Date date = new Date();
             String file = date.getTime() + ".xlsx";
@@ -275,13 +287,16 @@ public class EasyExcelUtils {
             excel.setPayMethod(a.toString());
             // 生成工作簿对象
             ExcelWriterBuilder workBookWriter = EasyExcel.write(fileName, InvAccountExcel.class)
-                    .withTemplate(inputStream);
+                    .withTemplate(byteArrayInputStream);
 
             workBookWriter.sheet().doFill(excel);
             excel.setUplodePath(file);
             excel.setPayMethod(payMethod);
             list.add(excel);
+            byteArrayInputStream.close();
+
         }
+
         insAccountExcelService.updateBatchById(list);
 
         File[] srcFiles = new File[list.size()];
@@ -296,4 +311,18 @@ public class EasyExcelUtils {
         return a;
     }
 
+
+    //保存流对象(输入流在第二次使用的时候会失效)
+    public byte[] saveaIns(InputStream ins){
+        byte[] buf = null;
+        try {
+            if(ins!=null){
+                buf = org.apache.commons.io.IOUtils.toByteArray(ins);//ins为InputStream流
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return buf;
+    }
+
 }