| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 
 | import org.apache.poi.xwpf.usermodel.*;import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
 import com.itextpdf.text.*;
 import com.itextpdf.text.pdf.*;
 
 import java.io.*;
 import java.util.List;
 import java.util.Map;
 
 public class ExportService {
 
 
 
 
 public byte[] generateWordReport(List<Map<String, Object>> data) throws IOException {
 try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
 XWPFDocument doc = new XWPFDocument();
 
 
 addCoverPage(doc);
 
 
 addTableOfContents(doc);
 
 
 addContentPages(doc, data);
 
 
 addHeaderFooter(doc);
 
 doc.write(out);
 return out.toByteArray();
 }
 }
 
 private void addCoverPage(XWPFDocument doc) {
 XWPFParagraph para = doc.createParagraph();
 para.setAlignment(ParagraphAlignment.CENTER);
 para.setVerticalAlignment(TextAlignment.CENTER);
 
 XWPFRun run = para.createRun();
 run.setText("公司机密报告");
 run.setBold(true);
 run.setFontSize(28);
 run.setColor("000080");
 run.addBreak();
 
 run = para.createRun();
 run.setText("生成日期: " + new java.util.Date());
 run.setFontSize(14);
 run.addBreak(BreakType.PAGE);
 }
 
 private void addTableOfContents(XWPFDocument doc) {
 XWPFParagraph para = doc.createParagraph();
 XWPFRun run = para.createRun();
 run.setText("目录");
 run.setBold(true);
 run.setFontSize(16);
 
 
 para = doc.createParagraph();
 run = para.createRun();
 run.setText("1. 数据概览 ...................... 2");
 run.addBreak();
 run.setText("2. 详细数据 ...................... 3");
 run.addBreak(BreakType.PAGE);
 }
 
 private void addContentPages(XWPFDocument doc, List<Map<String, Object>> data) {
 
 XWPFTable table = doc.createTable();
 
 
 CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();
 width.setType(STTblWidth.DXA);
 width.setW(9000);
 
 
 XWPFTableRow headerRow = table.getRow(0);
 String[] headers = {"ID", "姓名", "部门", "薪资"};
 for (int i = 0; i < headers.length; i++) {
 if (i == 0) {
 setCellStyle(headerRow.getCell(0), headers[i], true);
 } else {
 setCellStyle(headerRow.addNewTableCell(), headers[i], true);
 }
 }
 
 
 for (Map<String, Object> row : data) {
 XWPFTableRow dataRow = table.createRow();
 setCellStyle(dataRow.getCell(0), row.get("id").toString(), false);
 setCellStyle(dataRow.getCell(1), row.get("name").toString(), false);
 setCellStyle(dataRow.getCell(2), row.get("department").toString(), false);
 setCellStyle(dataRow.getCell(3), row.get("salary").toString(), false);
 }
 }
 
 private void setCellStyle(XWPFTableCell cell, String text, boolean isHeader) {
 cell.removeParagraph(0);
 XWPFParagraph para = cell.addParagraph();
 para.setAlignment(ParagraphAlignment.CENTER);
 XWPFRun run = para.createRun();
 run.setText(text);
 
 if (isHeader) {
 run.setBold(true);
 cell.setColor("D3D3D3");
 }
 }
 
 private void addHeaderFooter(XWPFDocument doc) {
 
 XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);
 XWPFParagraph hp = header.createParagraph();
 hp.setAlignment(ParagraphAlignment.RIGHT);
 XWPFRun hr = hp.createRun();
 hr.setText("公司机密 - 禁止外传");
 hr.setItalic(true);
 hr.setFontSize(10);
 
 
 XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);
 XWPFParagraph fp = footer.createParagraph();
 fp.setAlignment(ParagraphAlignment.CENTER);
 XWPFRun fr = fp.createRun();
 fr.setText("页码: ");
 fr.getCTR().addNewFldChar().setFldCharType(STFldCharType.BEGIN);
 fr.getCTR().addNewInstrText().setStringValue("PAGE");
 fr.getCTR().addNewFldChar().setFldCharType(STFldCharType.END);
 fr.getCTR().addNewInstrText().setStringValue(" / ");
 fr.getCTR().addNewFldChar().setFldCharType(STFldCharType.BEGIN);
 fr.getCTR().addNewInstrText().setStringValue("NUMPAGES");
 fr.getCTR().addNewFldChar().setFldCharType(STFldCharType.END);
 }
 
 
 
 
 public byte[] generatePdfReport(List<Map<String, Object>> data) throws DocumentException, IOException {
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 Document document = new Document(PageSize.A4, 50, 50, 70, 50);
 
 PdfWriter writer = PdfWriter.getInstance(document, out);
 
 
 document.addTitle("公司数据报告");
 document.addAuthor("系统自动生成");
 document.addCreator("企业报表系统");
 
 document.open();
 
 
 addPdfCoverPage(document);
 
 
 document.newPage();
 addPdfTableOfContents(document);
 
 
 document.newPage();
 addPdfContent(document, data);
 
 
 writer.setPageEvent(new PdfPageEvent());
 
 document.close();
 return out.toByteArray();
 }
 
 private void addPdfCoverPage(Document document) throws DocumentException {
 
 try {
 Image cover = Image.getInstance("company-logo.png");
 cover.scaleToFit(300, 300);
 cover.setAbsolutePosition(
 (PageSize.A4.getWidth() - cover.getScaledWidth()) / 2,
 (PageSize.A4.getHeight() - cover.getScaledHeight()) / 2 + 100
 );
 document.add(cover);
 } catch (IOException e) {
 
 }
 
 
 Font titleFont = new Font(Font.FontFamily.HELVETICA, 28, Font.BOLD, BaseColor.DARK_GRAY);
 Paragraph title = new Paragraph("年度数据报告\n\n", titleFont);
 title.setAlignment(Element.ALIGN_CENTER);
 title.setSpacingAfter(20);
 document.add(title);
 
 Font dateFont = new Font(Font.FontFamily.HELVETICA, 14, Font.ITALIC);
 Paragraph date = new Paragraph("生成日期: " + new java.util.Date() + "\n\n\n\n\n", dateFont);
 date.setAlignment(Element.ALIGN_CENTER);
 document.add(date);
 }
 
 private void addPdfTableOfContents(Document document) throws DocumentException {
 Font chapterFont = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD);
 Paragraph toc = new Paragraph("目录\n\n", chapterFont);
 toc.setAlignment(Element.ALIGN_CENTER);
 document.add(toc);
 
 
 Font itemFont = new Font(Font.FontFamily.HELVETICA, 12);
 Paragraph item1 = new Paragraph("1. 数据概览 ...................... 2", itemFont);
 document.add(item1);
 
 Paragraph item2 = new Paragraph("2. 详细数据 ...................... 3", itemFont);
 document.add(item2);
 }
 
 private void addPdfContent(Document document, List<Map<String, Object>> data) throws DocumentException {
 
 Font chapterFont = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD, BaseColor.BLUE);
 Chapter chapter = new Chapter(new Paragraph("详细数据", chapterFont), 1);
 chapter.setNumberDepth(0);
 document.add(chapter);
 
 
 PdfPTable table = new PdfPTable(4);
 table.setWidthPercentage(100);
 table.setSpacingBefore(20f);
 table.setSpacingAfter(30f);
 
 
 Font headerFont = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.WHITE);
 String[] headers = {"ID", "姓名", "部门", "薪资"};
 
 for (String header : headers) {
 PdfPCell cell = new PdfPCell(new Phrase(header, headerFont));
 cell.setBackgroundColor(BaseColor.DARK_GRAY);
 cell.setHorizontalAlignment(Element.ALIGN_CENTER);
 cell.setPadding(8);
 table.addCell(cell);
 }
 
 
 Font dataFont = new Font(Font.FontFamily.HELVETICA, 10);
 for (Map<String, Object> row : data) {
 addTableRow(table, row.get("id").toString(), dataFont);
 addTableRow(table, row.get("name").toString(), dataFont);
 addTableRow(table, row.get("department").toString(), dataFont);
 
 
 PdfPCell salaryCell = new PdfPCell(new Phrase(row.get("salary").toString(), dataFont));
 salaryCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
 salaryCell.setPadding(5);
 table.addCell(salaryCell);
 }
 
 document.add(table);
 
 
 try {
 Image chart = createPdfChart(data);
 chart.scaleToFit(400, 300);
 chart.setAlignment(Image.MIDDLE);
 document.add(chart);
 } catch (IOException e) {
 
 }
 }
 
 private void addTableRow(PdfPTable table, String text, Font font) {
 PdfPCell cell = new PdfPCell(new Phrase(text, font));
 cell.setPadding(5);
 table.addCell(cell);
 }
 
 private Image createPdfChart(List<Map<String, Object>> data) throws IOException, DocumentException {
 
 
 int width = 400;
 int height = 300;
 java.awt.Image awtImage = new java.awt.image.BufferedImage(width, height, java.awt.image.BufferedImage.TYPE_INT_RGB);
 java.awt.Graphics2D g2d = (java.awt.Graphics2D) awtImage.getGraphics();
 
 g2d.setColor(java.awt.Color.WHITE);
 g2d.fillRect(0, 0, width, height);
 g2d.setColor(java.awt.Color.BLUE);
 
 
 int barWidth = 30;
 int x = 50;
 for (Map<String, Object> row : data) {
 int salary = Integer.parseInt(row.get("salary").toString());
 int barHeight = salary / 1000;
 g2d.fillRect(x, height - barHeight - 50, barWidth, barHeight);
 g2d.drawString(row.get("name").toString(), x, height - 30);
 x += barWidth + 20;
 }
 
 g2d.dispose();
 
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 javax.imageio.ImageIO.write((java.awt.image.BufferedImage) awtImage, "png", baos);
 return Image.getInstance(baos.toByteArray());
 }
 
 
 
 
 class PdfPageEvent extends PdfPageEventHelper {
 public void onEndPage(PdfWriter writer, Document document) {
 try {
 
 PdfContentByte head = writer.getDirectContent();
 head.beginText();
 head.setFontAndSize(BaseFont.createFont(), 10);
 head.setTextMatrix(document.left(), document.top() + 20);
 head.showTextAligned(Element.ALIGN_RIGHT, "机密文档 - 第" + writer.getPageNumber() + "页",
 document.right(), document.top() + 20, 0);
 head.endText();
 
 
 PdfContentByte foot = writer.getDirectContent();
 foot.beginText();
 foot.setFontAndSize(BaseFont.createFont(), 10);
 foot.setTextMatrix(document.left(), document.bottom() - 20);
 foot.showTextAligned(Element.ALIGN_CENTER, "© 2023 公司名称. 保留所有权利.",
 (document.right() + document.left()) / 2, document.bottom() - 20, 0);
 foot.endText();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 }
 }
 
 |