|
@@ -1,32 +1,23 @@
|
|
|
package org.jeecg.ctop.finance.settlement.controller;
|
|
|
|
|
|
-import cn.afterturn.easypoi.cache.manager.POICacheManager;
|
|
|
-import cn.afterturn.easypoi.entity.ImageEntity;
|
|
|
-import cn.afterturn.easypoi.excel.ExcelExportUtil;
|
|
|
-import cn.afterturn.easypoi.excel.entity.ExportParams;
|
|
|
-import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.poi.ss.usermodel.Workbook;
|
|
|
-import org.jeecg.common.api.vo.Result;
|
|
|
+import org.apache.poi.hssf.usermodel.*;
|
|
|
+import org.apache.poi.ss.usermodel.ClientAnchor;
|
|
|
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
import org.jeecg.ctop.finance.settlement.entity.vo.SettlementReportBytedanceVo;
|
|
|
import org.jeecg.ctop.finance.settlement.service.IReportSettlementService;
|
|
|
-import org.jeecg.ctop.finance.settlement.service.serviceImpl.FileLoaderImpl;
|
|
|
-import org.jeecg.ctop.finance.utils.SettlementUtil;
|
|
|
+import org.jeecg.ctop.finance.utils.createExcelByTemplate;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.awt.*;
|
|
|
-import java.io.BufferedOutputStream;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.OutputStream;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
import java.net.URLEncoder;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -43,91 +34,165 @@ public class ExportReportController {
|
|
|
@Autowired
|
|
|
private IReportSettlementService reportSettlementService;
|
|
|
|
|
|
+
|
|
|
+
|
|
|
@ApiOperation(value="财务结算-结算单导出", notes="财务结算-结算单导出")
|
|
|
- @GetMapping(value = "/exportSettlementReport")
|
|
|
- public Result exportSettlementReport(HttpServletResponse response) {
|
|
|
+ @GetMapping(value = "/settlementReportBytedance")
|
|
|
+ public String settlementReportBytedance(HttpServletResponse response) throws Exception {
|
|
|
+ //第一步创建workbook
|
|
|
+ HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
|
|
- // byte[] img = SettlementUtil.getByteImgByUrl("https://media-1301855440.cos.ap-chongqing.myqcloud.com/image/2021-08-05/2-1628129040416.jpg");
|
|
|
+ //设置样式
|
|
|
+ HSSFCellStyle style = wb.createCellStyle();
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);//水平居中
|
|
|
|
|
|
- List<SettlementReportBytedanceVo> list = reportSettlementService.getAccountTransactionDayBytedance();
|
|
|
+ //设置字体
|
|
|
+ HSSFFont font = wb.createFont();
|
|
|
+ font.setFontName("黑体");
|
|
|
+ font.setFontHeightInPoints((short) 12);//设置字体大小
|
|
|
+ style.setFont(font);
|
|
|
|
|
|
|
|
|
|
|
|
- Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("这是表头", "这是sheet名称"), SettlementReportBytedanceVo.class, list);
|
|
|
+ for (int j = 0; j< 2;j++){
|
|
|
|
|
|
|
|
|
- try {
|
|
|
- response.setCharacterEncoding("UTF-8");
|
|
|
- response.setHeader("content-Type", "application/vnd.ms-excel");
|
|
|
- response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("文件名称.xlsx", "UTF-8"));
|
|
|
- workbook.write(response.getOutputStream());
|
|
|
- } catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
|
|
|
|
|
|
- return null;
|
|
|
- }
|
|
|
+ //2:创建sheet
|
|
|
+ HSSFSheet sheet = wb.createSheet("账户id"+j);
|
|
|
|
|
|
|
|
|
+ //3:添加 第一行单元格
|
|
|
+ HSSFRow row0 = sheet.createRow(0);
|
|
|
+ HSSFCell cell0 = row0.createCell(0);
|
|
|
+ //设置 第一行 表头
|
|
|
+ cell0.setCellValue("头条结算单");
|
|
|
+ CellRangeAddress region = new CellRangeAddress(0, 0, 0, 10);
|
|
|
+ sheet.addMergedRegion(region);//合并行
|
|
|
+ cell0.setCellStyle(style);
|
|
|
|
|
|
- @ApiOperation(value="财务结算-结算单导出", notes="财务结算-结算单导出")
|
|
|
- @GetMapping(value = "/exportImg")
|
|
|
- public String exportImg(HttpServletResponse response) throws Exception {
|
|
|
|
|
|
- // byte[] img = SettlementUtil.getByteImgByUrl("https://media-1301855440.cos.ap-chongqing.myqcloud.com/image/2021-08-05/2-1628129040416.jpg");
|
|
|
|
|
|
- TemplateExportParams params = new TemplateExportParams("C://Users//Administrator//Desktop//bytedance.xlsx",true);
|
|
|
|
|
|
- // 重要代码
|
|
|
- //POICacheManager.setFileLoader(new FileLoaderImpl());
|
|
|
- POICacheManager.setFileLoder(new FileLoaderImpl());
|
|
|
+ //4:添加表头2行
|
|
|
+ HSSFRow row = sheet.createRow(1);
|
|
|
+
|
|
|
+ //5:创建第2行的单元格
|
|
|
+ HSSFCell cell = row.createCell(0); //第一个单元格
|
|
|
+ cell.setCellValue(" 广告主ID "); //设定值
|
|
|
+ cell.setCellStyle(style); //内容居中
|
|
|
+
|
|
|
+
|
|
|
+ cell = row.createCell(1);
|
|
|
+ cell.setCellValue(" 日期 ");
|
|
|
+ cell.setCellStyle(style);
|
|
|
+
|
|
|
+ cell = row.createCell(2);
|
|
|
+ cell.setCellValue("日终结余(单位元)");
|
|
|
+ cell.setCellStyle(style);
|
|
|
+
|
|
|
+ cell = row.createCell(3);
|
|
|
+ cell.setCellValue("现金支出(单位元)");
|
|
|
+ cell.setCellStyle(style);
|
|
|
+
|
|
|
+ cell = row.createCell(4);
|
|
|
+ cell.setCellValue("总支出(单位元)");
|
|
|
+ cell.setCellStyle(style);
|
|
|
+
|
|
|
+ cell = row.createCell(5);
|
|
|
+ cell.setCellValue("冻结(单位元)");
|
|
|
+ cell.setCellStyle(style);
|
|
|
+
|
|
|
+ cell = row.createCell(6);
|
|
|
+ cell.setCellValue("总存入(单位元)");
|
|
|
+ cell.setCellStyle(style);
|
|
|
|
|
|
- Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ cell = row.createCell(7);
|
|
|
+ cell.setCellValue("赠款支出(单位元)");
|
|
|
+ cell.setCellStyle(style);
|
|
|
+
|
|
|
+ cell = row.createCell(8);
|
|
|
+ cell.setCellValue("共享钱包支出(单位元)");
|
|
|
+ cell.setCellStyle(style);
|
|
|
+
|
|
|
+ cell = row.createCell(9);
|
|
|
+ cell.setCellValue("总转入(单位元)");
|
|
|
+ cell.setCellStyle(style);
|
|
|
+
|
|
|
+ cell = row.createCell(10);
|
|
|
+ cell.setCellValue("总转出(单位元)");
|
|
|
+ cell.setCellStyle(style);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //添加数据
|
|
|
List<SettlementReportBytedanceVo> list = reportSettlementService.getAccountTransactionDayBytedance();
|
|
|
- List<Map<String, Object>> pictureList = new ArrayList<Map<String, Object>>() ;
|
|
|
-
|
|
|
- Map<String,Object> picture1 = new HashMap<String, Object>();
|
|
|
- Map<String,Object> picture2 = new HashMap<String, Object>();
|
|
|
- byte[] datas = SettlementUtil.getByteImgByUrl("https://media-1301855440.cos.ap-chongqing.myqcloud.com/image/2021-08-05/2-1628129040416.jpg");
|
|
|
- ImageEntity img = new ImageEntity();
|
|
|
- img.setUrl("https://media-1301855440.cos.ap-chongqing.myqcloud.com/image/2021-08-05/2-1628129040416.jpg");
|
|
|
- //img.setUrl("C:\\Users\\Administrator\\Desktop\\汇创\\20210507161223.jpg");
|
|
|
- img.setHeight(1000);
|
|
|
- img.setWidth(500);
|
|
|
- picture1.put("imag", img);
|
|
|
- picture2.put("imag", img);
|
|
|
- pictureList.add(picture1);
|
|
|
- pictureList.add(picture2);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- Map<String, Object> resultMap = new HashMap<>();
|
|
|
- resultMap.put("l",list);
|
|
|
- resultMap.put("p",pictureList);
|
|
|
-
|
|
|
-
|
|
|
- try {
|
|
|
- Workbook workbook = ExcelExportUtil.exportExcel(params, resultMap);
|
|
|
-
|
|
|
- response.setCharacterEncoding("UTF-8");
|
|
|
- response.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
|
|
- response.setHeader("Pragma", "no-cache");
|
|
|
- response.setHeader("Cache-Control", "no-cache");
|
|
|
- response.setDateHeader("Expires", 0);
|
|
|
- response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("文件名称.xlsx", "UTF-8"));
|
|
|
- OutputStream output = response.getOutputStream();
|
|
|
- BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
|
|
|
- workbook.write(bufferedOutPut);
|
|
|
- bufferedOutPut.flush();
|
|
|
- bufferedOutPut.close();
|
|
|
- output.close();
|
|
|
- } catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ SettlementReportBytedanceVo vo = list.get(i);
|
|
|
+ //创建行 (表头占0-1行 所以 从 2开始)
|
|
|
+ row = sheet.createRow(i+2);
|
|
|
+ sheet.autoSizeColumn(0,true);
|
|
|
+ sheet.autoSizeColumn(i+1,true);
|
|
|
+
|
|
|
+ //创建单元格并且添加数据
|
|
|
+ row.createCell(0).setCellValue(vo.getAdvertiserId());
|
|
|
+ row.createCell(1).setCellValue(vo.getDate());
|
|
|
+ row.createCell(2).setCellValue(vo.getBalance());
|
|
|
+ row.createCell(3).setCellValue(vo.getCashCost());
|
|
|
+ row.createCell(4).setCellValue(vo.getCost());
|
|
|
+ row.createCell(5).setCellValue(vo.getFrozen());
|
|
|
+ row.createCell(6).setCellValue(vo.getIncome());
|
|
|
+ row.createCell(7).setCellValue(vo.getRewardCost());
|
|
|
+ row.createCell(8).setCellValue(vo.getSharedWalletCost());
|
|
|
+ row.createCell(9).setCellValue(vo.getTransferIn());
|
|
|
+ row.createCell(10).setCellValue(vo.getTransferOut());
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
- return "123";
|
|
|
+
|
|
|
+ //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
|
|
|
+ ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
|
|
|
+ //bufferImg = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\汇创\\20210507161223.jpg"));
|
|
|
+ //bufferImg = ImageIO.read(new File(URLDecoder.decode("https://media-1301855440.cos.ap-chongqing.myqcloud.com/image/2021-08-05/2-1628129040416.jpg","utf-8")));
|
|
|
+ String imagePath = "https://media-1301855440.cos.ap-chongqing.myqcloud.com/image/2021-08-05/2-1628129040416.jpg";
|
|
|
+ //InputStream is = new FileInputStream(imagePath);
|
|
|
+ //通过图片网络地址 获取输入流
|
|
|
+ InputStream is = createExcelByTemplate.getInputStreamByUrl(imagePath);
|
|
|
+ BufferedImage bufferImg = ImageIO.read(is);
|
|
|
+
|
|
|
+ ImageIO.write(bufferImg, imagePath.substring(imagePath.lastIndexOf(".")+1), byteArrayOut);
|
|
|
+
|
|
|
+ //画图的顶级管理器,一个sheet只能获取一个
|
|
|
+ HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
|
|
|
+ //anchor主要用于设置图片的属性
|
|
|
+ //HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8);
|
|
|
+ HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 5, list.size()+6, (short) 10, 40);
|
|
|
+ HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 255, 255,(short) 5, list.size()+40, (short) 10, 40*2);
|
|
|
+ anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
|
|
|
+ anchor2.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
|
|
|
+ //插入图片
|
|
|
+ patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
|
|
|
+ patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
|
|
+ response.setHeader("Pragma", "no-cache");
|
|
|
+ response.setHeader("Cache-Control", "no-cache");
|
|
|
+ response.setDateHeader("Expires", 0);
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("文件名称.xlsx", "UTF-8"));
|
|
|
+ OutputStream output = response.getOutputStream();
|
|
|
+ BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
|
|
|
+ wb.write(bufferedOutPut);
|
|
|
+ bufferedOutPut.flush();
|
|
|
+ bufferedOutPut.close();
|
|
|
+ output.close();
|
|
|
+
|
|
|
+ return "success";
|
|
|
}
|
|
|
|
|
|
|