!122 功能迭代-Excel报表导出PDF-增加样式-1(附带背景颜色,字体加粗,字体斜体,字体颜色,字体大小)

Merge pull request !122 from 陈旭光/dev
Foming 1 year ago committed by Gitee
commit 3703ece9ed
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

@ -0,0 +1,20 @@
package com.anjiplus.template.gaea.business.modules.reportexcel.controller.dto;
import lombok.Data;
/**
* @author chenxg
* @date 2023/8/1 10:37
*/
@Data
public class CellStyleDto {
/**
*
*/
private String fa;
/**
*
*/
private String t;
}

@ -0,0 +1,42 @@
package com.anjiplus.template.gaea.business.modules.reportexcel.controller.dto;
import lombok.Data;
/**
* @author chenxg
* @date 2023/8/1 10:37
*/
@Data
public class CommentDto {
/**
*
*/
private int left;
/**
*
*/
private int top;
/**
*
*/
private int width;
/**
*
*/
private int height;
/**
*
*/
private String value;
/**
*
*/
private boolean isshow;
}

@ -5,6 +5,7 @@ import com.anji.plus.gaea.curd.dto.GaeaBaseDTO;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
/** /**
@ -58,4 +59,9 @@ public class ReportExcelDto extends GaeaBaseDTO implements Serializable {
*/ */
private String exportType; private String exportType;
/**
*
*/
private String rowDatas;
} }

@ -0,0 +1,97 @@
package com.anjiplus.template.gaea.business.modules.reportexcel.controller.dto;
import com.anji.plus.gaea.curd.dto.GaeaBaseDTO;
import lombok.Data;
import java.io.Serializable;
/**
* @author chenxg
* @date 2023/8/1 10:37
*/
@Data
public class ReportExcelStyleDto extends GaeaBaseDTO implements Serializable {
/**
*
*/
private CellStyleDto ct;
/**
*
*/
private String v;
/**
*
*/
private String m;
/**
*
*/
private String bg;
/**
*
*/
private String ff;
/**
*
*/
private String fc;
/**
*
*/
private boolean bl;
/**
*
*/
private boolean it;
/**
*
*/
private int fs;
/**
* 线
*/
private boolean cl;
/**
*
*/
private int ht;
/**
*
*/
private int vt;
/**
*
*/
private int tr;
/**
*
*/
private int tb;
/**
*
*/
private CommentDto ps;
/**
*
*/
private String f;
}

@ -19,6 +19,7 @@ import com.anjiplus.template.gaea.business.modules.file.service.GaeaFileService;
import com.anjiplus.template.gaea.business.modules.report.dao.ReportMapper; import com.anjiplus.template.gaea.business.modules.report.dao.ReportMapper;
import com.anjiplus.template.gaea.business.modules.report.dao.entity.Report; import com.anjiplus.template.gaea.business.modules.report.dao.entity.Report;
import com.anjiplus.template.gaea.business.modules.reportexcel.controller.dto.ReportExcelDto; import com.anjiplus.template.gaea.business.modules.reportexcel.controller.dto.ReportExcelDto;
import com.anjiplus.template.gaea.business.modules.reportexcel.controller.dto.ReportExcelStyleDto;
import com.anjiplus.template.gaea.business.modules.reportexcel.dao.ReportExcelMapper; import com.anjiplus.template.gaea.business.modules.reportexcel.dao.ReportExcelMapper;
import com.anjiplus.template.gaea.business.modules.reportexcel.dao.entity.ReportExcel; import com.anjiplus.template.gaea.business.modules.reportexcel.dao.entity.ReportExcel;
import com.anjiplus.template.gaea.business.modules.reportexcel.service.ReportExcelService; import com.anjiplus.template.gaea.business.modules.reportexcel.service.ReportExcelService;
@ -26,17 +27,16 @@ import com.anjiplus.template.gaea.business.modules.reportexcel.util.CellType;
import com.anjiplus.template.gaea.business.modules.reportexcel.util.XlsUtil; import com.anjiplus.template.gaea.business.modules.reportexcel.util.XlsUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itextpdf.text.*; import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper; import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.lowagie.text.DocumentException; import com.lowagie.text.DocumentException;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -52,6 +52,7 @@ import java.nio.file.Paths;
import java.util.*; import java.util.*;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* TODO * TODO
@ -146,17 +147,26 @@ public class ReportExcelServiceImpl implements ReportExcelService {
@Override @Override
public Boolean exportExcel(ReportExcelDto reportExcelDto) { public Boolean exportExcel(ReportExcelDto reportExcelDto) {
String reportCode = reportExcelDto.getReportCode();
String exportType = reportExcelDto.getExportType();
logger.error("导出..."); logger.error("导出...");
exportExcelCore(reportCode,exportType,reportExcelDto); exportExcelCore(reportExcelDto);
return true; return true;
} }
/** /**
* Excel * Excel
*/ */
public void exportExcelCore(String reportCode,String exportType,ReportExcelDto reportExcelDto) public void exportExcelCore(ReportExcelDto reportExcelDto)
{ {
String reportCode = reportExcelDto.getReportCode();
String exportType = reportExcelDto.getExportType();
List<List<ReportExcelStyleDto>> reportExcelStyleList = new ArrayList<>();
JSONObject rowData= JSON.parseObject(reportExcelDto.getRowDatas());
// 将JSONObject对象转换为列表
List<Integer> dataNumList = rowData.keySet().stream().map(Integer::parseInt).sorted().collect(Collectors.toList());
for (Integer i : dataNumList) {
JSONArray jsonArray = rowData.getJSONArray(i.toString());
List<ReportExcelStyleDto> reportExcelStyleDtos = jsonArray.toJavaList(ReportExcelStyleDto.class);
reportExcelStyleList.add(reportExcelStyleDtos);
}
ReportExcelDto report = detailByReportCode(reportCode); ReportExcelDto report = detailByReportCode(reportCode);
reportExcelDto.setJsonStr(report.getJsonStr()); reportExcelDto.setJsonStr(report.getJsonStr());
String jsonStr = analysisReportData(reportExcelDto); String jsonStr = analysisReportData(reportExcelDto);
@ -182,7 +192,7 @@ public class ReportExcelServiceImpl implements ReportExcelService {
// 将Excel文件转换为PDF // 将Excel文件转换为PDF
String pdfFileName = fileName + ".pdf"; String pdfFileName = fileName + ".pdf";
String pdfFilePath = dir.getAbsolutePath() + File.separator + pdfFileName; String pdfFilePath = dir.getAbsolutePath() + File.separator + pdfFileName;
pdfFile = convertExcelToPdf(filePath, pdfFilePath); pdfFile = convertExcelToPdf(filePath, pdfFilePath,reportExcelStyleList);
gaeaFileService.upload(pdfFile); gaeaFileService.upload(pdfFile);
} }
@ -203,7 +213,7 @@ public class ReportExcelServiceImpl implements ReportExcelService {
} }
} }
// 将Excel文件转换为PDF // 将Excel文件转换为PDF
public File convertExcelToPdf(String excelFilePath, String pdfFilePath) { public File convertExcelToPdf(String excelFilePath, String pdfFilePath, List<List<ReportExcelStyleDto>> reportExcelStyleList) {
try { try {
// 读取Excel文件 // 读取Excel文件
Workbook workbook = new XSSFWorkbook(excelFilePath); Workbook workbook = new XSSFWorkbook(excelFilePath);
@ -226,8 +236,15 @@ public class ReportExcelServiceImpl implements ReportExcelService {
Row headerRow = sheet.getRow(0); Row headerRow = sheet.getRow(0);
for (int i = 0; i < headerRow.getLastCellNum(); i++) { for (int i = 0; i < headerRow.getLastCellNum(); i++) {
Cell headerCell = headerRow.getCell(i); Cell headerCell = headerRow.getCell(i);
// 获取单元格样式
PdfPCell tableCell = new PdfPCell(); PdfPCell tableCell = new PdfPCell();
tableCell.setPhrase(new Phrase(getStringValue(headerCell), font)); tableCell.setPhrase(new Phrase(getStringValue(headerCell), font));
ReportExcelStyleDto reportExcelStyleDto = reportExcelStyleList.get(0).get(i);
if(!Objects.isNull(reportExcelStyleDto))
{
processCellStyle(reportExcelStyleDto,tableCell);
}
table.addCell(tableCell); table.addCell(tableCell);
} }
@ -245,6 +262,11 @@ public class ReportExcelServiceImpl implements ReportExcelService {
} }
PdfPCell tableCell = new PdfPCell(); PdfPCell tableCell = new PdfPCell();
tableCell.setPhrase(new Phrase(getStringValue(cell), font)); tableCell.setPhrase(new Phrase(getStringValue(cell), font));
ReportExcelStyleDto reportExcelStyleDto = reportExcelStyleList.get(i).get(j);
if(!Objects.isNull(reportExcelStyleDto))
{
processCellStyle(reportExcelStyleDto,tableCell);
}
table.addCell(tableCell); table.addCell(tableCell);
} }
} }
@ -267,6 +289,65 @@ public class ReportExcelServiceImpl implements ReportExcelService {
return null; return null;
} }
/**
* pdf
* @param tableCell
*/
public void processCellStyle(ReportExcelStyleDto reportExcelStyleDto,PdfPCell tableCell)
{
//处理单元格背景颜色
String bg = reportExcelStyleDto.getBg();
java.awt.Color color = null;
if(!Objects.isNull(bg))
{
color = java.awt.Color.decode(bg);
tableCell.setBackgroundColor(new BaseColor(color.getRed(), color.getGreen(), color.getBlue()));
}
//处理字体
String fc = reportExcelStyleDto.getFc();
Integer fs = reportExcelStyleDto.getFs();
String ff = reportExcelStyleDto.getFf();
Boolean bl = reportExcelStyleDto.isBl();
Boolean it = reportExcelStyleDto.isIt();
Font font = new Font();
// 设置字体颜色
if(!Objects.isNull(fc))
{
color = java.awt.Color.decode(fc);
font.setColor(new BaseColor(color.getRed(), color.getGreen(), color.getBlue()));
}
// 设置字体
if(!Objects.isNull(ff) && !Objects.equals("0",ff))
{
font.setFamily(ff.toString());
}
// 设置字体大小
if(!Objects.isNull(fs) && !Objects.equals(0,fs))
{
font.setSize(fs);
}
// 设置字体加粗
if(Objects.equals(Boolean.TRUE,bl))
{
font.setStyle(Font.BOLD);
}
// 设置字体斜体
if(Objects.equals(Boolean.TRUE,it))
{
font.setStyle(Font.ITALIC);
}
// 设置字体加粗且斜体
if(Objects.equals(Boolean.TRUE,bl) && Objects.equals(Boolean.TRUE,it))
{
font.setStyle(Font.BOLDITALIC);
}
Phrase phrase = tableCell.getPhrase();
tableCell.setPhrase(new Paragraph(phrase.getContent(), font));
//处理字体
tableCell.setBorderColor(BaseColor.BLACK);
}
private String getStringValue(Cell cell) { private String getStringValue(Cell cell) {
if (cell == null) if (cell == null)
return ""; return "";

@ -118,6 +118,7 @@ export default {
if (val != "") { if (val != "") {
result["exportType"] = val; result["exportType"] = val;
} }
this.getCellStyleData(result);
const { code, message } = await exportExcel(result); const { code, message } = await exportExcel(result);
if (code != 200) return; if (code != 200) return;
this.$message.success(message); this.$message.success(message);
@ -192,7 +193,22 @@ export default {
$(function() { $(function() {
luckysheet.create(options); luckysheet.create(options);
}); });
} },
getCellStyleData(result) {
const sheetData = luckysheet.getluckysheetfile(); //
const rowDatas = {};
for (let sheetIndex in sheetData) {
const sheet = sheetData[sheetIndex];
if (sheet && sheet.data) {
for (let rowIndex in sheet.data) {
const row = sheet.data[rowIndex];
rowDatas[rowIndex]=row;
}
}
}
result["rowDatas"] = JSON.stringify(rowDatas);;
},
} }
}; };
</script> </script>

Loading…
Cancel
Save