package com.report;
import java.awt.Dimension;

import javax.swing.JDialog;
import javax.swing.JScrollPane;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
import com.coolservlets.beans.method.*;

/*
* Created on 24/07/2003
*
* To change the template for this generated file go to
* Window&Preferences&Java&Code Generation&Code and Comments
*/

/**
* @author kenta
*
* To change the template for this generated type comment go to
* Window&Preferences&Java&Code Generation&Code and Comments
*/
public class HtmlReport extends JDialog {
    //每页行数
    private int aPageLineCount;
    //需要动态设置的元素：key:替换成数据的标记；value:为Vector;
    private Properties clusters = new Properties();
    //报表表头
    private String  reportTitle;
    //报表种类category :"CARD" 卡片式；"REPORT" 记录格式
    private String category;
    //卡片式报表数据
    private String[] carddata;
    //记录格式报表数据
    private String[][] reportdata;
    //替换成数据的标记
    private String  covertag;
    //报表模板文件名
    private String modulefln;
    private Vector HtmlModule;
    //外部文件名
    //private String printflm;
    //全局表字符串，用来保存上次报表的字符串
    private String htmlreport = "";

    public HtmlReport() {
    	
        super();
    }
    public String createHtmlReport() throws Exception{
      //规定报表模板必须放在maximo 的resources\defaults
      //String sReportpath = loadConfigValue("com/config/report.cfg","reportpath");
      //String modflm   = sReportpath+ System.getProperties().getProperty("file.separator")+modulefln;
      //printflm = sReportpath + System.getProperties().getProperty("file.separator")+ modulefln.substring(0,modulefln.indexOf(".",0))+"print.htm";
      //Vector HtmlModule = com.coolservlets.beans.method.genMethod.readFileRecord(modflm);
      Vector tableBefore  = new Vector();
      Vector tableCont    = new Vector();
      //表内容，但没有换行
      Vector tableContNoSp    = new Vector();
      Vector tableAfter   = new Vector();
      boolean IsFirstTab =true;
      int lp = HtmlModule.size();
      int firstTabNo = 0;
      int lastTabNo  = 0;
      //表的公共部分填写完成Begin
      //找出替换的表内容
      for(int i=0;i<lp;i++){
        String s = (String)HtmlModule.elementAt(i);
        if(s.indexOf("<table",0) > -1 && IsFirstTab){
           firstTabNo = i;
           IsFirstTab = false;
        }
        if(s.indexOf("</table>",0) > -1){
           lastTabNo = i;
        }
      }
      //将模板文件分为三部分
      for(int i=0;i<lp;i++){
        String s = (String)HtmlModule.elementAt(i);
        if(i < firstTabNo){
          tableBefore.addElement(new String(s));
        }else{
          if(i >=firstTabNo && i <= lastTabNo){
            tableCont.addElement(new String(s));
          }else{
            tableAfter.addElement(new String(s));
          }
        }
      }//for end
      String tabRows = "";
      int ulp = tableCont.size();
      for(int u=0;u<ulp;u++){
        String s= (String)tableCont.elementAt(u);
        tabRows = tabRows + s;
      }
      //替换表前端内容
      Enumeration  ee = clusters.propertyNames();
      while(ee.hasMoreElements()){
        //替换的符号
        String keyList = (String)ee.nextElement();
        //替换的值
        Vector cluList = (Vector)clusters.get(keyList);
        int plp = cluList.size();
        String[] rplValue = new String[plp];
        for(int j=0;j<plp;j++){
          String s = (String)cluList.elementAt(j);
          rplValue[j] = s;
        }
        tableBefore = rplValueStr(tableBefore,rplValue,keyList);
      }
      //已经替换过的表内容
      Vector tabLine = tableCont;
      Enumeration  e = clusters.propertyNames();
      while(e.hasMoreElements()){
        //替换的符号
        String keyList = (String)e.nextElement();
        //替换的值
        Vector cluList = (Vector)clusters.get(keyList);
        int plp = cluList.size();
        String[] rplValue = new String[plp];
        for(int j=0;j<plp;j++){
          String s = (String)cluList.elementAt(j);
          rplValue[j] = s;
        }
        tabLine = rplValueStr(tabLine,rplValue,keyList);
      }
      //去掉换行符
      int ylp = tabLine.size();
      boolean putIs = true;
      boolean firsttrIs = true;
      boolean firstputIs = false;
      Vector tmpve = new Vector();
      for(int y=ylp -1;y>=0;y--){
        String s = (String)tabLine.elementAt(y);
        if(firsttrIs){
          if(s.indexOf("</tr>",0) > -1){
            putIs = false;
          }
          if(s.indexOf("<tr",0) > -1){
            putIs = true;
            firstputIs = true;
            firsttrIs = false;
          }
        }
        if(firstputIs){
          firstputIs = false;
          continue;
        }
        if(putIs && !firstputIs){
          tmpve.addElement(new String(s));
        }
      }
      //去掉换行符end
      int flp = tmpve.size();
      for(int f=flp-1;f>=0;f--){
        String s = (String)tmpve.elementAt(f);
        tableContNoSp.addElement(new String(s));
      }

      //表的公共部分填写完成END

      //开始填写数据部分Begin
      //最新的要写到外部的文件
      Vector wriTable = tableBefore;
      //wriTable.addElement(new String(tabLine));
      if(category.equalsIgnoreCase("REPORT")){
        //reportdata = new String[3][11];
        //记录型报表格式
        int totalData = com.coolservlets.beans.method.genMethod.statSeparCount(tabRows,covertag);
        int clmCount = reportdata[0].length;
        totalData--;
        aPageLineCount = totalData / clmCount;
        String[] rplValue = new String[totalData];
        for(int j=0;j<totalData;j++){
          rplValue[j] = "";
        }
        int totalRow = reportdata.length;
        int pageActRow = 0;
        for(int i=0;i<totalRow;i++){
          if(pageActRow == aPageLineCount){
            Vector dataLine = rplValueStr(tabLine,rplValue,covertag);
            int klp= dataLine.size();
            for(int k=0;k<klp;k++){
              String s = (String)dataLine.elementAt(k);
              wriTable.addElement(new String(s));
            }
            wriTable.addElement(new String("<P>"));
            for(int j=0;j<totalData;j++){
              rplValue[j] = "";
            }
            pageActRow = 0;
          }
          int clmlp = reportdata[i].length;
          for(int j=0;j<clmlp;j++){
            int clmcnt = pageActRow * clmlp + j;
            rplValue[clmcnt] = reportdata[i][j];
          }
          pageActRow++;
        }
        if(pageActRow > 0){
          Vector dataLine = rplValueStr(tableContNoSp,rplValue,covertag);
          int klp= dataLine.size();
          for(int k=0;k<klp;k++){
            String s = (String)dataLine.elementAt(k);
            wriTable.addElement(new String(s));
          }
          //wriTable.addElement(new String("<P>"));
        }
      }
      if(category.equalsIgnoreCase("CARD")){
        //卡片型报表格式
      }
      //开始填写数据部分END

      //最新的要写到外部的文件
      int alp = tableAfter.size();
      for(int t=0;t<alp;t++){
        String s = (String)tableAfter.elementAt(t);
        wriTable.addElement(new String(s));
      }
      String s ="";
      int xlp = wriTable.size();
      for(int x=0;x<xlp;x++){
        String ts = (String)wriTable.elementAt(x);
        s = s + ts;
        //System.out.println("s = "+s);
      }
      //return s;
      //genMethod.writeFileRecord(wriTable,printflm,"1");
      return s;
      //<P>
    }

    public String createHtmlReportByXls() throws Exception {
      String tabRows = "";
      int ulp = HtmlModule.size();  
      for(int u=0;u<ulp;u++){
        String s= (String)HtmlModule.elementAt(u);
        tabRows = tabRows + s;
      }
      //System.out.println("-------tabRows:"+tabRows);
      //System.out.println("-------covertag:"+covertag);
      int totalData = com.coolservlets.beans.method.genMethod.statSeparCount(tabRows,covertag);
      
      //替换表前端内容
      Enumeration ee = clusters.propertyNames();
      while (ee.hasMoreElements()) {
        //替换的符号
        String keyList = (String) ee.nextElement();
        //替换的值
        Vector cluList = (Vector) clusters.get(keyList);
        int plp = cluList.size();
        String[] rplValue = new String[plp];
        for (int j = 0; j < plp; j++) {
          String s = (String) cluList.elementAt(j);
          rplValue[j] = s;
        }
        HtmlModule = rplValueStr(HtmlModule, rplValue, keyList);
      }
      if(category.equalsIgnoreCase("REPORT")){
        int clmCount = reportdata[0].length;
        int rowCount = reportdata.length;
        //int iCount = clmCount * rowCount;
        

        
        
       // System.out.println("------------totalData:"+totalData);
        String[] rplValue = new String[totalData];
        int ia = 0;
        for(int r = 0;r< rowCount;r++){
          for(int c = 0;c< clmCount;c++){
        	 // System.out.println(r+"--"+c+":"+reportdata[r][c]);
            rplValue[ia] = reportdata[r][c];
            ia++;
          }
        }
        for(int i=ia;i<totalData;i++){
          rplValue[i] = "";
        }
        HtmlModule = rplValueStr(HtmlModule,rplValue,covertag);
      }
      String s ="";
      int xlp = HtmlModule.size();
      for(int x=0;x<xlp;x++){
        String ts = (String)HtmlModule.elementAt(x);
        s = s + ts;
      }
      return s;
    }

    /**
     *
     * @该方法为行记录不固定的表格的格式打印
     * @return String
     */
    public String createHtmlReportByColl() throws Exception {
      //将<style>风格行添加在htmlreport中 begin
      boolean addStyle = true;
      boolean addTable = true;
      if(htmlreport.length() > 0 && addStyle){
        //得到<style>风格行
        //Vector vStyle = new Vector();
        boolean inIs = false;
        int sloop = HtmlModule.size();
        String style = "";
        for (int s = 0; s < sloop; s++) {
          String sl = (String) HtmlModule.elementAt(s);
          if(sl.indexOf("</head>",0) > -1){
            break;
          }
          if(sl.indexOf("</style>",0) > -1){
            inIs = false;
          }
          if(inIs){
            style = style + sl;
          }
          if(sl.indexOf("<style",0) > -1){
            inIs = true;
          }
        }
        int st = 0;
        int sp = htmlreport.indexOf("</style>",0);
        String ls = htmlreport.substring(st,sp)+ style + htmlreport.substring(sp,htmlreport.length());
        htmlreport = ls;
        addStyle = false;
      }
      //将<style>风格行添加在htmlreport中 end

      //处理表<table></table>的内容 begin
      if(htmlreport.length() > 0 && addTable){
        Vector vTable = new Vector();
        boolean inIs = false;
        int sloop = HtmlModule.size();
        //String style = "";
        for (int s = 0; s < sloop; s++) {
          String sl = (String) HtmlModule.elementAt(s);
          if(sl.indexOf("<table",0) > -1){
            inIs = true;
          }
          if(inIs){
            vTable.addElement(new String(sl));
          }
          if(sl.indexOf("</table>",0) > -1){
            break;
          }
        }
        HtmlModule = new Vector();
        HtmlModule = vTable;
        addTable = false;
      }
      //处理表<table></table>的内容 end

      //替换表前端内容
      if(!clusters.isEmpty()){
        Enumeration ee = clusters.propertyNames();
        while (ee.hasMoreElements()) {
          //替换的符号
          String keyList = (String) ee.nextElement();
          //替换的值
          Vector cluList = (Vector) clusters.get(keyList);
          int plp = cluList.size();
          String[] rplValue = new String[plp];
          for (int j = 0; j < plp; j++) {
            String s = (String) cluList.elementAt(j);
            rplValue[j] = s;
          }
          HtmlModule = rplValueStr(HtmlModule, rplValue, keyList);
        }
      }
      String[] vReport = null;
      if(category.equalsIgnoreCase("REPORT")){
        int clmCount = reportdata[0].length;
        int rowCount = reportdata.length;
        vReport = new String[rowCount * HtmlModule.size()];
        int vInt = 0;
        for(int r = 0;r< rowCount;r++){
          String[] rplValue = new String[clmCount];
          for(int c = 0;c< clmCount;c++){
            rplValue[c] = reportdata[r][c];
          }
          String[] tReport = rplValueStrByArry(HtmlModule,rplValue,covertag);
          //Vector tReport = HtmlModule;
          int floop = tReport.length;
          for(int f=0;f<floop;f++){
            vReport[vInt] = tReport[f];
            vInt++;
          }
        }
      }
      if (category.equalsIgnoreCase("CARD")) {
        //卡片型报表格式
        int clmCount = reportdata[0].length;
        int rowCount = reportdata.length;
        int totalData = rowCount * clmCount;
        //int iCount = clmCount * rowCount;
        String[] rplValue = new String[totalData];
        int ia = 0;
        for(int r = 0;r< rowCount;r++){
          for(int c = 0;c< clmCount;c++){
            rplValue[ia] = reportdata[r][c];
            ia++;
          }
        }
        for(int i=ia;i<totalData;i++){
          rplValue[i] = "";
        }
        vReport = rplValueStrByArry(HtmlModule,rplValue,covertag);
      }
      String sl = "";
      int xloop = vReport.length;
      for (int x = 0; x < xloop; x++) {
        //String sx = (String) vReport.elementAt(x);
        sl = sl + vReport[x];
        //System.out.println("sl="+sl);
      }
      if (htmlreport.length() > 0) {
        int st = 0;
        int sp = htmlreport.indexOf("</div>",0);
        htmlreport = htmlreport.substring(st,sp) + sl + htmlreport.substring(sp,htmlreport.length());
      }else{
        htmlreport = sl;
      }
      return htmlreport;
    }

    /**
     *
     * @该方法为行记录不固定的表格的格式打印,处理多个卡片一起打印
     * PageNextIs : 当有多个卡片一起打印时，是否分页
     * @return String
     */
    public String createHtmlReportByCard(boolean PageNextIs) throws Exception {
      //将<style>风格行添加在htmlreport中 begin
      boolean addTable = true;
      //将<style>风格行添加在htmlreport中 end
      //处理表<table></table>的内容 begin
      //模板左边
      Vector vLeft = new Vector();
      //模板右边
      Vector vRight = new Vector();
      if(addTable){
        Vector vTable = new Vector();
        boolean inIs = false;
        int sloop = HtmlModule.size();
        //<table 起始行号
        int istRow = 0;
        //最后一个</table> 行号
        int ispRow = 0;
        //获得<table></table> 的内容
        for (int s = 0; s < sloop; s++) {
          String sl = (String) HtmlModule.elementAt(s);
          if(sl.indexOf("<table",0) > -1 && !inIs){
            inIs = true;
            istRow = s;
          }
          if(sl.indexOf("</table>",0) > -1 && inIs){
            ispRow = s;
          }
        }
        for (int s = 0; s < sloop; s++) {
          String sl = (String) HtmlModule.elementAt(s);
          if(s < istRow ) vLeft.addElement(new String(sl));
          if(s >=istRow && s <= ispRow) vTable.addElement(new String(sl));
          if(s > ispRow ) vRight.addElement(new String(sl));
        }
        HtmlModule = new Vector();
        HtmlModule = vTable;
        addTable = false;
      }
      //处理表<table></table>的内容 end
      Vector vReport = new Vector();
      if(category.equalsIgnoreCase("REPORT")){
        //暂时没有此类型
      }
      if (category.equalsIgnoreCase("CARD")) {
        //卡片型报表格式
        int clmCount = reportdata[0].length;
        int rowCount = reportdata.length;
        for(int r = 0;r< rowCount;r++){
          String[] rplValue = new String[clmCount];
          for(int c = 0;c< clmCount;c++){
            rplValue[c] = reportdata[r][c];
          }
          String[] aReport = rplValueStrByArry(HtmlModule,rplValue,covertag);
          int uloop = aReport.length;
          for(int u=0;u<uloop;u++){
            String ts = aReport[u];
            vReport.addElement(new String(ts));
          }
          if(PageNextIs && r < rowCount -1){
            vReport.addElement(new String("<div class=\"PageNext\"></div>"));
          }
        }
      }
      String sl = "";
      int loop = vLeft.size();
      for(int l=0;l<loop;l++){
        String st = (String) vLeft.elementAt(l);
        sl = sl + st;
      }
      loop = vReport.size();
      for(int l=0;l<loop;l++){
        String st = (String) vReport.elementAt(l);
        sl = sl + st;
      }
      loop = vRight.size();
      for(int l=0;l<loop;l++){
        String st = (String) vRight.elementAt(l);
        sl = sl + st;
      }
      return sl;
    }

    public static void test01(String[] args) {
       //调用例子
       HtmlReport htmlReport = new HtmlReport();
       //01
       htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\workorder01.htm"));
       /**
       Vector vecTitle = new Vector();
       vecTitle.addElement(new String("10份预防性维护计划"));
       Properties clusters = new Properties();
       clusters.put("$$TITLE",vecTitle);
       Vector vecDate = new Vector();
       vecDate.addElement(new String("1-7"));
       vecDate.addElement(new String("8-15"));
       vecDate.addElement(new String("16-23"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("1-7"));
       vecDate.addElement(new String("8-15"));
       vecDate.addElement(new String("16-23"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       clusters.put("$$DATE",vecDate);
       htmlReport.setClusters(clusters);
       htmlReport.setCategory("REPORT");
       htmlReport.setCovertag("$$$$");
       String[][] testData = new String[1000][18];
       int lp = testData.length;
       for(int i=0;i<lp;i++){
         int ulp = testData[i].length;
         for(int j=0;j<ulp;j++){
           testData[i][j] = "t"+Integer.toString(i)+" "+Integer.toString(j);
         }
       }
       **/
      htmlReport.setCategory("CARD");
      htmlReport.setCovertag("***");
      String[][] testData = new String[1][18];
      int lp = testData.length;
      for (int i = 0; i < lp; i++) {
        int ulp = testData[i].length;
        for (int j = 0; j < ulp; j++) {
          testData[i][j] = "t" + Integer.toString(i) + " " + Integer.toString(j);
        }
      }
      htmlReport.setReportdata(testData);
      try {
        //System.out.println("exeFlm=......");
        String exeFlm = htmlReport.createHtmlReportByColl();
        //02
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\workorder02.htm"));
        htmlReport.setCategory("REPORT");
        htmlReport.setCovertag("***");
        String[][] testData02 = new String[8][2];
        lp = testData02.length;
        for (int i = 0; i < lp; i++) {
           testData02[i][0] = Integer.toString(i+1);
           testData02[i][1] = Integer.toString(i)+ "_" + Integer.toString(i)+"戴安全帽戴安全帽戴安全帽戴安全帽戴安全帽戴安全帽戴安全帽戴安全帽";
        }
        htmlReport.setReportdata(testData02);
        exeFlm = htmlReport.createHtmlReportByColl();
        //03
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\workorder03.htm"));
        htmlReport.setCategory("CARD");
        htmlReport.setCovertag("***");
        String[][] testData03 = new String[1][2];
        lp = testData03.length;
        for (int i = 0; i < lp; i++) {
          int ulp = testData03[i].length;
          for (int j = 0; j < ulp; j++) {
            testData03[i][j] = "t" + Integer.toString(i) + " " + Integer.toString(j);
          }
        }
        htmlReport.setReportdata(testData03);
        exeFlm = htmlReport.createHtmlReportByColl();
        //04
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\workorder04.htm"));
        htmlReport.setCategory("REPORT");
        htmlReport.setCovertag("***");
        String[][] testData04 = new String[10][2];
        lp = testData04.length;
        for (int i = 0; i < lp; i++) {
           testData04[i][0] = Integer.toString(i+1);
           testData04[i][1] = Integer.toString(i)+ "_" + Integer.toString(i)+"具体描述（再鉴定/试验的条件及要求）";
        }
        htmlReport.setReportdata(testData04);
        exeFlm = htmlReport.createHtmlReportByColl();
        //05
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\workorder05.htm"));
        htmlReport.setCategory("CARD");
        htmlReport.setCovertag("***");
        String[][] testData05 = new String[1][22];
        lp = testData05.length;
        for (int i = 0; i < lp; i++) {
          int ulp = testData05[i].length;
          for (int j = 0; j < ulp; j++) {
            testData05[i][j] = "t" + Integer.toString(i) + Integer.toString(j);
          }
        }
        htmlReport.setReportdata(testData05);
        exeFlm = htmlReport.createHtmlReportByColl();
        System.out.println("exeFlm="+exeFlm);
        //String exeFlm = htmlReport.getPrintflm();
        Runtime hello=Runtime.getRuntime();//测试如何调用exe可执行文件通过
       // hello.exec("cmd /E:ON /c start IEXPLORE.EXE "+exeFlm);
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
    }

    public static void main(String[] args) {
      /**
       Vector vecTitle = new Vector();
       vecTitle.addElement(new String("10份预防性维护计划"));
       Properties clusters = new Properties();
       clusters.put("$$TITLE",vecTitle);
       Vector vecDate = new Vector();
       vecDate.addElement(new String("1-7"));
       vecDate.addElement(new String("8-15"));
       vecDate.addElement(new String("16-23"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("1-7"));
       vecDate.addElement(new String("8-15"));
       vecDate.addElement(new String("16-23"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       clusters.put("$$DATE",vecDate);
       htmlReport.setClusters(clusters);
       htmlReport.setCategory("REPORT");
       htmlReport.setCovertag("$$$$");
       String[][] testData = new String[1000][18];
       int lp = testData.length;
       for(int i=0;i<lp;i++){
         int ulp = testData[i].length;
         for(int j=0;j<ulp;j++){
           testData[i][j] = "t"+Integer.toString(i)+" "+Integer.toString(j);
         }
       }
       **/

      /**
       //调用例子
       HtmlReport htmlReport = new HtmlReport();
       //01
       htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\qucontrol01.htm"));
      htmlReport.setCategory("CARD");
      htmlReport.setCovertag("***");
      String[][] testData = new String[1][1];
      int lp = testData.length;
      for (int i = 0; i < lp; i++) {
        int ulp = testData[i].length;
        for (int j = 0; j < ulp; j++) {
          testData[i][j] = "t" + Integer.toString(i) + " " + Integer.toString(j);
        }
      }
      htmlReport.setReportdata(testData);
      try {
        //System.out.println("exeFlm=......");
        String exeFlm = htmlReport.createHtmlReportByColl();
        //02
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\qucontrol02.htm"));
        htmlReport.setCategory("REPORT");
        htmlReport.setCovertag("***");
        String[][] testData02 = new String[21][7];
        lp = testData02.length;
        for (int i = 0; i < lp; i++) {
          int ulp = testData02[i].length;
          for (int j = 0; j < ulp; j++) {
            testData02[i][j] = "t" + Integer.toString(i) + " " +Integer.toString(j);
          }
        }
        htmlReport.setReportdata(testData02);
        //System.out.println("********************");
        exeFlm = htmlReport.createHtmlReportByColl();
        //03
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\qucontrol03.htm"));
        htmlReport.setCategory("CARD");
        htmlReport.setCovertag("***");
        String[][] testData03 = new String[1][2];
        lp = testData03.length;
        for (int i = 0; i < lp; i++) {
          int ulp = testData03[i].length;
          for (int j = 0; j < ulp; j++) {
            testData03[i][j] = "t" + Integer.toString(i) + " " + Integer.toString(j);
          }
        }
        htmlReport.setReportdata(testData03);
        exeFlm = htmlReport.createHtmlReportByColl();
        System.out.println("exeFlm="+exeFlm);
        //String exeFlm = htmlReport.getPrintflm();
        Runtime hello=Runtime.getRuntime();//测试如何调用exe可执行文件通过
       // hello.exec("cmd /E:ON /c start IEXPLORE.EXE "+exeFlm);

      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
      **/
     //调用例子
     HtmlReport htmlReport = new HtmlReport();
     htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\hdepc\\app\\wotrack\\caution\\wthcaurpt\\wthcaurpt.htm"));
     htmlReport.setCategory("CARD");
     htmlReport.setCovertag("***");
     String[][] testData = new String[2][7];
     int lp = testData.length;
     for (int i = 0; i < lp; i++) {
       int ulp = testData[i].length;
       for (int j = 0; j < ulp; j++) {
         testData[i][j] = "t" + Integer.toString(i) + " " + Integer.toString(j);
       }
     }
     htmlReport.setReportdata(testData);
     try {
       String exeFlm = htmlReport.createHtmlReportByCard(true);
       System.out.println("exeFlm="+exeFlm);
       Runtime hello=Runtime.getRuntime();
       hello.exec("cmd /E:ON /c start IEXPLORE.EXE "+exeFlm);
     }
      catch (Exception ex) {
        ex.printStackTrace();
      }

    }

    public static void test03(String[] args) {
       //调用例子
       HtmlReport htmlReport = new HtmlReport();
       //01
       htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\spart01.htm"));
       /**
       Vector vecTitle = new Vector();
       vecTitle.addElement(new String("10份预防性维护计划"));
       Properties clusters = new Properties();
       clusters.put("$$TITLE",vecTitle);
       Vector vecDate = new Vector();
       vecDate.addElement(new String("1-7"));
       vecDate.addElement(new String("8-15"));
       vecDate.addElement(new String("16-23"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("1-7"));
       vecDate.addElement(new String("8-15"));
       vecDate.addElement(new String("16-23"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       clusters.put("$$DATE",vecDate);
       htmlReport.setClusters(clusters);
       htmlReport.setCategory("REPORT");
       htmlReport.setCovertag("$$$$");
       String[][] testData = new String[1000][18];
       int lp = testData.length;
       for(int i=0;i<lp;i++){
         int ulp = testData[i].length;
         for(int j=0;j<ulp;j++){
           testData[i][j] = "t"+Integer.toString(i)+" "+Integer.toString(j);
         }
       }
       **/
      htmlReport.setCategory("CARD");
      htmlReport.setCovertag("***");
      String[][] testData = new String[1][1];
      int lp = testData.length;
      for (int i = 0; i < lp; i++) {
        int ulp = testData[i].length;
        for (int j = 0; j < ulp; j++) {
          testData[i][j] = "t" + Integer.toString(i) + " " + Integer.toString(j);
        }
      }
      htmlReport.setReportdata(testData);
      try {
        //System.out.println("exeFlm=......");
        String exeFlm = htmlReport.createHtmlReportByColl();
        //02
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\spart02.htm"));
        htmlReport.setCategory("REPORT");
        htmlReport.setCovertag("***");
        String[][] testData02 = new String[21][6];
        lp = testData02.length;
        for (int i = 0; i < lp; i++) {
          int ulp = testData02[i].length;
          for (int j = 0; j < ulp; j++) {
            testData02[i][j] = "t" + Integer.toString(i) + " " +Integer.toString(j);
          }
        }
        htmlReport.setReportdata(testData02);
        //System.out.println("********************");
        exeFlm = htmlReport.createHtmlReportByColl();
        //03
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\spart03.htm"));
        htmlReport.setCategory("CARD");
        htmlReport.setCovertag("***");
        String[][] testData03 = new String[1][2];
        lp = testData03.length;
        for (int i = 0; i < lp; i++) {
          int ulp = testData03[i].length;
          for (int j = 0; j < ulp; j++) {
            testData03[i][j] = "t" + Integer.toString(i) + " " + Integer.toString(j);
          }
        }
        htmlReport.setReportdata(testData03);
        exeFlm = htmlReport.createHtmlReportByColl();
        //04
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\spart02.htm"));
        htmlReport.setCategory("REPORT");
        htmlReport.setCovertag("***");
        String[][] testData04 = new String[21][6];
        lp = testData04.length;
        for (int i = 0; i < lp; i++) {
          int ulp = testData04[i].length;
          for (int j = 0; j < ulp; j++) {
            testData04[i][j] = "t" + Integer.toString(i) + " " +Integer.toString(j);
          }
        }
        htmlReport.setReportdata(testData04);
        //System.out.println("********************");
        exeFlm = htmlReport.createHtmlReportByColl();
        System.out.println("exeFlm="+exeFlm);
        //String exeFlm = htmlReport.getPrintflm();
        Runtime hello=Runtime.getRuntime();//测试如何调用exe可执行文件通过
       // hello.exec("cmd /E:ON /c start IEXPLORE.EXE "+exeFlm);
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
    }

    public static void test04(String[] args) {
       //调用例子
       HtmlReport htmlReport = new HtmlReport();
       //01
       htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\mareport01.htm"));
       /**
       Vector vecTitle = new Vector();
       vecTitle.addElement(new String("10份预防性维护计划"));
       Properties clusters = new Properties();
       clusters.put("$$TITLE",vecTitle);
       Vector vecDate = new Vector();
       vecDate.addElement(new String("1-7"));
       vecDate.addElement(new String("8-15"));
       vecDate.addElement(new String("16-23"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("1-7"));
       vecDate.addElement(new String("8-15"));
       vecDate.addElement(new String("16-23"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       vecDate.addElement(new String("24-31"));
       clusters.put("$$DATE",vecDate);
       htmlReport.setClusters(clusters);
       htmlReport.setCategory("REPORT");
       htmlReport.setCovertag("$$$$");
       String[][] testData = new String[1000][18];
       int lp = testData.length;
       for(int i=0;i<lp;i++){
         int ulp = testData[i].length;
         for(int j=0;j<ulp;j++){
           testData[i][j] = "t"+Integer.toString(i)+" "+Integer.toString(j);
         }
       }
       **/
      htmlReport.setCategory("CARD");
      htmlReport.setCovertag("***");
      String[][] testData = new String[1][4];
      int lp = testData.length;
      for (int i = 0; i < lp; i++) {
        int ulp = testData[i].length;
        for (int j = 0; j < ulp; j++) {
          testData[i][j] = "t" + Integer.toString(i) + " " + Integer.toString(j);
        }
      }
      htmlReport.setReportdata(testData);
      try {
        //System.out.println("exeFlm=......");
        String exeFlm = htmlReport.createHtmlReportByColl();
        //02
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\mareport02.htm"));
        htmlReport.setCategory("REPORT");
        htmlReport.setCovertag("***");
        String[][] testData02 = new String[21][4];
        lp = testData02.length;
        for (int i = 0; i < lp; i++) {
          int ulp = testData02[i].length;
          for (int j = 0; j < ulp; j++) {
            testData02[i][j] = "t" + Integer.toString(i) + " " +Integer.toString(j);
          }
        }
        htmlReport.setReportdata(testData02);
        //System.out.println("********************");
        exeFlm = htmlReport.createHtmlReportByColl();
        //03
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\mareport03.htm"));
        htmlReport.setCategory("CARD");
        htmlReport.setCovertag("***");
        String[][] testData03 = new String[1][0];
        lp = testData03.length;
        for (int i = 0; i < lp; i++) {
          int ulp = testData03[i].length;
          for (int j = 0; j < ulp; j++) {
            testData03[i][j] = "t" + Integer.toString(i) + " " + Integer.toString(j);
          }
        }
        htmlReport.setReportdata(testData03);
        exeFlm = htmlReport.createHtmlReportByColl();
        //04
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\mareport04.htm"));
        htmlReport.setCategory("REPORT");
        htmlReport.setCovertag("***");
        String[][] testData04 = new String[10][5];
        lp = testData04.length;
        for (int i = 0; i < lp; i++) {
          int ulp = testData04[i].length;
          for (int j = 0; j < ulp; j++) {
            testData04[i][j] = "t" + Integer.toString(i) + " " +Integer.toString(j);
          }
        }
        htmlReport.setReportdata(testData04);
        //System.out.println("********************");
        exeFlm = htmlReport.createHtmlReportByColl();
        //05
        htmlReport.setModulefln(htmlReport.getPrintflm("D:\\sqlrun\\reportcoll\\mareport05.htm"));
        htmlReport.setCategory("CARD");
        htmlReport.setCovertag("***");
        String[][] testData05 = new String[1][6];
        lp = testData05.length;
        for (int i = 0; i < lp; i++) {
          int ulp = testData05[i].length;
          for (int j = 0; j < ulp; j++) {
            testData05[i][j] = "t" + Integer.toString(i) + " " + Integer.toString(j);
          }
        }
        htmlReport.setReportdata(testData05);
        exeFlm = htmlReport.createHtmlReportByColl();
        System.out.println("exeFlm="+exeFlm);
        //String exeFlm = htmlReport.getPrintflm();
        Runtime hello=Runtime.getRuntime();//测试如何调用exe可执行文件通过
       // hello.exec("cmd /E:ON /c start IEXPLORE.EXE "+exeFlm);
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
    }

    //每页行数
    public void setAPageLineCount(int p_aPageLineCount){
      this.aPageLineCount = p_aPageLineCount;
    }
    //需要动态设置的元素：key:替换成数据的标记；value:为String[];
    public void setClusters(Properties p_clusters){
      this.clusters = p_clusters;
    }
    //报表表头
    public void setReportTitle(String p_reportTitle) {
       this.reportTitle = p_reportTitle;
    }
    //报表种类category :"CARD" 卡片式；"REPORT" 记录格式
    public void setCategory(String p_Category){
      this.category = p_Category;
    }
    //卡片式报表数据
    public void setCarddata(String[] p_carddata) {
       this.carddata = p_carddata;
    }
    //记录格式报表数据
    public void setReportdata(String[][] p_reportdata) {
       this.reportdata = p_reportdata;
    }
    //替换成数据的标记
    public void setCovertag(String p_covertag) {
      this.covertag = p_covertag;
    }
    //报表模板文件名
    public void setModulefln(Vector vHtml) {
    	this.HtmlModule = vHtml;
    }
    //报表模板文件名
    public Vector getPrintflm(String modpath) {
      Vector htmlModule = com.coolservlets.beans.method.genMethod.readFileRecord(modpath);
      return htmlModule;
    }
    //按指定的字符替换行
    public Vector rplValueStr(Vector rplVec,String[] rplValue,String covertag)
    {
      Vector rplNewvec = new Vector();
      int m_StartStr = 0;
      int m_StopStr =0;
      int count = 0;
      int lp = rplVec.size();
      //System.out.println("covertag ="+covertag);
      for(int i=0;i<lp;i++){
        String p_LineStr = (String)rplVec.elementAt(i);
        //System.out.println(p_LineStr);
        m_StartStr = 0;
        m_StopStr =0;
        while((p_LineStr.indexOf(covertag,m_StartStr) > -1)){
          m_StopStr = p_LineStr.indexOf(covertag,m_StartStr);
          String rightStr = p_LineStr.substring(0,m_StopStr);
          String leftStr = p_LineStr.substring(m_StopStr+covertag.length(),p_LineStr.length());
          p_LineStr = rightStr + rplValue[count]+ leftStr;
          m_StartStr = m_StopStr+covertag.length();
          //System.out.println("count ="+count);
          //System.out.println("rplValue ="+rplValue[count]);
          count++;
        }
        rplNewvec.addElement(new String(p_LineStr));
      }
      return rplNewvec;
    }

    public String[] rplValueStrByArry(Vector rplVec,String[] rplValue,String covertag)
    {
      String[] rplNewvec = new String[rplVec.size()];
      int m_StartStr = 0;
      int m_StopStr =0;
      int count = 0;
      int lp = rplVec.size();
      //System.out.println("covertag ="+covertag);
      for(int i=0;i<lp;i++){
        String p_LineStr = (String)rplVec.elementAt(i);
        //System.out.println(p_LineStr);
        m_StartStr = 0;
        m_StopStr =0;
        while((p_LineStr.indexOf(covertag,m_StartStr) > -1)){
          m_StopStr = p_LineStr.indexOf(covertag,m_StartStr);
          String rightStr = p_LineStr.substring(0,m_StopStr);
          String leftStr = p_LineStr.substring(m_StopStr+covertag.length(),p_LineStr.length());
          p_LineStr = rightStr + rplValue[count]+ leftStr;
          m_StartStr = m_StopStr+covertag.length();
          //System.out.println("count ="+count);
          //System.out.println("rplValue ="+rplValue[count]);
          count++;
        }
        rplNewvec[i] = p_LineStr + "\n";
        //rplNewvec.addElement(new String(p_LineStr));
      }
      return rplNewvec;
    }

    //读出配置文件
    public String loadConfigValue(String name,String key)
        throws Exception
    {
      String rc = null;

      // Get our class loader
      ClassLoader cl = getClass().getClassLoader();

      // Attempt to open an input stream to the configuration file.
      // The configuration file is considered to be a system
      // resource.
      java.io.InputStream in;

      if (cl != null) {
        in = cl.getResourceAsStream(name);
      }
      else {
        in = ClassLoader.getSystemResourceAsStream(name);
      }

      // If the input stream is null, then the configuration file
      // was not found
      if (in == null) {
        throw new Exception("configuration file '" +
                            name + "' not found");
      }
      else {
        try {
          java.util.Properties m_ConfigProperties = new java.util.Properties();

          // Load the configuration file into the properties table
          m_ConfigProperties.load(in);

          // Got the properties. Pull out the properties that we
          // are interested in
          rc  = consume(m_ConfigProperties,key);

        }
        finally {
          // Always close the input stream
          if (in != null) {
            try {
              in.close();
            }
            catch (Exception ex) {
            }
          }
        }
      }
      return rc;
    }
    /**
     * <p>Consumes the given property and returns the value.
     *
     * @param properties Properties table
     * @param key Key of the property to retrieve and remove from
     * the properties table
     * @return Value of the property, or null if not found
     */
    private String consume(java.util.Properties p, String key)
    {
      String s = null;

      if ((p != null) &&
          (key != null)) {

        // Get the value of the key
        s = p.getProperty(key);

        // If found, remove it from the properties table
        if (s != null) {
          p.remove(key);
        }
      }
      return s;
    }
}
