!120 Excel报表导出PDF

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

@ -166,6 +166,18 @@
<version>3.0.9</version> <version>3.0.9</version>
</dependency> </dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>core-renderer</artifactId>
<version>R8</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.13</version>
</dependency>
</dependencies> </dependencies>
<developers> <developers>

@ -25,21 +25,31 @@ import com.anjiplus.template.gaea.business.modules.reportexcel.service.ReportExc
import com.anjiplus.template.gaea.business.modules.reportexcel.util.CellType; 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.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.lowagie.text.DocumentException;
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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -139,13 +149,21 @@ public class ReportExcelServiceImpl implements ReportExcelService {
String reportCode = reportExcelDto.getReportCode(); String reportCode = reportExcelDto.getReportCode();
String exportType = reportExcelDto.getExportType(); String exportType = reportExcelDto.getExportType();
logger.error("导出..."); logger.error("导出...");
if (exportType.equals(ExportTypeEnum.GAEA_TEMPLATE_EXCEL.getCodeValue())) { exportExcelCore(reportCode,exportType,reportExcelDto);
return true;
}
/**
* Excel
*/
public void exportExcelCore(String reportCode,String exportType,ReportExcelDto reportExcelDto)
{
ReportExcelDto report = detailByReportCode(reportCode); ReportExcelDto report = detailByReportCode(reportCode);
reportExcelDto.setJsonStr(report.getJsonStr()); reportExcelDto.setJsonStr(report.getJsonStr());
String jsonStr = analysisReportData(reportExcelDto); String jsonStr = analysisReportData(reportExcelDto);
List<JSONObject> lists=(List<JSONObject> ) JSON.parse(jsonStr); List<JSONObject> lists=(List<JSONObject> ) JSON.parse(jsonStr);
OutputStream out = null; OutputStream out = null;
File file = null; File file = null;
File pdfFile = null;
try { try {
String fileName = report.getReportCode(); String fileName = report.getReportCode();
File dir = new File(dictPath + ZIP_PATH); File dir = new File(dictPath + ZIP_PATH);
@ -156,7 +174,17 @@ public class ReportExcelServiceImpl implements ReportExcelService {
file = new File(filePath); file = new File(filePath);
out = Files.newOutputStream(Paths.get(filePath)); out = Files.newOutputStream(Paths.get(filePath));
XlsUtil.exportXlsFile(out, true, lists); XlsUtil.exportXlsFile(out, true, lists);
if (exportType.equals(ExportTypeEnum.GAEA_TEMPLATE_EXCEL.getCodeValue())) {
gaeaFileService.upload(file); gaeaFileService.upload(file);
}
else if(exportType.equals(ExportTypeEnum.GAEA_TEMPLATE_PDF.getCodeValue()))
{
// 将Excel文件转换为PDF
String pdfFileName = fileName + ".pdf";
String pdfFilePath = dir.getAbsolutePath() + File.separator + pdfFileName;
pdfFile = convertExcelToPdf(filePath, pdfFilePath);
gaeaFileService.upload(pdfFile);
}
} catch (IOException e) { } catch (IOException e) {
logger.error("导出失败", e); logger.error("导出失败", e);
@ -164,15 +192,87 @@ public class ReportExcelServiceImpl implements ReportExcelService {
try { try {
out.close(); out.close();
file.delete(); file.delete();
if(!Objects.isNull(pdfFile))
{
pdfFile.delete();
}
} catch (IOException e) { } catch (IOException e) {
throw BusinessExceptionBuilder.build(ResponseCode.FILE_OPERATION_FAILED, e.getMessage()); throw BusinessExceptionBuilder.build(ResponseCode.FILE_OPERATION_FAILED, e.getMessage());
} }
} }
} }
return true; // 将Excel文件转换为PDF
public File convertExcelToPdf(String excelFilePath, String pdfFilePath) {
try {
// 读取Excel文件
Workbook workbook = new XSSFWorkbook(excelFilePath);
Sheet sheet = workbook.getSheetAt(0);
// 创建PDF文档
Document document = new Document(PageSize.A4);
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
document.open();
// 创建PDF表格
PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());
table.setWidthPercentage(100);
// 设置中文字体
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont, 10, Font.NORMAL);
// 设置表头
Row headerRow = sheet.getRow(0);
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
Cell headerCell = headerRow.getCell(i);
PdfPCell tableCell = new PdfPCell();
tableCell.setPhrase(new Phrase(getStringValue(headerCell), font));
table.addCell(tableCell);
} }
// 遍历Excel表格的行和列
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row == null) {
// 如果行为空创建一个空的行并添加到PDF表格中
row = sheet.createRow(i);
}
for (int j = 0; j < headerRow.getLastCellNum(); j++) {
Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (cell.getCellType() == org.apache.poi.ss.usermodel.CellType.BLANK) {
cell.setCellValue(" ");
}
PdfPCell tableCell = new PdfPCell();
tableCell.setPhrase(new Phrase(getStringValue(cell), font));
table.addCell(tableCell);
}
}
// 将表格添加到PDF文档中
document.add(table);
document.close();
workbook.close();
System.out.println("Excel转换为PDF成功");
return new File(pdfFilePath);
} catch (IOException e) {
e.printStackTrace();
} catch (com.itextpdf.text.DocumentException e) {
e.printStackTrace();
}
return null;
}
private String getStringValue(Cell cell) {
if (cell == null)
return "";
else
return cell.toString();
}
/** /**
* *
*/ */

@ -108,10 +108,10 @@ export default {
this.createSheet(); this.createSheet();
}, },
async download(val) { async download(val) {
if (val == "gaea_template_pdf") { // if (val == "gaea_template_pdf") {
this.$message("暂不支持pdf"); // this.$message("pdf");
return; // return;
} // }
const result = {}; const result = {};
result["reportCode"] = this.reportCode; result["reportCode"] = this.reportCode;
result["setParam"] = JSON.stringify(this.params.setParam); result["setParam"] = JSON.stringify(this.params.setParam);

Loading…
Cancel
Save