From 67beae402c92228b458c7d0b9bd226bdaa3517c8 Mon Sep 17 00:00:00 2001 From: chenxg Date: Mon, 7 Aug 2023 14:12:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E8=BF=AD=E4=BB=A3-Excel?= =?UTF-8?q?=E6=8A=A5=E8=A1=A8=E5=AF=BC=E5=87=BAPDF-=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F-2(=E5=8A=A0=E5=88=A0=E9=99=A4=E7=BA=BF?= =?UTF-8?q?=E3=80=81=E6=B0=B4=E5=B9=B3=E5=AF=B9=E9=BD=90=E3=80=81=E5=9E=82?= =?UTF-8?q?=E7=9B=B4=E5=AF=B9=E9=BD=90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ReportExcelServiceImpl.java | 89 +++++++++++++++++-- 1 file changed, 81 insertions(+), 8 deletions(-) diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java index 4069daf2..64c4116f 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java @@ -243,7 +243,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { ReportExcelStyleDto reportExcelStyleDto = reportExcelStyleList.get(0).get(i); if(!Objects.isNull(reportExcelStyleDto)) { - processCellStyle(reportExcelStyleDto,tableCell); + processCellStyle(reportExcelStyleDto,tableCell,font); } table.addCell(tableCell); } @@ -265,7 +265,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { ReportExcelStyleDto reportExcelStyleDto = reportExcelStyleList.get(i).get(j); if(!Objects.isNull(reportExcelStyleDto)) { - processCellStyle(reportExcelStyleDto,tableCell); + processCellStyle(reportExcelStyleDto,tableCell,font); } table.addCell(tableCell); } @@ -293,27 +293,31 @@ public class ReportExcelServiceImpl implements ReportExcelService { * 处理导出pdf文件样式 * @param tableCell */ - public void processCellStyle(ReportExcelStyleDto reportExcelStyleDto,PdfPCell tableCell) + public void processCellStyle(ReportExcelStyleDto reportExcelStyleDto,PdfPCell tableCell,Font font) { - //处理单元格背景颜色 + // 处理单元格背景颜色 String bg = reportExcelStyleDto.getBg(); java.awt.Color color = null; if(!Objects.isNull(bg)) { - color = java.awt.Color.decode(bg); + color = parseRGB(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(); + Boolean cl = reportExcelStyleDto.isCl(); + Integer ht = reportExcelStyleDto.getHt(); + Integer vt = reportExcelStyleDto.getVt(); + + // 设置字体颜色 if(!Objects.isNull(fc)) { - color = java.awt.Color.decode(fc); + color = parseRGB(fc); font.setColor(new BaseColor(color.getRed(), color.getGreen(), color.getBlue())); } // 设置字体 @@ -341,6 +345,61 @@ public class ReportExcelServiceImpl implements ReportExcelService { { font.setStyle(Font.BOLDITALIC); } + // 是否删除线 + if(Objects.equals(Boolean.TRUE,cl)) + { + // 如果是粗体且斜体 + if (font.getStyle() == Font.BOLDITALIC) + { + font.setStyle(Font.BOLDITALIC | Font.STRIKETHRU); + } + // 如果是粗体 + else if(font.getStyle() == Font.BOLD) + { + font.setStyle(Font.BOLDITALIC | Font.STRIKETHRU); + } + // 如果是斜体 + else if(font.getStyle() == Font.ITALIC) + { + font.setStyle(Font.BOLDITALIC | Font.STRIKETHRU); + } + else + { + font.setStyle(Font.STRIKETHRU); + } + } + // 水平对齐 + if(!Objects.isNull(ht)) + { + if(Objects.equals(ht,0)) + { + tableCell.setHorizontalAlignment(Element.ALIGN_CENTER); + } + else if(Objects.equals(ht,1)) + { + tableCell.setHorizontalAlignment(Element.ALIGN_LEFT); + } + else if(Objects.equals(ht,2)) + { + tableCell.setHorizontalAlignment(Element.ALIGN_RIGHT); + } + } + // 垂直对齐 + if(!Objects.isNull(vt)) + { + if(Objects.equals(ht,0)) + { + tableCell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE); + } + else if(Objects.equals(ht,1)) + { + tableCell.setVerticalAlignment(Element.ALIGN_TOP); + } + else if(Objects.equals(ht,2)) + { + tableCell.setVerticalAlignment(Element.ALIGN_BOTTOM); + } + } Phrase phrase = tableCell.getPhrase(); tableCell.setPhrase(new Paragraph(phrase.getContent(), font)); //处理字体 @@ -348,6 +407,20 @@ public class ReportExcelServiceImpl implements ReportExcelService { } + public static java.awt.Color parseRGB(String rgb) { + try { + String[] components = rgb.substring(rgb.indexOf("(") + 1, rgb.indexOf(")")).split(","); + int red = Integer.parseInt(components[0].trim()); + int green = Integer.parseInt(components[1].trim()); + int blue = Integer.parseInt(components[2].trim()); + + return new java.awt.Color(red, green, blue); + } catch (Exception e) { + e.printStackTrace(); + return null; // 解析失败,返回null + } + } + private String getStringValue(Cell cell) { if (cell == null) return ""; From 7a47aebaaaeda0339e8f526077e2fe3b890548fa Mon Sep 17 00:00:00 2001 From: chenxg Date: Mon, 7 Aug 2023 14:12:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E8=BF=AD=E4=BB=A3-Excel?= =?UTF-8?q?=E6=8A=A5=E8=A1=A8=E5=AF=BC=E5=87=BAPDF-=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F-2(=E5=8A=A0=E5=88=A0=E9=99=A4=E7=BA=BF?= =?UTF-8?q?=E3=80=81=E6=B0=B4=E5=B9=B3=E5=AF=B9=E9=BD=90=E3=80=81=E5=9E=82?= =?UTF-8?q?=E7=9B=B4=E5=AF=B9=E9=BD=90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ReportExcelServiceImpl.java | 89 +++++++++++++++++-- 1 file changed, 81 insertions(+), 8 deletions(-) diff --git a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java index 4069daf2..911d1ada 100644 --- a/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java +++ b/report-core/src/main/java/com/anjiplus/template/gaea/business/modules/reportexcel/service/impl/ReportExcelServiceImpl.java @@ -243,7 +243,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { ReportExcelStyleDto reportExcelStyleDto = reportExcelStyleList.get(0).get(i); if(!Objects.isNull(reportExcelStyleDto)) { - processCellStyle(reportExcelStyleDto,tableCell); + processCellStyle(reportExcelStyleDto,tableCell,font); } table.addCell(tableCell); } @@ -265,7 +265,7 @@ public class ReportExcelServiceImpl implements ReportExcelService { ReportExcelStyleDto reportExcelStyleDto = reportExcelStyleList.get(i).get(j); if(!Objects.isNull(reportExcelStyleDto)) { - processCellStyle(reportExcelStyleDto,tableCell); + processCellStyle(reportExcelStyleDto,tableCell,font); } table.addCell(tableCell); } @@ -293,27 +293,31 @@ public class ReportExcelServiceImpl implements ReportExcelService { * 处理导出pdf文件样式 * @param tableCell */ - public void processCellStyle(ReportExcelStyleDto reportExcelStyleDto,PdfPCell tableCell) + public void processCellStyle(ReportExcelStyleDto reportExcelStyleDto,PdfPCell tableCell,Font font) { - //处理单元格背景颜色 + // 处理单元格背景颜色 String bg = reportExcelStyleDto.getBg(); java.awt.Color color = null; if(!Objects.isNull(bg)) { - color = java.awt.Color.decode(bg); + color = parseRGB(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(); + Boolean cl = reportExcelStyleDto.isCl(); + Integer ht = reportExcelStyleDto.getHt(); + Integer vt = reportExcelStyleDto.getVt(); + + // 设置字体颜色 if(!Objects.isNull(fc)) { - color = java.awt.Color.decode(fc); + color = parseRGB(fc); font.setColor(new BaseColor(color.getRed(), color.getGreen(), color.getBlue())); } // 设置字体 @@ -341,6 +345,61 @@ public class ReportExcelServiceImpl implements ReportExcelService { { font.setStyle(Font.BOLDITALIC); } + // 是否删除线 + if(Objects.equals(Boolean.TRUE,cl)) + { + // 如果是粗体且斜体 + if (font.getStyle() == Font.BOLDITALIC) + { + font.setStyle(Font.BOLDITALIC | Font.STRIKETHRU); + } + // 如果是粗体 + else if(font.getStyle() == Font.BOLD) + { + font.setStyle(Font.BOLD | Font.STRIKETHRU); + } + // 如果是斜体 + else if(font.getStyle() == Font.ITALIC) + { + font.setStyle(Font.ITALIC | Font.STRIKETHRU); + } + else + { + font.setStyle(Font.STRIKETHRU); + } + } + // 水平对齐 + if(!Objects.isNull(ht)) + { + if(Objects.equals(ht,0)) + { + tableCell.setHorizontalAlignment(Element.ALIGN_CENTER); + } + else if(Objects.equals(ht,1)) + { + tableCell.setHorizontalAlignment(Element.ALIGN_LEFT); + } + else if(Objects.equals(ht,2)) + { + tableCell.setHorizontalAlignment(Element.ALIGN_RIGHT); + } + } + // 垂直对齐 + if(!Objects.isNull(vt)) + { + if(Objects.equals(ht,0)) + { + tableCell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE); + } + else if(Objects.equals(ht,1)) + { + tableCell.setVerticalAlignment(Element.ALIGN_TOP); + } + else if(Objects.equals(ht,2)) + { + tableCell.setVerticalAlignment(Element.ALIGN_BOTTOM); + } + } Phrase phrase = tableCell.getPhrase(); tableCell.setPhrase(new Paragraph(phrase.getContent(), font)); //处理字体 @@ -348,6 +407,20 @@ public class ReportExcelServiceImpl implements ReportExcelService { } + public static java.awt.Color parseRGB(String rgb) { + try { + String[] components = rgb.substring(rgb.indexOf("(") + 1, rgb.indexOf(")")).split(","); + int red = Integer.parseInt(components[0].trim()); + int green = Integer.parseInt(components[1].trim()); + int blue = Integer.parseInt(components[2].trim()); + + return new java.awt.Color(red, green, blue); + } catch (Exception e) { + e.printStackTrace(); + return null; // 解析失败,返回null + } + } + private String getStringValue(Cell cell) { if (cell == null) return "";