小虎j2ee歷程

          小虎的小小收集

          常用鏈接

          統計

          最新評論

          java 類

          DownloadExcel  --excel導出和下載類

          package com.san.report;

          import java.awt.image.BufferedImage;
          import java.io.ByteArrayOutputStream;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.OutputStream;
          import java.math.BigInteger;
          import java.util.List;

          import javax.imageio.ImageIO;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import javax.servlet.http.HttpSession;

          import org.apache.poi.hssf.usermodel.HSSFCell;
          import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
          import org.apache.poi.hssf.usermodel.HSSFPatriarch;
          import org.apache.poi.hssf.usermodel.HSSFRow;
          import org.apache.poi.hssf.usermodel.HSSFSheet;
          import org.apache.poi.hssf.usermodel.HSSFWorkbook;
          import org.apache.poi.hssf.util.Region;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.actions.DispatchAction;


          /**
           * 簡單Excel導出工具
           *
           * @author shmilyLT
           *
           * TODO To change the template for this generated type comment go to Window -
           * Preferences - Java - Code Style - Code Templates
           */
          public class DownloadExcel extends DispatchAction {
           
           /**
            * Excel文件下載
            * @param mapping
            * @param form
            * @param request
            * @param response
            * @return
            */
           public ActionForward downloadExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) {
            HttpSession session = request.getSession();
            String outputFile = (String)session.getAttribute("outputFile");
            OutputStream os = null;
            FileInputStream fis = null;
            File file = null;
            try {
             if(outputFile != null) {
              file = new File(outputFile);
              fis=new FileInputStream(outputFile);
              String contentType = "application/octet-stream;charset=iso8859-1";
              response.setContentType(contentType);
              StringBuffer contentDisposition = new StringBuffer();
              contentDisposition.append("attachment;");
              contentDisposition.append("filename=\"");
              contentDisposition.append(file.getName());
              contentDisposition.append("\"");
              response.setHeader("Content-Disposition", new String(
                contentDisposition.toString().getBytes(
                  System.getProperty("file.encoding")), "iso8859-1"));   
              os=response.getOutputStream();
              byte[] b=new byte[1024*2];
              int i=0;
              while((i=fis.read(b,0,b.length))>0){
               //當前線程等待0.1
               //Thread.sleep(100);
               os.write(b,0,i);
              }
              
             }
            } catch(Exception e) {
             e.printStackTrace();
             System.out.println("用戶取消操作");
            } finally {
             try {
              if(fis != null) {
               fis.close();
              }
              if(os != null) {
               os.close();
              }
              if(file != null) {
               file.delete();
              }
             } catch(Exception e) {
              
             }
             
            }
            
            return null;
           }
           
           
           /*****************
            *
            * 異步寫入excel文件;
            *
            *
            * ******************/
           public static void getExcelObject(ExcelObj obj,HttpServletRequest request,HttpServletResponse response) throws Exception {
            try {

             if(obj == null) {
              return;
             }
             
             String excelName = obj.getExcelName();//excel名稱
             String sheetName = obj.getSheetName();//sheet名稱
             
             
             String imgList1 = obj.getImg1();//圖片
             String bTitleList1 = obj.getBTitle1();//大標題list
             List sTitleList1 = obj.getSTitle1();//小標題list
             List objList1 = obj.getObj1();//內容
             
             String imgList2 = obj.getImg2();//圖片
             String bTitleList2 = obj.getBTitle2();//大標題list
             List sTitleList2 = obj.getSTitle2();//小標題list
             List objList2 = obj.getObj2();//內容
             
             HSSFWorkbook workbook = new HSSFWorkbook();
             HSSFSheet sheet = workbook.createSheet(sheetName);
             
             HSSFRow row = null;
             HSSFCell cell = null;
             
             int countX = 0;
             int countY = 0;
             if(bTitleList1 != null && !"".equals(bTitleList1)) {
              row = sheet.createRow(countX);
              cell = row.createCell((short)countY);
              cell.setEncoding(HSSFCell.ENCODING_UTF_16);
              cell.setCellValue(bTitleList1);
              if(sTitleList1 != null && sTitleList1.size() !=0) {
               sheet.addMergedRegion(new Region(countX,(short)countY,countX,(short)(sTitleList1.size()-1)));
              }
              countX++;
              countY = sTitleList1.size();
             }
             
             if(sTitleList1 != null && sTitleList1.size() !=0) {
              for(int i=0;i<sTitleList1.size();i++) {
               row = sheet.createRow(countX);
               cell = row.createCell((short)i);
               cell.setCellValue((String)sTitleList1.get(i));
               
               
              }
              countX++;
              
             }
             
             
             if(objList1 != null) {
              for(int i=0;i<objList1.size();i++) {
               Object[] o = (Object[])objList1.get(i);
               for(int j=0;j<2;j++) {
                row = sheet.createRow(countX);
                cell = row.createCell((short)j);
                if(o[j] instanceof String) {
                 cell.setCellValue((String)o[j]);
                } else if(o[j] instanceof java.math.BigInteger) {
                 cell.setCellValue(((BigInteger)o[j]).toString());
                }
                
                //countY++;
               }
               countX++;
              }
             }
             
          //   ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
          //   BufferedImage bufferImg = ImageIO.read(new File("c:/123.jpg"));
          //   ImageIO.write(bufferImg,"jpeg",byteArrayOut);
          //   HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
          //   HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1,1,(short) (countY+2),1,(short)(countY+8),16);
          //   //HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 0,15,(short)10,20);
          //   patriarch.createPicture(anchor , workbook.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
             BufferedImage bufferImg =null;
             HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
             ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
             HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1,1,(short) (countY+2),1,(short)(countY+8),16);
             bufferImg = ImageIO.read(new File(imgList1));
             ImageIO.write(bufferImg,"jpeg",byteArrayOut);
             patriarch.createPicture(anchor , workbook.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
             
             
             if(objList1 != null && objList1.size() < 16) {
              countX = 19;
             }
             countY =0;
             
             BufferedImage bufferImg1 = null;
             ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
             if(imgList2 !=null && !"".equals(imgList2)){
              bufferImg1 = ImageIO.read(new File(imgList2));
              ImageIO.write(bufferImg1,"jpeg",byteArrayOut1);
             }
             int temp = 0;
             if(sTitleList2 != null) {
              temp = sTitleList2.size();
             }
             
             HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,1,1,(short) (countY +2 + temp),(countX),(short)(countY+8 + temp),(countX+17));
             patriarch.createPicture(anchor1 , workbook.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
             
             
             if(bTitleList2 != null && !"".equals(bTitleList2)) {
              row = sheet.createRow(countX);
              cell = row.createCell((short)countY);
              cell.setEncoding(HSSFCell.ENCODING_UTF_16);
              cell.setCellValue(bTitleList2);
              if(sTitleList2 != null && sTitleList2.size() !=0) {
               sheet.addMergedRegion(new Region(countX,(short)countY,countX,(short)(sTitleList2.size()-1)));
              }
              countX++;
              countY += sTitleList2.size();
             }
             
             
             if(sTitleList2 != null && sTitleList2.size() !=0) {
              for(int i=0;i<sTitleList2.size();i++) {
               row = sheet.createRow(countX);
               cell = row.createCell((short)i);
               cell.setCellValue((String)sTitleList2.get(i));
               
               
              }
              countX++;
              
             }
             
             
             if(objList2 != null) {
              for(int i=0;i<objList2.size();i++) {
               Object[] o = (Object[])objList2.get(i);
               for(int j=0;j<2;j++) {
                row = sheet.createRow(countX);
                cell = row.createCell((short)j);
                if(o[j] instanceof String) {
                 cell.setCellValue((String)o[j]);
                } else if(o[j] instanceof java.math.BigInteger) {
                 cell.setCellValue(((BigInteger)o[j]).toString());
                } else if(o[j] instanceof Integer) {
                 cell.setCellValue(o[j].toString());
                }
                //countY++;
               }
               countX++;
              }
             }
             
             
             
             excelName = InitServlet.sysPath+"exceldown\\"+ excelName+".xls";
             
             
             
             
             FileOutputStream fileOut = new FileOutputStream(excelName);
             workbook.write(fileOut);
             
             if(fileOut != null) {
              fileOut.close();
             }
             //文件下載
             HttpSession session = request.getSession();
             String outputFile = excelName;
             session.setAttribute("outputFile", outputFile);
             if(null!=imgList1 && !"".equals(imgList1)){
               File file=new File(imgList1);
               file.delete();
             }
             
             if(null!=imgList2 && !"".equals(imgList2)){
               File file=new File(imgList2);
               file.delete();
             }
             
          //   OutputStream os = null;
          //   FileInputStream fis = null;
          //   File file = null;
          //   try {
          //    if(outputFile != null) {
          //     file = new File(outputFile);
          //     fis=new FileInputStream(outputFile);
          //     String contentType = "application/octet-stream;charset=iso8859-1";
          //     response.setContentType(contentType);
          //     StringBuffer contentDisposition = new StringBuffer();
          //     contentDisposition.append("attachment;");
          //     contentDisposition.append("filename=\"");
          //     contentDisposition.append(file.getName());
          //     contentDisposition.append("\"");
          //     response.setHeader("Content-Disposition", new String(
          //       contentDisposition.toString().getBytes(
          //         System.getProperty("file.encoding")), "iso8859-1"));   
          //     os=response.getOutputStream();
          //     byte[] b=new byte[1024*2];
          //     int i=0;
          //     while((i=fis.read(b,0,b.length))>0){
          //      //當前線程等待0.1
          //      //Thread.sleep(100);
          //      os.write(b,0,i);
          //     }
          //     
          //    }
          //   } catch(Exception e) {
          //    e.printStackTrace();
          //    System.out.println("用戶取消操作");
          //   } finally {
          //    try {
          //     if(fis != null) {
          //      fis.close();
          //     }
          //     if(os != null) {
          //      os.close();
          //     }
          //     if(file != null) {
          //      file.delete();
          //     }
          //    } catch(Exception e) {
          //     
          //    }
          //    
          //   }
            } catch (Exception e) {
             e.printStackTrace();
            }
           }
           
           public static void getExcelToImg(ExcelObj obj,HttpServletRequest request,HttpServletResponse response) throws Exception {
            if(obj == null) {
             return;
            }
            
            String excelName = obj.getExcelName();//excel名稱
            String sheetName = obj.getSheetName();//sheet名稱
            
            
            String imgList1 = obj.getImg1();//圖片
            
            
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet(sheetName);
            
            
            
            BufferedImage bufferImg =null;
            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1,1,(short) 2,2,(short)12,30);
            bufferImg = ImageIO.read(new File(imgList1));
            ImageIO.write(bufferImg,"jpeg",byteArrayOut);
            patriarch.createPicture(anchor , workbook.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
            
            
            excelName = InitServlet.sysPath+"exceldown\\"+ excelName+".xls";
            

            FileOutputStream fileOut = new FileOutputStream(excelName);
            workbook.write(fileOut);
            
            if(fileOut != null) {
             fileOut.close();
            }
            //文件下載
            HttpSession session = request.getSession();
            String outputFile = excelName;
            session.setAttribute("outputFile", outputFile);
            
             File file=new File(imgList1);
             file.delete();
           }
           
           
           
           public static void getExcelToImgAfter(ExcelObj obj) throws Exception {
            if(obj == null) {
             return;
            }
            
            String excelName = obj.getExcelName();//excel名稱
            String sheetName = obj.getSheetName();//sheet名稱
            
            
            String imgList1 = obj.getImg1();//圖片
            
            
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet(sheetName);
            
            
            
            BufferedImage bufferImg =null;
            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1,1,(short) 2,2,(short)12,30);
            bufferImg = ImageIO.read(new File(imgList1));
            ImageIO.write(bufferImg,"jpeg",byteArrayOut);
            patriarch.createPicture(anchor , workbook.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));

            excelName = InitServlet.sysPath+"exceldown\\"+ excelName+".xls";
            
               
            FileOutputStream fileOut = new FileOutputStream(excelName);
            workbook.write(fileOut);

            if(fileOut != null) {
             fileOut.close();
            }
            
              File file=new File(InitServlet.sysPath+"JfreeImg\\"+excelName+".jpeg");
              file.delete();
           }
          }


          InitServlet----初始化類

          package com.san.report;

          import java.io.IOException;
          import java.util.List;

          import javax.servlet.Servlet;
          import javax.servlet.ServletConfig;
          import javax.servlet.ServletException;
          import javax.servlet.ServletRequest;
          import javax.servlet.ServletResponse;

          import org.springframework.context.ApplicationContext;
          import org.springframework.web.context.support.WebApplicationContextUtils;

           

          /**
           * 初始化ApplicationContext
           * @author shmilyLT
           *
           */
          public class InitServlet implements Servlet{
           public static ApplicationContext ctx;
           public static String sysPath;
           
           public void destroy() {
            
           }

           public ServletConfig getServletConfig() {
            return null;
           }

           public String getServletInfo() {
            return null;
           }

           public void init(ServletConfig servlet) throws ServletException {
            
            sysPath = servlet.getServletContext().getRealPath("/");
            
            System.out.println("初始化系統路徑成功,當前系統路徑為" + sysPath);
            
           }

           public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {

           }
          }

          PalntInIt --繪圖類

          package com.san.report;

          import java.awt.Color;
          import java.awt.Font;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.text.SimpleDateFormat;
          import java.util.Calendar;
          import java.util.Date;
          import java.util.List;

          import javax.servlet.http.HttpServletRequest;


          import org.jfree.chart.ChartFactory;
          import org.jfree.chart.ChartRenderingInfo;
          import org.jfree.chart.ChartUtilities;
          import org.jfree.chart.JFreeChart;
          import org.jfree.chart.axis.NumberAxis;
          import org.jfree.chart.entity.StandardEntityCollection;
          import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
          import org.jfree.chart.plot.CategoryPlot;
          import org.jfree.chart.plot.PiePlot;
          import org.jfree.chart.plot.PlotOrientation;
          import org.jfree.chart.renderer.category.BarRenderer;
          import org.jfree.chart.renderer.category.LineAndShapeRenderer;
          import org.jfree.chart.servlet.ServletUtilities;
          import org.jfree.chart.title.TextTitle;
          import org.jfree.data.category.DefaultCategoryDataset;
          import org.jfree.data.general.DefaultPieDataset;
          import org.jfree.ui.HorizontalAlignment;
          import org.jfree.ui.RectangleEdge;

          public class PlantInit {
           /*********
            *
            * @author wuwh
            * vlineName代表橫坐標標志;
            * hlineName代表縱坐標標志名稱;
            *
            * 曲線圖的繪制;
            * **********/
          public static String excelName;

          public static String  plantCurvesLine(List list,String topName,String vlineName,String hlineName,HttpServletRequest request){
           
           DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
           
            FileOutputStream fos_jpg = null;
           //設置值;
           int cont=list.size();
           for(int i=0;i<cont;i++){
            PlantTo to=(PlantTo) list.get(i);
            defaultcategorydataset.addValue(to.getMapValue(), to.getMapname(), to.getMapkey());
           }
           //繪圖到內存
           JFreeChart jfreechart1 = ChartFactory.createLineChart(
             topName, vlineName, hlineName,
             defaultcategorydataset, PlotOrientation.VERTICAL, true, true,
             true);
           //頭信息的設置;
           TextTitle texttitle = new TextTitle(topName);
           texttitle.setFont(new Font("SansSerif", 0, 10));
           texttitle.setPosition(RectangleEdge.BOTTOM);
           texttitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);
           jfreechart1.addSubtitle(texttitle);
           jfreechart1.setBackgroundPaint(Color.decode("#FFCC00"));

           
           CategoryPlot categoryplot = (CategoryPlot) jfreechart1.getPlot();
           //設置縱橫條的顏色;
           categoryplot.setBackgroundPaint(Color.decode("#996600"));
           categoryplot.setRangeGridlinePaint(Color.white);
           categoryplot.setDomainGridlinePaint(Color.white);
           categoryplot.setDomainGridlinesVisible(true);//顯示縱線
           NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
           numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

           LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
             .getRenderer();
           
           lineandshaperenderer.setShapesVisible(true);
           lineandshaperenderer.setDrawOutlines(true);
           lineandshaperenderer.setUseFillPaint(true);
           //設置兩條曲線的不同顏色;
           //jfreechart1.getSubtitleCount()----表示有多少條曲線;
           lineandshaperenderer.setSeriesPaint(0, Color.RED);
           lineandshaperenderer.setSeriesPaint(1, Color.BLUE);
           lineandshaperenderer.setSeriesFillPaint(0, Color.RED);
           lineandshaperenderer.setSeriesFillPaint(1, Color.BLUE);
           
           if(null!=request.getAttribute("incont") && ""!=request.getAttribute("incont")){
            lineandshaperenderer.setSeriesFillPaint(2, Color.YELLOW);
            lineandshaperenderer.setSeriesFillPaint(2, Color.YELLOW); 
           }

           lineandshaperenderer.setBaseLinesVisible(true);
           
           
           String imageFile_qx=null;
           try {
            //向內存中繪制出真實的曲線圖并且保存在session當中;
            imageFile_qx = ServletUtilities.saveChartAsJPEG(jfreechart1,600,450,request.getSession());//設置生成圖片,包括圖片的大小,長度是500,寬是400       
           } catch (IOException e) {
            e.printStackTrace();
           }
           
           
           String fullpath = InitServlet.sysPath+"JfreeImg\\"+imageFile_qx;
           
           try {
            fos_jpg = new FileOutputStream(fullpath);
            ChartUtilities.writeChartAsJPEG(fos_jpg,1.0f,jfreechart1,600,450,null);
           } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           }
           return imageFile_qx;//圖片路徑;
            }

           


             /******************
              *
              *
              * @author wuwh
              * 2D餅圖的顯示;
              *
              *
              * ***********************/

             public static String plant2DLine(List list,HttpServletRequest request,String topname){

              DefaultPieDataset dfp=new DefaultPieDataset();//數據池;
              FileOutputStream fos_jpg = null;
                 //向數據池中設置一堆數據,將其填充;
              int cont=list.size();
              for(int i=0;i<cont;i++){
             PlantTo to=(PlantTo) list.get(i);
             dfp.setValue(to.getMapkey(), to.getMapValue());
              }
                
                 JFreeChart a =ChartFactory.createPieChart(topname,dfp, true, true, true);
                //設置圖形上面的字體,顏色,變化。。。
                 PiePlot  pie=(PiePlot )a.getPlot();
                 pie.setLabelFont(new Font("SansSerif",Font.BOLD,12));
                 pie.setNoDataMessage("No data available");
                 pie.setCircular(true);
                 pie.setLabelGap(0.01D);//間距
                 //將緩存中的jfreechart對象用frame的實例顯示給用戶;
                 //PrintWriter out = response.getWriter();
                 ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
                 String imgaeFileName_2d=null;
            try {
             imgaeFileName_2d = ServletUtilities.saveChartAsJPEG(a,350,300,info,request.getSession());//設置生成圖片,包括圖片的大小,長度是500,寬是400       
            } catch (IOException e) {
             e.printStackTrace();
            }
            
            String fullpath = InitServlet.sysPath+"JfreeImg\\"+imgaeFileName_2d;
            
            try {
             fos_jpg = new FileOutputStream(fullpath);
             ChartUtilities.writeChartAsJPEG(fos_jpg,1.0f,a,350,300,null);
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            
            
            request.setAttribute("image_name_2d", imgaeFileName_2d);//保存圖片的名稱;
              return imgaeFileName_2d;
             }
            
             /******************
              *
              *
              * @author wuwh
              * 柱狀圖的顯示;
              * vlineName代表橫坐標標志;
           * hlineName代表縱坐標標志名稱;
           *
              * ***********************/
            
             public static String planeReacgLine(List list,String topName,String vlineName,String hlineName,HttpServletRequest request){
           
              /*********柱狀圖的生成**********/
              int cont=list.size();
              DefaultCategoryDataset dataset = new DefaultCategoryDataset();
              for(int i=0;i<cont;i++){
               PlantTo to=(PlantTo) list.get(i);
               dataset.addValue(to.getMapValue(),i+"",to.getMapkey());//待補充。。。。。
              }
                 JFreeChart chart=ChartFactory.createBarChart3D(topName, hlineName,vlineName, dataset, PlotOrientation.HORIZONTAL, false, true, false);
                 CategoryPlot plot = chart.getCategoryPlot();
              final BarRenderer renderer1 = (BarRenderer) plot.getRenderer();
              renderer1.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
              renderer1.setItemMargin(0.01D);// 設置每個柱子之間的距離
              plot.setRenderer(renderer1);
             
              String imgaeFileName_RE=null;
            try {
             imgaeFileName_RE = ServletUtilities.saveChartAsJPEG(chart,600,400,request.getSession());//設置生成圖片,包括圖片的大小,長度是500,寬是400       
            } catch (IOException e) {
             e.printStackTrace();
            }
            request.setAttribute("image_name_RE", imgaeFileName_RE);//保存圖片的名稱;
                  return imgaeFileName_RE;
             }
            
            
            
             public static String  plantCurvesLineAfter(List list,String topName,String vlineName,String hlineName,String contName,String imgName){
            
            DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
            
             FileOutputStream fos_jpg = null;
            //設置值;
            int cont=list.size();
            for(int i=0;i<cont;i++){
             PlantTo to=(PlantTo) list.get(i);
             defaultcategorydataset.addValue(to.getMapValue(), to.getMapname(), to.getMapkey());
            }
            //繪圖到內存
            JFreeChart jfreechart1 = ChartFactory.createLineChart(
              topName, vlineName, hlineName,
              defaultcategorydataset, PlotOrientation.VERTICAL, true, true,
              true);
            //頭信息的設置;
            TextTitle texttitle = new TextTitle(topName);
            texttitle.setFont(new Font("SansSerif", 0, 10));
            texttitle.setPosition(RectangleEdge.BOTTOM);
            texttitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);
            jfreechart1.addSubtitle(texttitle);
            jfreechart1.setBackgroundPaint(Color.decode("#FFCC00"));

            
            CategoryPlot categoryplot = (CategoryPlot) jfreechart1.getPlot();
            //設置縱橫條的顏色;
            categoryplot.setBackgroundPaint(Color.decode("#996600"));
            categoryplot.setRangeGridlinePaint(Color.white);
            categoryplot.setDomainGridlinePaint(Color.white);
            categoryplot.setDomainGridlinesVisible(true);//顯示縱線
            NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
            numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

            LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
              .getRenderer();
            
            lineandshaperenderer.setShapesVisible(true);
            lineandshaperenderer.setDrawOutlines(true);
            lineandshaperenderer.setUseFillPaint(true);
            //設置兩條曲線的不同顏色;
            //jfreechart1.getSubtitleCount()----表示有多少條曲線;
            lineandshaperenderer.setSeriesPaint(0, Color.RED);
            lineandshaperenderer.setSeriesPaint(1, Color.BLUE);
            lineandshaperenderer.setSeriesFillPaint(0, Color.RED);
            lineandshaperenderer.setSeriesFillPaint(1, Color.BLUE);
            
            if(null !=contName && !"".equals(contName)){
             lineandshaperenderer.setSeriesPaint(2, Color.yellow);
             lineandshaperenderer.setSeriesFillPaint(2, Color.yellow);
            }

            String imageFile_qx=null;
            lineandshaperenderer.setBaseLinesVisible(true);
            Calendar  calendar=Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
            try {
             Date date=df.parse(calendar.getTime().toLocaleString());
             imageFile_qx=imgName+date.toLocaleString().split(" ")[0]+date.toLocaleString().split(" ")[1].split(":")[0]+""+date.toLocaleString().split(" ")[1].split(":")[1]+".jpeg";
             excelName=imageFile_qx.split(".jpeg")[0];
            } catch (Exception e1) {
             e1.printStackTrace();
            }
            
            
          //  try {
          //   //向內存中繪制出真實的曲線圖并且保存在session當中;
          //   imageFile_qx = ServletUtilities.saveChartAsJPEG(jfreechart1,600,450,request.getSession());//設置生成圖片,包括圖片的大小,長度是500,寬是400       
          //  } catch (IOException e) {
          //   e.printStackTrace();
          //  }
            String fullpath = InitServlet.sysPath+"JfreeImg\\"+imageFile_qx;
            
            try {
             fos_jpg = new FileOutputStream(fullpath);
             ChartUtilities.writeChartAsJPEG(fos_jpg,1.0f,jfreechart1,600,450,null);
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            return fullpath;//圖片路徑;
             }


          }



          ReportDao--dao類

          package com.san.report;

          import java.text.ParseException;
          import java.text.SimpleDateFormat;
          import java.util.ArrayList;
          import java.util.Calendar;
          import java.util.Date;
          import java.util.List;

          import org.hibernate.HibernateException;
          import org.hibernate.Session;

          import com.san.pwms.dao.Getdao;
          import com.san.tools.hibernate.config.HibernateConfig;
          import com.san.tools.hibernate.session.SessionManager2;

          public class ReportDao {

           private SessionManager2 manager;
           private static final String partid="OuLi20050823145928841";
           
           public SessionManager2 getManager() {
            return manager;
           }

           public void setManager(SessionManager2 manager) {
            this.manager = manager;
           }
           
           public Session getSession(){
             Session session=manager.getSession(HibernateConfig.getConfigFile());
             return session;
           }
           
           /************
            * @author wuwh
            * 按周統計圖;
            * sarttime 代表開始時間;
            * endtime 代表結束時間;
            *
            * *************/
           
           public  List getWeekList(String sarttime,String endtime ,String party){
            Calendar  calendar=Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Session session=this.getSession();//獲得session;
            List sumList=new ArrayList();
            if(null==party || "".equals(party)){
             party=partid;
            }
            StringBuffer strsql=new StringBuffer();
            StringBuffer strsql1=new StringBuffer();
            StringBuffer strsql2=new StringBuffer();
            
           try {
            calendar.setTime(df.parse(sarttime));
            String midtime=null;
            for(int i=0;i<7;i++){
              calendar.setTime(df.parse(sarttime));
              calendar.add(Calendar.DATE,i);
              calendar.setTime(df.parse(sarttime));
              String timenow=calendar.getTime().toLocaleString();
              calendar.add(Calendar.DATE,i+1);
              String timeend=calendar.getTime().toLocaleString();
            
              if(i==0){
                strsql.append("select  'contsum'= case when sum(virusNum) is null then '0'  else sum(virusNum) end ,  '星期"+(i+1)+"' as stattime from HostVirus_Statistic  where StartTime>='"+timenow+"' and endtime<'"+timeend+"' and DeptId='"+party+"'  ");
                strsql1.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end ,   '星期"+(i+1)+"' as stattime from ObjectiveIP_IDS_Statistic  where StartTime>='"+timenow+"' and endtime<'"+timeend+"' and DeptId='"+party+"' ");
                strsql2.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end ,   '星期"+(i+1)+"' as stattime from SourceIP_IDS_Statistic  where StartTime>='"+timenow+"' and endtime<'"+timeend+"' and DeptId='"+party+"' ");
             
                 }else{
                   strsql.append("select  'contsum'= case when sum(virusNum) is null then '0'  else sum(virusNum) end ,  '星期"+(i+1)+"' as stattime from HostVirus_Statistic  where StartTime>='"+midtime+"' and endtime<'"+timeend+"' and DeptId='"+party+"'  ");
                strsql1.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end ,   '星期"+(i+1)+"' as stattime from ObjectiveIP_IDS_Statistic  where StartTime>='"+midtime+"' and endtime<'"+timeend+"' and DeptId='"+party+"' ");
                strsql2.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end ,   '星期"+(i+1)+"' as stattime from SourceIP_IDS_Statistic  where StartTime>='"+midtime+"' and endtime<'"+timeend+"' and DeptId='"+party+"' ");
              }
                 if(i!=6){
                   strsql.append(" union all "); 
                strsql1.append(" union all ");
                strsql2.append(" union all ");
                 }
                
                 midtime=timeend;
            }
            } catch (ParseException e) {
             e.printStackTrace();
            }
             
            
             
            //總體病毒統計;
            List listsql =session.createSQLQuery(strsql.toString()).list();
            //目的地址IDS病毒統計;
            List listsql1=session.createSQLQuery(strsql1.toString()).list();
            //源地址IDS病毒統計;
            List listsql2=session.createSQLQuery(strsql2.toString()).list();
            Getdao.closeSession(session);
            
            if(null!=listsql && !listsql.isEmpty()){
             int contstr=listsql.size();
             for(int i=0;i<contstr;i++){
              Object [] ob=(Object[]) listsql.get(i);
              PlantTo to=new PlantTo();
              to.setMapValue(Integer.parseInt(ob[0].toString()));//病毒數量
              to.setMapname("病毒趨勢圖");
              to.setMapkey(ob[1].toString());//監控時間;/*ob[1].toString()*/
              sumList.add(to);
             }
            }
            //目的地址IDS病毒監控;
            if(null !=listsql1 && !listsql1.isEmpty()){
             int contstr=listsql1.size();
             for(int i=0;i<contstr;i++){
              Object [] ob=(Object[]) listsql1.get(i);
              PlantTo to=new PlantTo();
              to.setMapValue(Integer.parseInt(ob[0].toString()));//IDS數量
              to.setMapname("目的地址IDS趨勢圖");
              to.setMapkey(ob[1].toString());//監控時間;/*ob[1].toString()*/
              sumList.add(to);
             }
            }
            
            //源地址IDS病毒監控;
            if(null !=listsql2 && !listsql2.isEmpty()){
             int contstr=listsql2.size();
             for(int i=0;i<contstr;i++){
              Object [] ob=(Object[]) listsql2.get(i);
              PlantTo to=new PlantTo();
              to.setMapValue(Integer.parseInt(ob[0].toString()));//IDS數量
              to.setMapname("源地址IDS趨勢圖");
              to.setMapkey(ob[1].toString());//監控時間;/*ob[1].toString()*/
              sumList.add(to);
             }
            }
            
                  return sumList;
           }
           
           
           /************
            * @author wuwh
            * 按周統計圖;
            * sarttime 代表開始時間;
            * endtime 代表結束時間;
            *
            * *************/
           
           public  List getWeekList(String sarttime,String endtime ){
            Calendar  calendar=Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Session session=this.getSession();//獲得session;
            List sumList=new ArrayList();
            List list=null;

            StringBuffer strsql=new StringBuffer();
            StringBuffer strsql1=new StringBuffer();
            
           try {
            calendar.setTime(df.parse(sarttime));
            String midtime=null;
            for(int i=0;i<7;i++){
              calendar.setTime(df.parse(sarttime));
              calendar.add(Calendar.DATE,i);
              calendar.setTime(df.parse(sarttime));
              String timenow=calendar.getTime().toLocaleString();
              calendar.add(Calendar.DATE,i+1);
              String timeend=calendar.getTime().toLocaleString();
            
              if(i==0){
                 strsql.append("select  'contsum'= case when sum(virusNum) is null then '0'  else sum(virusNum) end ,  '星期"+(i+1)+"' as stattime from Virus_Statistic  where StartTime>='"+timenow+"' and endtime<'"+timeend+"' ");
                 strsql1.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end ,   '星期"+(i+1)+"' as stattime from IDS_Statistic  where StartTime>='"+timenow+"' and endtime<'"+timeend+"' ");
              }else{
               strsql.append("select  'contsum'= case when sum(virusNum) is null then '0'  else sum(virusNum) end ,  '星期"+(i+1)+"' as stattime from Virus_Statistic  where StartTime>='"+midtime+"' and endtime<'"+timeend+"' ");
                  strsql1.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end ,   '星期"+(i+1)+"' as stattime from IDS_Statistic  where StartTime>='"+midtime+"' and endtime<'"+timeend+"' ");
              }
                 if(i!=6){
                   strsql.append(" union all "); 
                strsql1.append(" union all "); 
                 }
                
                 midtime=timeend;
            }
            } catch (ParseException e) {
             e.printStackTrace();
            }
            //總體病毒統計;
             List listsql =session.createSQLQuery(strsql.toString()).list();
             //IDS病毒統計;
             List listsql1=session.createSQLQuery(strsql1.toString()).list();
             this.closeSession(session);
             
             if(null!=listsql && !listsql.isEmpty()){
              int contstr=listsql.size();
              for(int i=0;i<contstr;i++){
               Object [] ob=(Object[]) listsql.get(i);
               PlantTo to=new PlantTo();
               to.setMapValue(Integer.parseInt(ob[0].toString()));//病毒數量
               to.setMapname("病毒趨勢圖");
               to.setMapkey(ob[1].toString());//監控時間;/*ob[1].toString()*/
               sumList.add(to);
              }
             }
             //IDS病毒監控;
             if(null !=listsql1 && !listsql1.isEmpty()){
              int contstr=listsql1.size();
              for(int i=0;i<contstr;i++){
               Object [] ob=(Object[]) listsql1.get(i);
               PlantTo to=new PlantTo();
               to.setMapValue(Integer.parseInt(ob[0].toString()));//IDS數量
               to.setMapname("IDS趨勢圖");
               to.setMapkey(ob[1].toString());//監控時間;/*ob[1].toString()*/
               sumList.add(to);
              }
             }
                  return sumList;
           }
           
           
           
           /****************
            * @author wuwh
            * 月統計病毒獲取;
            *
            *
            * ***************/
           
           
           public List getMonthList(String sarttime,String endtime ,String party){
            
            if(null==party || "".equals(party)){
             party=partid;
            }
            
            Session session=this.getSession();//獲得session;
            List sumList=new ArrayList();
            StringBuffer hsq=new StringBuffer("select  sum(virusNum) as contsum ,Convert(varchar(10),StartTime,20) as datetimes  from HostVirus_Statistic where 1=1 ");///總體病毒統計;
            hsq.append(" and StartTime>='"+sarttime+"' and endtime<='"+endtime+"' and DeptId='"+party+"' ");
            hsq.append(" group by Convert(varchar(10),StartTime,20)");
            
            StringBuffer hsq1=new StringBuffer("select  sum(IDSNum) as contsum ,Convert(varchar(10),StartTime,20) as datetimes  from ObjectiveIP_IDS_Statistic where 1=1 ");//目的地址IDS病毒統計;
            hsq1.append(" and StartTime>='"+sarttime+"' and endtime<='"+endtime+"' and DeptId='"+party+"' ");
            hsq1.append(" group by Convert(varchar(10),StartTime,20)");

            StringBuffer hsq2=new StringBuffer("select  sum(IDSNum) as contsum ,Convert(varchar(10),StartTime,20) as datetimes  from SourceIP_IDS_Statistic where 1=1 ");//目的地址IDS病毒統計;
            hsq2.append(" and StartTime>='"+sarttime+"' and endtime<='"+endtime+"' and DeptId='"+party+"' ");
            hsq2.append(" group by Convert(varchar(10),StartTime,20)");

            //總體病毒統計;
            List list =session.createSQLQuery(hsq.toString()).list();
            //目的地址IDS病毒統計;
            List list1=session.createSQLQuery(hsq1.toString()).list();
            //源地址IDS病毒統計;
            List list2=session.createSQLQuery(hsq2.toString()).list();
            
            Getdao.closeSession(session);
            if(null!=list && !list.isEmpty()){
             int cont=list.size();
             for(int i=0;i<cont;i++){
              Object [] ob=(Object[]) list.get(i);
              PlantTo to=new PlantTo();
              to.setMapValue(Integer.parseInt(ob[0].toString()));//病毒數量
              to.setMapname("病毒趨勢圖");
              to.setMapkey(ob[1].toString());//監控時間;
              sumList.add(to);
             }
            }
            //目的IDS病毒監控;
            if(null !=list1 && !list1.isEmpty()){
             int cont=list1.size();
             for(int i=0;i<cont;i++){
              Object [] ob=(Object[]) list1.get(i);
              PlantTo to=new PlantTo();
              to.setMapValue(Integer.parseInt(ob[0].toString()));//IDS數量
              to.setMapname("目的地址IDS趨勢圖");
              to.setMapkey(ob[1].toString());//監控時間;
              sumList.add(to);
             }
            }
            
            //源IDS病毒監控;
            if(null !=list2 && !list2.isEmpty()){
             int cont=list2.size();
             for(int i=0;i<cont;i++){
              Object [] ob=(Object[]) list2.get(i);
              PlantTo to=new PlantTo();
              to.setMapValue(Integer.parseInt(ob[0].toString()));//IDS數量
              to.setMapname("源地址IDS趨勢圖");
              to.setMapkey(ob[1].toString());//監控時間;
              sumList.add(to);
             }
            }
            return sumList;
           }
            
            
           
           /****************
            * @author wuwh
            * 月統計病毒獲取;
            *
            *
            * ***************/
           
           public  List getMonthList(String sarttime,String endtime ){
            Session session=this.getSession();//獲得session;
            List sumList=new ArrayList();
            StringBuffer hsq=new StringBuffer("select  sum(virusNum) as contsum ,Convert(varchar(10),StartTime,20) as datetimes  from Virus_Statistic where 1=1 ");///總體病毒統計;
            hsq.append(" and StartTime>='"+sarttime+"' and endtime<='"+endtime+"' ");
            hsq.append(" group by Convert(varchar(10),StartTime,20)");
            
            StringBuffer hsq1=new StringBuffer("select  sum(IDSNum) as contsum ,Convert(varchar(10),StartTime,20) as datetimes  from IDS_Statistic where 1=1 ");//IDS病毒統計;
            hsq1.append(" and StartTime>='"+sarttime+"' and endtime<='"+endtime+"' ");
            hsq1.append(" group by Convert(varchar(10),StartTime,20)");

            //總體病毒統計;
            List list =session.createSQLQuery(hsq.toString()).list();
            //IDS病毒統計;
            List list1=session.createSQLQuery(hsq1.toString()).list();
            
            this.closeSession(session);
            if(null!=list && !list.isEmpty()){
             int cont=list.size();
             for(int i=0;i<cont;i++){
              Object [] ob=(Object[]) list.get(i);
              PlantTo to=new PlantTo();
              to.setMapValue(Integer.parseInt(ob[0].toString()));//病毒數量
              to.setMapname("病毒趨勢圖");
              to.setMapkey(ob[1].toString());//監控時間;
              sumList.add(to);
             }
            }
            //IDS病毒監控;
            if(null !=list1 && !list1.isEmpty()){
             int cont=list1.size();
             for(int i=0;i<cont;i++){
              Object [] ob=(Object[]) list1.get(i);
              PlantTo to=new PlantTo();
              to.setMapValue(Integer.parseInt(ob[0].toString()));//IDS數量
              to.setMapname("IDS趨勢圖");
              to.setMapkey(ob[1].toString());//監控時間;
              sumList.add(to);
             }
            }
             
            return sumList;
           }
           
           /****************
            * @author wuwh
            * 日統計病毒獲取;
            *
            *
            * ***************/
           
           public  List getDayList(String sarttime,String endtime ){
            Session session=this.getSession();//獲得session;
            Calendar  calendar=Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            StringBuffer strsql=new StringBuffer();
            StringBuffer strsql1=new StringBuffer();
            try{
            calendar.setTime(df.parse(sarttime));

            String midtime=null;
            for(int i=0;i<24;i++){
               calendar.setTime(df.parse(sarttime));
               calendar.add(Calendar.HOUR,i);
               calendar.setTime(df.parse(sarttime));
               String timenow=calendar.getTime().toLocaleString();
               calendar.add(Calendar.HOUR,i+1);
               String timeend=calendar.getTime().toLocaleString();
              
             if(i==0){
              strsql.append("select  'contsum'= case when sum(virusNum) is null then '0'  else sum(virusNum) end ,"+(i+1)+" as datetimes  from Virus_Statistic where 1=1 and StartTime>='"+timenow+"' and endtime<'"+timeend+"'");///總體病毒統計;;
              strsql1.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end ,"+(i+1)+" as datetimes  from IDS_Statistic where 1=1 and StartTime>='"+timenow+"' and endtime<'"+timeend+"'");///IDS病毒統計;
             }else{
              strsql.append("select  'contsum'= case when sum(virusNum) is null then '0'  else sum(virusNum) end ,"+(i+1)+" as datetimes  from Virus_Statistic where 1=1 and StartTime>='"+midtime+"' and endtime<'"+timeend+"'");///總體病毒統計;;
              strsql1.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end,"+(i+1)+" as datetimes  from IDS_Statistic where 1=1 and StartTime>='"+midtime+"' and endtime<'"+timeend+"'");///IDS病毒統計;
             }
             
             if(i!=23){
                    strsql.append(" union all "); 
                 strsql1.append(" union all ");
                  }
             
              midtime=timeend;
            }
            }catch(Exception ex){
             ex.printStackTrace();
             
            }
            List sumList=new ArrayList();
           
            //總體病毒統計;
            List list =session.createSQLQuery(strsql.toString()).list();
            //IDS病毒統計;
            List list1=session.createSQLQuery(strsql1.toString()).list();
            
            this.closeSession(session);
            if(null!=list && !list.isEmpty()){
             int cont=list.size();
             for(int i=0;i<cont;i++){
              Object [] ob=(Object[]) list.get(i);
              PlantTo to=new PlantTo();
               if(null==ob[0]){
                to.setMapValue(0);//病毒數量
                to.setMapname("病毒趨勢圖");
                to.setMapkey("");//監控時間;
               }else{
               to.setMapValue(Integer.parseInt(ob[0].toString()));//病毒數量
               to.setMapname("病毒趨勢圖");
               to.setMapkey(ob[1].toString());//監控時間;
               }
              sumList.add(to);
             }
            }
            //IDS病毒監控;
            if(null !=list1 && !list1.isEmpty()){
             int cont=list1.size();
             for(int i=0;i<cont;i++){
              Object [] ob=(Object[]) list1.get(i);
              PlantTo to=new PlantTo();
              if(null==ob[0]){
                to.setMapValue(0);//病毒數量
                to.setMapname("病毒趨勢圖");
                to.setMapkey("");//監控時間;
               }else{
               to.setMapValue(Integer.parseInt(ob[0].toString()));//IDS數量
               to.setMapname("IDS趨勢圖");
               to.setMapkey(ob[1].toString());//監控時間;
               }
              sumList.add(to);
             }
            }
             
            return sumList;
           }
           
           
           /****************
            * @author wuwh
            * 日統計病毒獲取;
            *
            *
            * ***************/
           public  List getDayList(String sarttime,String endtime ,String party){
            
            if(null==party || "".equals(party)){
             party=partid;
            }
            Session session=this.getSession();//獲得session;
            Calendar  calendar=Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            StringBuffer strsql=new StringBuffer();
            StringBuffer strsql1=new StringBuffer();
            StringBuffer strsql2=new StringBuffer();
            try{
            calendar.setTime(df.parse(sarttime));

            String midtime=null;
            for(int i=0;i<24;i++){
               calendar.setTime(df.parse(sarttime));
               calendar.add(Calendar.HOUR,i);
               calendar.setTime(df.parse(sarttime));
               String timenow=calendar.getTime().toLocaleString();
               calendar.add(Calendar.HOUR,i+1);
               String timeend=calendar.getTime().toLocaleString();
              
             if(i==0){
              strsql.append("select  'contsum'= case when sum(virusNum) is null then '0'  else sum(virusNum) end ,"+(i+1)+" as datetimes  from HostVirus_Statistic where 1=1 and StartTime>='"+timenow+"' and endtime<'"+timeend+"' and DeptId='"+party+"'");///總體病毒統計;
              strsql1.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end ,"+(i+1)+" as datetimes  from ObjectiveIP_IDS_Statistic where 1=1 and StartTime>='"+timenow+"' and endtime<'"+timeend+"' and DeptId='"+party+"'");//目的地址IDS病毒統計;
              strsql2.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end,"+(i+1)+" as datetimes  from SourceIP_IDS_Statistic where 1=1 and StartTime>='"+timenow+"' and endtime<'"+timeend+"' and DeptId='"+party+"'");//目的地址IDS病毒統計;
             }else{
              strsql.append("select  'contsum'= case when sum(virusNum) is null then '0'  else sum(virusNum) end ,"+(i+1)+" as datetimes  from HostVirus_Statistic where 1=1 and StartTime>='"+midtime+"' and endtime<'"+timeend+"' and DeptId='"+party+"'");///總體病毒統計;
              strsql1.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end ,"+(i+1)+" as datetimes  from ObjectiveIP_IDS_Statistic where 1=1 and StartTime>='"+midtime+"' and endtime<'"+timeend+"' and DeptId='"+party+"'");//目的地址IDS病毒統計;
              strsql2.append("select  'contsum'= case when sum(IDSNum) is null then '0'  else sum(IDSNum) end ,"+(i+1)+" as datetimes  from SourceIP_IDS_Statistic where 1=1 and StartTime>='"+midtime+"' and endtime<'"+timeend+"' and DeptId='"+party+"'");//目的地址IDS病毒統計;
             }
             
             if(i!=23){
                    strsql.append(" union all "); 
                 strsql1.append(" union all ");
                 strsql2.append(" union all ");
                  }
             
              midtime=timeend;
            }
            }catch(Exception ex){
             ex.printStackTrace();
             
            }
            List sumList=new ArrayList();
           
            //總體病毒統計;
            List list =session.createSQLQuery(strsql.toString()).list();
            //目的地址IDS病毒統計;
            List list1=session.createSQLQuery(strsql1.toString()).list();
            //源地址IDS病毒統計;
            List list2=session.createSQLQuery(strsql2.toString()).list();
            
            this.closeSession(session);
            if(null!=list && !list.isEmpty()){
             int cont=list.size();
             for(int i=0;i<cont;i++){
              Object [] ob=(Object[]) list.get(i);
              PlantTo to=new PlantTo();
              to.setMapValue(Integer.parseInt(ob[0].toString()));//病毒數量
              to.setMapname("病毒趨勢圖");
              to.setMapkey(ob[1].toString());//監控時間;
              sumList.add(to);
             }
            }
            
            
            //目的IDS病毒監控;
            if(null !=list1 && !list1.isEmpty()){
             int cont=list1.size();
             for(int i=0;i<cont;i++){
              Object [] ob=(Object[]) list1.get(i);
              PlantTo to=new PlantTo();
              to.setMapValue(Integer.parseInt(ob[0].toString()));//IDS數量
              to.setMapname("目的地址IDS趨勢圖");
              to.setMapkey(ob[1].toString());//監控時間;
              sumList.add(to);
             }
            }
             
            //源IDS病毒監控;
            if(null !=list2 && !list2.isEmpty()){
             int cont=list2.size();
             for(int i=0;i<cont;i++){
              Object [] ob=(Object[]) list2.get(i);
              PlantTo to=new PlantTo();
              to.setMapValue(Integer.parseInt(ob[0].toString()));//IDS數量
              to.setMapname("源地址IDS趨勢圖");
              to.setMapkey(ob[1].toString());//監控時間;
              sumList.add(to);
             }
            }
             
            return sumList;
           }
           
           
           public  void closeSession(Session s) throws HibernateException
             {
                 if (null != s)
                     s.close();
             }
           
          }


          RepostQuarz      ---業務類

          package com.san.report;

          import java.io.File;
          import java.text.ParseException;
          import java.text.SimpleDateFormat;
          import java.util.Calendar;
          import java.util.Date;
          import java.util.List;

          import javax.servlet.http.HttpServletRequest;

          import org.quartz.SchedulerException;
          import org.quartz.Trigger;
          import org.quartz.impl.StdSchedulerFactory;
          import org.quartz.Scheduler;
          import org.springframework.scheduling.quartz.CronTriggerBean;
          import org.springframework.web.servlet.HttpServletBean;

          import com.san.report.virusstat.struts.action.VirusStatisticAction;


          public class RepostQuarz {

           private ReportDao dao;
           private String [] getDateList(String dateType){
            Calendar  calendar=Calendar.getInstance();
            Date now=new Date();
            String nowtime=now.toLocaleString();
            String time []=new String[2];
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            try {
             //eg 17號
             now=df.parse(nowtime);
             if("day".equals(dateType)){
              calendar.add(Calendar.DATE, -1);
              calendar.setTime(calendar.getTime());
              time[1]=calendar.getTime().toLocaleString().split(" ")[0]+" 0:00:00";//則為16號
              calendar.add(Calendar.DATE, -1);
              time[0]=calendar.getTime().toLocaleString().split(" ")[0]+" 0:00:00";//則為15號;
             }else if("week".equals(dateType)){
               calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
               calendar.setTime(calendar.getTime());
               calendar.add(Calendar.DATE, -1);
               calendar.setTime(calendar.getTime());
               time[1]=calendar.getTime().toLocaleString().split(" ")[0]+" 0:00:00";
               calendar.add(Calendar.DATE, -6);
               time[0]=calendar.getTime().toLocaleString().split(" ")[0]+" 0:00:00";//則為15號;

             }else{
              calendar.add(Calendar.MONTH, -1);
              calendar.setTime(calendar.getTime());
              calendar.set(Calendar.DAY_OF_MONTH,1);//設置為1號,當前日期既為本月第一天
              time[0]=calendar.getTime().toLocaleString().split(" ")[0]+" 0:00:00";
                 calendar.add(Calendar.MONTH,1);//月增加1天
                 calendar.add(Calendar.DAY_OF_MONTH,-1);//日期倒數一日,既得到本月最后一天
                 time[1]=calendar.getTime().toLocaleString().split(" ")[0]+" 0:00:00";
             }
             
            } catch (ParseException e) {
             e.printStackTrace();
            }
            return time;
           }
           
           
           /***************
            *
            *
            * 按每日的一個固定時間導出數據;
            *
            *
            * ***********************/
           public void QuarzprintDay(){
            String [] time=this.getDateList("day");
            List sumList=dao.getDayList(time[0], time[1]);
            List sumList1=dao.getDayList(time[0], time[1],"");
            String img_url=PlantInit.plantCurvesLineAfter(sumList, "病毒、IDS趨勢圖","時間", "安全事件指數", "","病毒IDS(日)");
            ExcelObj obj=new ExcelObj();
            obj.setImg1(img_url);
            obj.setSheetName("IDS、病毒綜合曲線圖");
            obj.setExcelName(PlantInit.excelName);
            
            String img_url1=PlantInit.plantCurvesLineAfter(sumList1, "部門病毒、IDS趨勢圖","時間", "安全事件指數", "cont","部門病毒IDS(日)");
            ExcelObj obj1=new ExcelObj();
            obj1.setImg1(img_url1);
            obj1.setSheetName("部門IDS、病毒綜合曲線圖");
            obj1.setExcelName(PlantInit.excelName);
            try {
             DownloadExcel.getExcelToImgAfter(obj);
             DownloadExcel.getExcelToImgAfter(obj1);
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            System.out.println("按每日統計。。。。。。。。。。。。。。");
           }

           
           
           /***************
            *
            *
            * 按每周的一個固定時間導出數據;
            *
            *
            * ***********************/
           public void QuarzprintWeek(){
            String [] time=this.getDateList("week");
            List sumList=dao.getWeekList(time[0], time[1]);
            List sumList1=dao.getWeekList(time[0], time[1],"");
            String img_url=PlantInit.plantCurvesLineAfter(sumList, "病毒、IDS趨勢圖","時間", "安全事件指數", "","病毒IDS(周)");
            ExcelObj obj=new ExcelObj();
            obj.setImg1(img_url);
            obj.setSheetName("IDS、病毒綜合曲線圖(周)");
            obj.setExcelName(PlantInit.excelName);
            
            String img_url1=PlantInit.plantCurvesLineAfter(sumList1, "部門病毒、IDS趨勢圖","時間", "安全事件指數", "cont","部門病毒IDS(周)");
            ExcelObj obj1=new ExcelObj();
            obj1.setImg1(img_url1);
            obj1.setSheetName("部門IDS、病毒綜合曲線圖(周)");
            obj1.setExcelName(PlantInit.excelName);
            try {
             DownloadExcel.getExcelToImgAfter(obj);
             DownloadExcel.getExcelToImgAfter(obj1);
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            System.out.println("按每周統計。。。。。。。。。。。。。。");
           }
           
           /***************
            *
            *
            * 按每月的一個固定時間導出數據;
            *
            *
            * ***********************/
           public void QuarzprintMonth(){
            String [] time=this.getDateList("month");
            List sumList=dao.getMonthList(time[0], time[1]);
            List sumList1=dao.getMonthList(time[0], time[1],"");
            String img_url=PlantInit.plantCurvesLineAfter(sumList, "病毒、IDS趨勢圖","時間", "安全事件指數", "","病毒IDS(月)");
            ExcelObj obj=new ExcelObj();
            obj.setImg1(img_url);
            obj.setSheetName("IDS、病毒綜合曲線圖(月)");
            obj.setExcelName(PlantInit.excelName);
            
            String img_url1=PlantInit.plantCurvesLineAfter(sumList1, "部門病毒、IDS趨勢圖","時間", "安全事件指數", "cont","部門病毒IDS(月)");
            ExcelObj obj1=new ExcelObj();
            obj1.setImg1(img_url1);
            obj1.setSheetName("部門IDS、病毒綜合曲線圖(月)");
            obj1.setExcelName(PlantInit.excelName);
            try {
             DownloadExcel.getExcelToImgAfter(obj);
             DownloadExcel.getExcelToImgAfter(obj1);
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            System.out.println("按每月統計。。。。。。。。。。。。。。");
           }


           public ReportDao getDao() {
            return dao;
           }


           public void setDao(ReportDao dao) {
            this.dao = dao;
           }
           
           
           public static void main(String args []){
            File file=new File("D:\\Program Files\\jboss3.2.5\\server\\default\\deploy\\web.war\\exceldown");
            String [] str=file.list();
            for(int i=0;i<str.length;i++){
             String strname=str[i];
             String st []=strname.split("-");
             String time=st[0].substring(st[0].length()-4)+"-"+st[1]+"-"+st[2].substring(0,2);
             if(time.equals("2007-12-17")){
              System.out.println(strname);
             }
             
            }
            
            //file.delete();
            
            
           }


          }


          posted on 2007-12-18 18:02 小虎虎 閱讀(430) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 大石桥市| 鄂尔多斯市| 宁武县| 元阳县| 营口市| 横山县| 建瓯市| 镇远县| 正镶白旗| 宜川县| 巴楚县| 体育| 和静县| 兴山县| 平江县| 汤阴县| 嘉荫县| 海宁市| 南投市| 阿克陶县| 琼结县| 息烽县| 安阳县| 运城市| 武山县| 博白县| 陵川县| 四川省| 明水县| 宁强县| 卓资县| 香港 | 安仁县| 电白县| 绥化市| 磴口县| 玉溪市| 手游| 屯门区| 延吉市| 汉源县|