杜邦分析圖例java
java代碼字體
package cn.com.fasoft.bank.util; import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import cn.com.fasoft.bank.form.zhibiao.DbfxfForm; import cn.com.fasoft.core.util.writer.BaseCell; import cn.com.fasoft.core.util.writer.BaseParagraph; import cn.com.fasoft.core.util.writer.BaseWriter; import cn.com.fasoft.core.util.writer.ChineseFont; import com.lowagie.text.BadElementException; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Image; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Table; import com.lowagie.text.rtf.field.RtfPageNumber; import com.lowagie.text.rtf.field.RtfTotalPageNumber; import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter; import com.lowagie.text.rtf.style.RtfParagraphStyle; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageDecoder; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class DbfxfToWord extends BaseWriter { private String imgPath; public String getImgPath() { return imgPath; } public void setImgPath(String imgPath) { this.imgPath = imgPath; } public DbfxfToWord(String filepath, String filename, int type, boolean bol) { super(filepath + filename, type, bol); this.setImgPath(filepath); } public DbfxfToWord(String title) { super(title); } // 設置頁眉 public void setHeader(String title) { HeaderFooter header = new HeaderFooter(new Phrase(title, ChineseFont .create(12, BOLD)), false); header.setBorder(2); header.setAlignment(RIGHT); document.setHeader(header); } // 設置頁腳 public void setFooder() { Font fontSong = ChineseFont.create(9, NORMAL); HeaderFooter footer = new HeaderFooter(new Phrase("第 ", fontSong), new Phrase(" 頁", fontSong)); footer.setAlignment(RIGHT); footer.setBorder(1); document.setFooter(footer); } // 設置頁腳 public void setFooter() { try { table = new Table(2); table.setBorder(0); table.setWidth(100); Paragraph pageNotify = new BaseParagraph("內部資料,注意保密", LEFT, 9, NORMAL); table.addCell(new Cell(pageNotify)); Paragraph pageNumber = new BaseParagraph("第 ", RIGHT, 9, NORMAL); pageNumber.add(new RtfPageNumber()); pageNumber.add(" 頁 共 "); pageNumber.add(new RtfTotalPageNumber()); pageNumber.add("頁"); table.addCell(new Cell(pageNumber)); document.setFooter(new RtfHeaderFooter(table)); } catch (BadElementException e) { e.printStackTrace(); } } // 輸出一級標題 public void setTitle1(String value) { try { Paragraph parag = new Paragraph(value, RtfParagraphStyle.STYLE_HEADING_1); parag.setAlignment(LEFT); parag.setSpacingBefore(0); parag.setSpacingAfter(0); // RtfParagraphStyle.STYLE_HEADING_1.setColor(Color.BLUE); RtfParagraphStyle.STYLE_HEADING_1.setSize(18); document.add(parag); } catch (DocumentException e) { e.printStackTrace(); } } // 輸出二級標題 public void setTitle2(String value) { try { Paragraph parag = new Paragraph(value, RtfParagraphStyle.STYLE_HEADING_2); parag.setAlignment(LEFT); parag.setSpacingBefore(0); parag.setSpacingAfter(0); RtfParagraphStyle.STYLE_HEADING_2.setSize(16); document.add(parag); } catch (DocumentException e) { e.printStackTrace(); } } // 輸出三級標題 public void setTitle3(String value) { try { Paragraph parag = new Paragraph(value, RtfParagraphStyle.STYLE_HEADING_3); parag.setAlignment(LEFT); parag.setSpacingBefore(0); parag.setSpacingAfter(0); RtfParagraphStyle.STYLE_HEADING_3.setSize(14); document.add(parag); } catch (DocumentException e) { e.printStackTrace(); } } // 輸出一段文字 public void SetConclusion(String value) { try { document.add(new Paragraph(" " + value, ChineseFont.create(14, NORMAL))); } catch (DocumentException e) { e.printStackTrace(); } } // 輸出當前字符串 public void AddChar(String value) { try { document.add(new Paragraph(value, ChineseFont.create(14, NORMAL))); } catch (DocumentException e) { e.printStackTrace(); } } public void setTitle(String value) { try { document.add(new BaseParagraph(value, CENTER, 22, BOLD)); document.add(new Paragraph("\t")); } catch (DocumentException e) { e.printStackTrace(); } } // 添加圖片 public void AddImage(String imgPath) { try { Image img = Image.getInstance(imgPath); img.setAlignment(Image.MIDDLE); document.add(img); document.add(new Paragraph("\t")); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // 結束 public void End() { try { close(); } catch (Exception e) { e.printStackTrace(); } } public void simpleTable(String dateStr, String tableName, List dateContent, int colNumber,List suitecodeList) { Map map = new HashMap();// 驗證數據用 Map map1 = new HashMap();// 驗證數據 Font fontChinese = ChineseFont.create(9, NORMAL); try { // 設置表頭 document.add(new BaseParagraph(tableName, CENTER, 15, BOLD)); // 建立表格 table = new Table(colNumber); table.setBorderWidth(0); // table.getDefaultCell().setBackgroundColor(Color.ORANGE); table.getDefaultCell().setHorizontalAlignment(CENTER); // 添加表頭元素 table.addCell(new BaseCell("杜邦", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("淨資產收益率", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("資產利潤率", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("權益乘數", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("銷售淨利率", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("資產週轉率", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("少數權益率", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("資產負債率", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("淨利潤", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("銷售收入", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("資產總額", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("少數股東權益", 1, 1, 14, NORMAL)); table.addCell(new BaseCell("負債總額", 1, 1, 14, NORMAL)); for (int k = 0; k < dateContent.size(); k++) { DbfxfForm dbfxfform = (DbfxfForm) dateContent.get(k); table.addCell(dbfxfform.getDateId()); table.addCell(dbfxfform.getValue_A()); table.addCell(dbfxfform.getValue_AA()); table.addCell(dbfxfform.getValue_AB()); table.addCell(dbfxfform.getValue_AAA()); table.addCell(dbfxfform.getValue_AAB()); table.addCell(dbfxfform.getValue_AAC()); table.addCell(dbfxfform.getValue_AAD()); table.addCell(dbfxfform.getValue_AAAA()); table.addCell(dbfxfform.getValue_AAAB()); table.addCell(dbfxfform.getValue_AABB()); table.addCell(dbfxfform.getValue_AACA()); table.addCell(dbfxfform.getValue_AADA()); } document.add(getsuitecodeList(suitecodeList));//增長數據來源 document.add(new Paragraph("\t")); document.add(new Paragraph("\t")); document.add(table); document.add(new Paragraph("\t")); document.add(new Paragraph("\t")); document.add(new Paragraph("\t")); try { String fileUrl=this.getImgPath()+ "\\dbfxf\\bg.jpg"; InputStream imageIn = new FileInputStream(new File(fileUrl)); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn); BufferedImage image = decoder.decodeAsBufferedImage(); Graphics g = image.getGraphics(); // 設置顏色 g.setColor(Color.black); // 設置字體 java.awt.Font mFont = new java.awt.Font("Arial", java.awt.Font.PLAIN, 11);// 默認字體 // if (fontStyle.equalsIgnoreCase("italic")) // if (fontStyle.equalsIgnoreCase("bold")) // mFont = new Font(fontName, Font.ITALIC, 12); // if (fontStyle.equalsIgnoreCase("plain")) // mFont = new Font(fontName, Font.ITALIC, 12); g.setFont(mFont); // 輸出文字 String[] dataArr = dateStr.split(","); g.drawString(dataArr[0], 377, 19); g.drawString(dataArr[1], 181, 99); g.drawString(dataArr[2], 554, 99); g.drawString(dataArr[3], 88, 176); g.drawString(dataArr[4], 280, 176); g.drawString(dataArr[5], 493, 176); g.drawString(dataArr[6], 657, 176); g.drawString(dataArr[7], 3, 271); g.drawString(dataArr[8], 92, 271); g.drawString(dataArr[9], 189, 271); g.drawString(dataArr[10], 286, 271); g.drawString(dataArr[11], 376, 271); g.drawString(dataArr[12], 482, 271); g.drawString(dataArr[13], 579, 271); g.drawString(dataArr[14], 669, 271); // 輸出數據流 Image img = Image.getInstance(ChartUtilities.encodeAsPNG( image)); img.setAlignment(Element.ALIGN_CENTER); img.scaleAbsolute(600,200); // img.scalePercent(62, 57); document.add(img); document.add(new Paragraph(" ")); } catch (Exception e) { e.printStackTrace(); } } catch (DocumentException e) { e.printStackTrace(); } } private String getDateTime() { SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String ret = fmt.format(new java.util.Date()); return ret; } public void processorDateContent(Map reportMap, String dateStr) throws IOException { List dateContent = (List) reportMap.get("dataList"); List suitecodeList=(List)reportMap.get("suitecodeList"); int titleNum = 13; setFooter(); simpleTable(dateStr, "杜邦分析", dateContent, titleNum,suitecodeList); End(); } public void saveWord() { try { close(); } catch (Exception e) { e.printStackTrace(); } } public void writeImage(Document document, JFreeChart chart, int aWidth, int aHeight) { try { Image img = Image.getInstance(ChartUtilities.encodeAsPNG(chart .createBufferedImage(769, 240, 740D, 200D, null))); img.setAlignment(Element.ALIGN_CENTER); img.scaleAbsolute(aWidth, aHeight); // img.scalePercent(62, 57); document.add(img); document.add(new Paragraph(" ")); // document.add(new Paragraph("\t")); } catch (DocumentException docEx) { docEx.printStackTrace(); } catch (IOException ioEx) { ioEx.printStackTrace(); } } }
---恢復內容結束---ui