add exportUtil 04/138204/1 14.0.0
authorzhen1234 <liz779@chinatelecom.cn>
Thu, 13 Jun 2024 04:06:44 +0000 (12:06 +0800)
committerzhen1234 <liz779@chinatelecom.cn>
Thu, 13 Jun 2024 04:07:11 +0000 (12:07 +0800)
Issue-ID: USECASEUI-837
Signed-off-by: zhen1234 <liz779@chinatelecom.cn>
Change-Id: I3547c3884b3e19fdfe4d94da63a02c5069363a06

server/src/main/java/org/onap/usecaseui/server/util/ExportUtil.java [new file with mode: 0644]

diff --git a/server/src/main/java/org/onap/usecaseui/server/util/ExportUtil.java b/server/src/main/java/org/onap/usecaseui/server/util/ExportUtil.java
new file mode 100644 (file)
index 0000000..236bf33
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2017 CMCC, Inc. and others. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.usecaseui.server.util;
+
+import jakarta.servlet.ServletOutputStream;
+import jakarta.servlet.http.HttpServletResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.VerticalAlignment;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
+
+import java.io.IOException;
+import java.util.List;
+
+
+@SuppressWarnings("unused")
+@Slf4j
+public class ExportUtil {
+
+    public static void exportExcel(HttpServletResponse response, String fileName, List<?> dataList) {
+        SXSSFWorkbook book = new SXSSFWorkbook();
+        Sheet sheet = book.createSheet();
+        CellStyle rowStyle = book.createCellStyle();
+        rowStyle.setAlignment(HorizontalAlignment.CENTER);
+        rowStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+        sheet.setDefaultColumnWidth(15);
+        for (int i = 0; i < dataList.size(); i++) {
+            Row row = sheet.createRow(i);
+            Cell cell = row.createCell(0);
+            cell.setCellStyle(rowStyle);
+            cell.setCellType(CellType.STRING);
+            cell.setCellValue((String) dataList.get(i));
+        }
+        try {
+            write(response, book, fileName);
+        } catch (IOException e) {
+            log.error("An exception occurred with exportExcel, message: "+e.getMessage());
+        }
+    }
+    private static void write(HttpServletResponse response, SXSSFWorkbook book, String fileName) throws IOException {
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+        response.setCharacterEncoding("utf-8");
+        String name = new String(fileName.getBytes("GBK"), "ISO8859_1") + ".xlsx";
+        response.addHeader("Content-Disposition", "attachment;filename=" + name);
+        ServletOutputStream out = response.getOutputStream();
+        book.write(out);
+        out.flush();
+        out.close();
+    }
+}