diff options
author | zhen1234 <liz779@chinatelecom.cn> | 2024-06-13 12:06:44 +0800 |
---|---|---|
committer | zhen1234 <liz779@chinatelecom.cn> | 2024-06-13 12:07:11 +0800 |
commit | eafd4e22f8b6d0a4c5d852c6033a37a0afd4c5cd (patch) | |
tree | a6927971e1d925046124334b3451848dd3ff93b9 | |
parent | dc0f7622f74db625169a168696f0b2615cf49c8b (diff) |
add exportUtil14.0.0
Issue-ID: USECASEUI-837
Signed-off-by: zhen1234 <liz779@chinatelecom.cn>
Change-Id: I3547c3884b3e19fdfe4d94da63a02c5069363a06
-rw-r--r-- | server/src/main/java/org/onap/usecaseui/server/util/ExportUtil.java | 68 |
1 files changed, 68 insertions, 0 deletions
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 index 00000000..236bf33c --- /dev/null +++ b/server/src/main/java/org/onap/usecaseui/server/util/ExportUtil.java @@ -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(); + } +} |