|
|
@@ -4,10 +4,7 @@ import java.io.ByteArrayInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
-import java.net.URLEncoder;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
import org.apache.poi.hssf.util.HSSFColor;
|
|
|
import org.apache.poi.ss.usermodel.BorderStyle;
|
|
|
@@ -16,17 +13,11 @@ import org.apache.poi.ss.usermodel.FillPatternType;
|
|
|
import org.apache.poi.ss.usermodel.Font;
|
|
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
|
-import org.apache.poi.util.IOUtils;
|
|
|
import org.apache.poi.xssf.streaming.SXSSFCell;
|
|
|
import org.apache.poi.xssf.streaming.SXSSFRow;
|
|
|
import org.apache.poi.xssf.streaming.SXSSFSheet;
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
-import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
-
|
|
|
import com.seecoder.BlueWhale.vo.OrderVO;
|
|
|
|
|
|
@Component
|
|
|
@@ -80,34 +71,10 @@ public class ExcelUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public ResponseEntity<byte[]> buildResponseEntity(InputStream is, String name) throws Exception {
|
|
|
-
|
|
|
- HttpHeaders header = new HttpHeaders();
|
|
|
- String fileSuffix = name.substring(name.lastIndexOf('.') + 1);
|
|
|
- fileSuffix = fileSuffix.toLowerCase();
|
|
|
-
|
|
|
- Map<String, String> arguments = new HashMap<String, String>();
|
|
|
- arguments.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
- arguments.put("xls", "application/vnd.ms-excel");
|
|
|
-
|
|
|
- String contentType = arguments.get(fileSuffix);
|
|
|
- header.add("Content-Type", (StringUtils.hasText(contentType) ? contentType : "application/x-download"));
|
|
|
- if (is != null && is.available() != 0) {
|
|
|
- header.add("Content-Length", String.valueOf(is.available()));
|
|
|
- header.add("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + URLEncoder.encode(name, "UTF-8"));
|
|
|
- byte[] bs = IOUtils.toByteArray(is);
|
|
|
- return new ResponseEntity<>(bs, header, HttpStatus.OK);
|
|
|
- } else {
|
|
|
- String string = "数据为空";
|
|
|
- header.add("Content-Length", "0");
|
|
|
- return new ResponseEntity<>(string.getBytes(), header, HttpStatus.OK);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public ResponseEntity<byte[]> exportExcel(List<OrderVO> orders) {
|
|
|
+ public InputStream exportExcel(List<OrderVO> orders) {
|
|
|
|
|
|
ByteArrayOutputStream output = null;
|
|
|
- InputStream inputStream1 = null;
|
|
|
+ InputStream inputStream = null;
|
|
|
SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
|
|
|
SXSSFSheet sheet = wb.createSheet();
|
|
|
// 设置报表头样式
|
|
|
@@ -139,7 +106,7 @@ public class ExcelUtil {
|
|
|
//将wb中的数据以字节形式存到inputStream1中
|
|
|
output = new ByteArrayOutputStream();
|
|
|
wb.write(output);
|
|
|
- inputStream1 = new ByteArrayInputStream(output.toByteArray());
|
|
|
+ inputStream = new ByteArrayInputStream(output.toByteArray());
|
|
|
output.flush();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -147,21 +114,15 @@ public class ExcelUtil {
|
|
|
try {
|
|
|
if (output != null) {
|
|
|
output.close();
|
|
|
- if (inputStream1 != null) {
|
|
|
- inputStream1.close();
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
}
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- try {
|
|
|
- return buildResponseEntity(inputStream1, "订单列表.xlsx");
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return null;
|
|
|
+ return inputStream;
|
|
|
|
|
|
}
|
|
|
}
|