📄 reportutil.java
字号:
package com.sxit.wap.report;import java.util.*;import java.io.*;import com.sxit.wap.channel.*;import org.apache.poi.hssf.usermodel.*;import org.jfree.data.*;import org.jfree.chart.JFreeChart;import java.sql.Timestamp;import java.awt.image.BufferedImage;import org.jfree.chart.ChartUtilities;import org.jfree.chart.ChartFactory;import org.jfree.chart.plot.PlotOrientation;public class ReportUtil { Collection coll = new ArrayList(); Vector fields = new Vector(); Vector heads = new Vector(); String path = ""; String fileType = ""; String title = ""; String xaxis = ""; String yaxis = ""; public ReportUtil(Collection coll) { this.coll = coll; } public ReportUtil(Collection coll, String path, String fileType, String title, String xaxis, String yaxis) { this.coll = coll; this.path = path; this.fileType = fileType; this.title = title; this.xaxis = xaxis; this.yaxis = yaxis; } public static String encode(String value) { try { return new String(value.getBytes("ISO-8859-1"),"GB2312"); } catch (Exception e) { return value; } } public void addColumn(String fieldName, String fieldDesc) { fields.add(fieldName); heads.add(fieldDesc); } public Collection getColl() { return coll; } public Vector getFields() { return fields; } public Vector getHeads() { return heads; } private String genFileName() { java.util.Date now =new java.util.Date(); java.text.SimpleDateFormat sfarmat = new java.text.SimpleDateFormat("yyyyMMddHHmmss"); String fname=sfarmat.format(now); return fname; } public String createFile() { if ("excel".equals(fileType)) { return createExcelFile(); } else if ("histogram".equals(fileType)) { return createHistogramFile(); } else if ("graph".equals(fileType)) { return createGraphFile(); } else if ("caky".equals(fileType)) { return createCakyFile(); } else { return ""; } } public String createFile(String path, String fileType, String title, String xaxis, String yaxis) { this.path = path; this.fileType = fileType; this.title = title; this.xaxis = xaxis; this.yaxis = yaxis; if ("excel".equals(fileType)) { return createExcelFile(); } else if ("histogram".equals(fileType)) { return createHistogramFile(); } else if ("graph".equals(fileType)) { return createGraphFile(); } else if ("caky".equals(fileType)) { return createCakyFile(); } else { return ""; } } public String createExcelFile() { String fileName = genFileName() + ".xls"; try{ String location= path + fileName; short rownum = 0; FileOutputStream out = new FileOutputStream(location); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; wb.setSheetName(0, "stat sheet"); r = s.createRow(rownum++); for (short i=(short)0; i<heads.size(); i++) { String headName = (String)heads.elementAt(i); c = r.createCell((short)i); c.setCellType(HSSFCell.CELL_TYPE_STRING); c.setEncoding(c.ENCODING_UTF_16); c.setCellValue(headName); } Iterator it = coll.iterator(); while (it.hasNext()) { Hashtable element = (Hashtable)it.next(); r = s.createRow(rownum++); for (short i=(short)0; i<fields.size(); i++) { String fieldName = (String)fields.elementAt(i); String value = (String)element.get(fieldName); c = r.createCell((short)i); c.setCellType(HSSFCell.CELL_TYPE_STRING); c.setEncoding(c.ENCODING_UTF_16); c.setCellValue(value); } } wb.write(out); out.close(); }catch(Exception e){ e.printStackTrace(); } return fileName; } public String createHistogramFile() {//柱形图 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); try { Iterator it = coll.iterator(); while (it.hasNext()) { Hashtable element = (Hashtable)it.next(); String keyName = ""; for (int i=0; i<fields.size(); i++) { String fieldName = (String)fields.elementAt(i); String fieldDesc = (String)heads.elementAt(i); String value = (String)element.get(fieldName); if (i == 0) { keyName = value; continue; } dataset.addValue(Integer.parseInt(value), fieldDesc, keyName); } } } catch (Exception e) { e.printStackTrace(); } return createHistogramFile(dataset); } public String createHistogramFile(DefaultCategoryDataset dataset) {//柱形图 JFreeChart chart = ChartFactory.createBarChart3D( title, // 图表标题 xaxis, // 目录轴的显示标签 yaxis, // 数值轴的显示标签 dataset, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平(HORIZONTAL)、垂直(VERTICAL) true, // 是否显示图例(对于简单的柱状图必须是false) false, // 是否生成工具 false // 是否生成URL链接 ); return outChart(chart, dataset); } public String createGraphFile() {//曲线图 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); try { Iterator it = coll.iterator(); while (it.hasNext()) { Hashtable element = (Hashtable)it.next(); String keyName = ""; for (int i=0; i<fields.size(); i++) { String fieldName = (String)fields.elementAt(i); String fieldDesc = (String)heads.elementAt(i); String value = (String)element.get(fieldName); if (i == 0) { keyName = value; continue; } dataset.addValue(Integer.parseInt(value), fieldDesc, keyName); } } } catch (Exception e) { e.printStackTrace(); } return createGraphFile(dataset); } public String createGraphFile(DefaultCategoryDataset dataset) {//曲线图 JFreeChart chart = ChartFactory.createLineChart( title, // chart title xaxis, // domain axis label yaxis, // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); return outChart(chart, dataset); } public String createCakyFile(DefaultPieDataset dataset) {//饼图/* dataset.setValue("Java", new Double(43.2)); dataset.setValue("Visual Basic", new Double(10.0)); dataset.setValue("C/C++", new Double(17.5)); dataset.setValue("PHP", new Double(32.5)); dataset.setValue("Perl", new Double(1.0));*/ JFreeChart chart = ChartFactory.createPieChart3D( title, // chart title dataset, // data true, // include legend true, false ); return outChart(chart, dataset); } public String createCakyFile() {//饼图 DefaultPieDataset dataset = new DefaultPieDataset(); try { Iterator it = coll.iterator(); while (it.hasNext()) { Hashtable element = (Hashtable)it.next(); dataset.setValue((String)element.get((String)fields.elementAt(0)), Integer.parseInt((String)element.get((String)fields.elementAt(1)))); } } catch (Exception e) { e.printStackTrace(); } return createCakyFile(dataset); } public String outChart(JFreeChart chart, PieDataset dataset) { try { String fileName = genFileName() + ".png"; File file1 = new File(path + fileName); OutputStream out = new BufferedOutputStream(new FileOutputStream(file1)); BufferedImage image = chart.createBufferedImage(550, 400); ChartUtilities.writeBufferedImageAsPNG(out, image); out.close(); return fileName; } catch (IOException e) { System.out.println(e.toString()); return ""; } } public String outChart(JFreeChart chart, CategoryDataset dataset) { try { int columnCount = dataset.getColumnKeys().size(); String fileName = genFileName() + ".png"; System.out.println("path=" + path); System.out.println("path + fileName="+path + fileName); File file1 = new File(path + fileName); OutputStream out = new BufferedOutputStream(new FileOutputStream(file1)); BufferedImage image = chart.createBufferedImage(100 * columnCount + 200, 500); ChartUtilities.writeBufferedImageAsPNG(out, image); out.close(); return fileName; } catch (IOException e) { System.out.println(e.toString()); return ""; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -