|
@@ -0,0 +1,97 @@
|
|
|
|
|
+package com.aoyang.tms.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import com.aoyang.tms.entity.TmsGasFee;
|
|
|
|
|
+import com.aoyang.tms.service.CommonService;
|
|
|
|
|
+import com.aoyang.tms.service.JxlsExportTestService;
|
|
|
|
|
+import com.aoyang.tms.service.TmsGasFeeService;
|
|
|
|
|
+import com.aoyang.tms.util.FileUtil;
|
|
|
|
|
+import com.aoyang.tms.util.JxlsUtils;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
+import java.io.*;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class JxlsExportTestServiceImpl implements JxlsExportTestService {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private TmsGasFeeService tmsGasFeeService;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public StreamingResponseBody download(TmsGasFee param, HttpServletResponse response) throws IOException {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ TmsGasFee tmsGasFee = tmsGasFeeService.getById(param);
|
|
|
|
|
+ byte imageBytes[] = getImageBytes("/cn/chendd/examples/images/splash.jpg");
|
|
|
|
|
+
|
|
|
|
|
+ // 获取模板文件
|
|
|
|
|
+ String tplfileName = "circle_sign_tpl.xlsx";
|
|
|
|
|
+ String tmpFileName = FileUtil.getTmpFileName(FileUtil.getFilePrefix(tplfileName));
|
|
|
|
|
+ String tmpFilePath = FileUtils.getTempDirectoryPath() + File.separator + tmpFileName;
|
|
|
|
|
+ // 写文件
|
|
|
|
|
+ OutputStream os = new FileOutputStream(tmpFilePath);
|
|
|
|
|
+ InputStream inputStream = FileUtil.getFileTplInputStream(tplfileName);
|
|
|
|
|
+ log.info("tmpFilePath :{}", tmpFilePath);
|
|
|
|
|
+ log.info("tplfileName :{}", tplfileName);
|
|
|
|
|
+ if (os != null && inputStream != null) {
|
|
|
|
|
+ Map<String, Object> model = new HashMap<String, Object>();
|
|
|
|
|
+ model.put("tmsGasFee", tmsGasFee);
|
|
|
|
|
+ JxlsUtils.exportExcel(inputStream, os, model);
|
|
|
|
|
+ os.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ // 导出
|
|
|
|
|
+ String newFileName = "circleSign.xlsx";
|
|
|
|
|
+ // 输出文件
|
|
|
|
|
+ response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", newFileName));
|
|
|
|
|
+
|
|
|
|
|
+ response.setHeader("content-type", "application/octet-stream");
|
|
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
|
|
+ BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(tmpFilePath)));
|
|
|
|
|
+ return new StreamingResponseBody() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void writeTo(OutputStream outputStream) throws IOException {
|
|
|
|
|
+ int nRead;
|
|
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
|
|
+
|
|
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
|
|
+ while ((nRead = bufferedInputStream.read(bytes, 0, bytes.length)) != -1) {
|
|
|
|
|
+ outputStream.write(bytes, 0, nRead);
|
|
|
|
|
+ }
|
|
|
|
|
+ long userTime = System.currentTimeMillis() - startTime;
|
|
|
|
|
+ log.info("orgAccountLogList使用时间" + userTime);
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //获取图片的字节码,临时使用
|
|
|
|
|
+ private static byte[] getImageBytes(String imagePath) throws IOException {
|
|
|
|
|
+ byte imageBytes[] = null;
|
|
|
|
|
+ InputStream is = JxlsExportTestServiceImpl.class.getClass().getResourceAsStream(imagePath);
|
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
+ byte bytes[] = new byte[1024];
|
|
|
|
|
+ int lens = -1;
|
|
|
|
|
+ try {
|
|
|
|
|
+ while ((lens = is.read(bytes)) != -1) {
|
|
|
|
|
+ baos.write(bytes, 0, lens);
|
|
|
|
|
+ }
|
|
|
|
|
+ imageBytes = baos.toByteArray();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ throw new IOException(e);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ baos.flush();
|
|
|
|
|
+ baos.close();
|
|
|
|
|
+ is.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ return imageBytes;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|