package com.report;
import java.awt.Dimension;
import com.report.CustCell;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
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.genMethod;
import jxl.*;
import jxl.CellFormat.*;
import com.coolservlets.beans.method.*;
import jxl.write.biff.*;
import jxl.write.*;
import jxl.*;
import java.io.*;
import jxl.format.PageOrientation;
import jxl.format.PaperSize;
import jxl.write.Label;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import java.io.File;
/*
 * 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 ExcelReportSheets 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;
	//模板的 sheet 页编号
	int SheetNumber = 0;
	//记录填写数据记录的起始列号
	private int[]  MergerColum;
	//记录填写数据记录的起始行号
	private int  MergerSartRow;
	//报表模板文件的二进制流
	private byte [] ExeclModule = null;
	//报表模板文件的Excel 文件
	WritableWorkbook Sheetwwb = null;
	//报表模板文件的ByteArrayOutputStream 文件
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	//外部文件名
	//private String printflm;
	//全局表字符串，用来保存上次报表的字符串
	private byte [] excelreport = null;
	//新创建报表的sheet 名
	private String SheetName = null;
	//创建的报表模板
	HashMap HmReportModule = new HashMap();
	//创建的报表的数据列数
	int DataCloums = 0;
	//有效单元格的键值
	Vector vModuleKey = new Vector();
	//设置数据打印内容的字体！缺省的是 8号字
	int DataFontSize = 8;
	//设置数据打印的行高
	int DataLineHeight = 225;
	//报表表头的行数
	int TitleRows = 0;
	//创建的二进制流的报表模板
	private byte [] BytesExcelModule = null;
	//多个excel 合并为一个 excel 的 sheet
	WritableSheet MergerSheet = null;
	//多个excel 合并为一个 excel 的 sheet 上创建 新行的起始行号
	int MergerStartRow = 0;
	//某个 Sheet 上创建 数据行的起始行号
	int SheetStartRow = 0;
	//多个excel 合并为一个 excel 的 sheet 填写完数据的最后行行号
	int MergerStopRow = 0;

	public ExcelReportSheets() {
		super();
	}

	public byte [] createExcelReport() throws Exception{
		byte [] reportbytes = null;
		if(ExeclModule != null){
			//创建的报表模板
			//HmReportModule = createReportModule();
			//System.out.println("createReportModule is end");
			reportbytes = writeToExcel();
		}else{
			System.out.println("没有创建的报表模板，请先创建的报表模板！");
		}
		return reportbytes;
	}

	public WritableSheet createExcelReportMerger() throws Exception{
		WritableSheet SingleSheet = null;
		if(ExeclModule != null){
			//创建的报表模板
			//HmReportModule = createReportModule();
			//System.out.println("createReportModule is end");
			SingleSheet = writeToExcelMerger();
		}else{
			System.out.println("没有创建的报表模板，请先创建的报表模板！");
		}
		return SingleSheet;
	}

	public void createExcelReportSheet() throws Exception{
		WritableSheet SingleSheet = null;
		if(ExeclModule != null){
			//创建的报表-在对应的sheet 页上添加数据
			writeToExcelSheet();
		}else{
			System.out.println("没有创建的报表模板，请先创建的报表模板！");
		}
		//return SingleSheet;
	}

	public byte[] getSheetReportExcel() throws Exception 
	{
		Sheet[] sheets = Sheetwwb.getSheets();
		int mloop = sheets.length;
		for(int i = 1; i < mloop; i++)
		{
			WritableSheet tablesheet = Sheetwwb.getSheet(i);
			tablesheet.getSettings().setSelected(false);
		}
		Sheetwwb.write();
		Sheetwwb.close();
		byte [] ExcelReport = baos.toByteArray();
		return ExcelReport;
	}
	private byte[] writeToExcel() throws Exception 
	{
		byte[] filebyte = null;
		//int ArrayLength = 11;
		if((ReportData != null)&&(ReportData.length!=0)) 
		{
			//System.out.println("ReportData[0].length = " + ReportData[0].length + ", DataCloums =" + DataCloums);
			if (ReportData[0].length == DataCloums) 
			{
				try 
				{
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					// 设置WorkbookSettings，获得excel文件中的工作薄
					WorkbookSettings wss = new WorkbookSettings();
					wss.setEncoding("iso-8859-1");
					WritableWorkbook workbook = Workbook.createWorkbook((OutputStream) baos, wss);
					// 获得工作薄的数量
					WritableSheet tablesheet = workbook.createSheet(SheetName,0);
					for (int i = 0; i < 100; i++) 
					{
						tablesheet.setColumnView(i, 3);
					}
					tablesheet.getSettings().setPrintTitlesRow(0, 2);
					this.geneHeadFooter(tablesheet);
					//System.out.println("geneHeadFooter is end");
					this.geneTableHeader(tablesheet);
					//System.out.println("geneTableHeader is end");
					this.geneContent(tablesheet);
					//System.out.println("geneContent is end");
					workbook.write();
					workbook.close();
					filebyte = baos.toByteArray();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return filebyte;
	}

	private WritableSheet writeToExcelMerger() throws Exception 
	{
		if((ReportData != null)&&(ReportData.length!=0)) 
		{
			System.out.println("ReportData[0].length = " + ReportData[0].length + ", DataCloums =" + DataCloums);
			if (ReportData[0].length == DataCloums) 
			{
				try 
				{
					this.geneHeadFooterMerger(MergerSheet);
					//System.out.println("geneHeadFooter is end");
					this.geneTableHeaderMerger(MergerSheet);
					//System.out.println("geneTableHeader is end");
					this.geneContentMerger(MergerSheet);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return MergerSheet;
	}

	private void writeToExcelSheet() throws Exception 
	{
		if((ReportData != null)&&(ReportData.length!=0)) 
		{
			System.out.println("ReportData[0].length = " + ReportData[0].length + ", DataCloums =" + DataCloums);
			if (ReportData[0].length == DataCloums) 
			{
				try 
				{
					//this.geneHeadFooterMerger(MergerSheet);
					//System.out.println("geneHeadFooter is end");
					//this.geneTableHeaderMerger(MergerSheet);
					//System.out.println("geneTableHeader is end");
					//this.geneContentMerger(MergerSheet);
					this.geneContentSheet();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		//return MergerSheet;
	}

	public void geneHeadFooter(WritableSheet tablesheet) {
		tablesheet.getSettings().setLeftMargin(0.5d);
		tablesheet.getSettings().setRightMargin(0.5d);
		//设置纸张
		tablesheet.setPageSetup(PageOrientation.LANDSCAPE.LANDSCAPE,PaperSize.A4, 0.5d, 0.5d);
		//设置缩放调整宽
		tablesheet.getSettings().setFitWidth(1);
		// 设置页眉页脚
		//HeaderFooter header = new HeaderFooter();
		//header.getCentre().setFontSize(16);
		//header.getCentre().append("&B" + ReportTitle);
		//tablesheet.getSettings().setHeader(header);
		HeaderFooter footer = new HeaderFooter();
		footer.getCentre().append("第 ");
		footer.getCentre().appendPageNumber();
		footer.getCentre().append(" 页 共 ");
		footer.getCentre().appendTotalPages();
		footer.getCentre().append(" 页");
		tablesheet.getSettings().setFooter(footer);
	}

	public void geneHeadFooterMerger(WritableSheet tablesheet) {
		tablesheet.getSettings().setLeftMargin(0.5d);
		tablesheet.getSettings().setRightMargin(0.5d);
		//设置纸张
		tablesheet.setPageSetup(PageOrientation.LANDSCAPE.LANDSCAPE,PaperSize.A4, 0.5d, 0.5d);
		//设置缩放调整宽
		tablesheet.getSettings().setFitWidth(1);
		// 设置页眉页脚
		//HeaderFooter header = new HeaderFooter();
		//header.getCentre().setFontSize(16);
		//header.getCentre().append("&B" + ReportTitle);
		//tablesheet.getSettings().setHeader(header);
		HeaderFooter footer = new HeaderFooter();
		footer.getCentre().append("第 ");
		footer.getCentre().appendPageNumber();
		footer.getCentre().append(" 页 共 ");
		footer.getCentre().appendTotalPages();
		footer.getCentre().append(" 页");
		tablesheet.getSettings().setFooter(footer);
	}

	public void geneTableHeader(WritableSheet tablesheet) throws Exception {
		int loop = vModuleKey.size();
		Vector vLeaf = new Vector();
		for(int i=0;i<loop;i++){
			String getkey = (String)vModuleKey.elementAt(i);
			CustCell custCell= (CustCell)HmReportModule.get(getkey);
			//获得当前单元格的内容
			String contents = custCell.getContents();
			//获得当前单元格的起始行行号
			int startRows = custCell.getStartRows();
			//获得当前单元格的起始列列号
			int startCloums = custCell.getStartCloums();
			//获得当前单元格的终止行行号
			int stopRows = custCell.getStopRows();
			//获得当前单元格的终止列列号
			int stopCloums = custCell.getStopCloums();
			//获得当前单元格字体大小
			int fontSize = custCell.getFontSize();
			//单元格字体加重
			int boldWeight = custCell.getBoldWeight();
			//设置该单元格的格式
			WritableFont font = new WritableFont(WritableFont.ARIAL,fontSize,WritableFont.BOLD);
			WritableCellFormat cellFormat = new WritableCellFormat(font);
			cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
			//把垂直对齐方式指定为居中
			cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
			cellFormat.setWrap(true);
			cellFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
			//开始构建单元格
			//System.out.println("contents =" + contents + ",startRows =" +startRows + ",stopRows =" +stopRows + ",startCloums =" + startCloums + ",stopCloums=" + stopCloums);
			tablesheet.mergeCells(startCloums,startRows, stopCloums,stopRows);
			WritableCell cell = new Label(startCloums,startRows,contents);
			cell.setCellFormat(cellFormat);
			tablesheet.addCell(cell);

		}
		//表头设置结束
	}

	public void geneTableHeaderMerger(WritableSheet tablesheet) throws Exception {
		int loop = vModuleKey.size();
		Vector vLeaf = new Vector();
		for(int i=0;i<loop;i++){
			String getkey = (String)vModuleKey.elementAt(i);
			CustCell custCell= (CustCell)HmReportModule.get(getkey);
			//获得当前单元格的内容
			String contents = custCell.getContents();
			//获得当前单元格的起始行行号
			int startRows = custCell.getStartRows();
			startRows = MergerStartRow + startRows;
			//获得当前单元格的起始列列号
			int startCloums = custCell.getStartCloums();
			//获得当前单元格的终止行行号
			int stopRows = custCell.getStopRows();
			stopRows = MergerStartRow + stopRows;
			//获得当前单元格的终止列列号
			int stopCloums = custCell.getStopCloums();
			//获得当前单元格字体大小
			int fontSize = custCell.getFontSize();
			//单元格字体加重
			int boldWeight = custCell.getBoldWeight();
			//设置该单元格的格式
			WritableFont font = new WritableFont(WritableFont.ARIAL,fontSize,WritableFont.BOLD);
			WritableCellFormat cellFormat = new WritableCellFormat(font);
			cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
			//把垂直对齐方式指定为居中
			cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
			cellFormat.setWrap(true);
			cellFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
			//开始构建单元格
			//System.out.println("contents =" + contents + ",startRows =" +startRows + ",stopRows =" +stopRows + ",startCloums =" + startCloums + ",stopCloums=" + stopCloums);
			tablesheet.mergeCells(startCloums,startRows, stopCloums,stopRows);
			WritableCell cell = new Label(startCloums,startRows,contents);
			cell.setCellFormat(cellFormat);
			tablesheet.addCell(cell);
		}
		//表头设置结束
	}

	/**
	 * 填写数据
	 * @param tablesheet
	 * @throws Exception
	 */
	public void geneContent(WritableSheet tablesheet) throws Exception {
		if (ReportData != null) {
			if (ReportData[0].length == DataCloums) {
				// 小字体设置
				WritableFont smallfont = new WritableFont(WritableFont.ARIAL,8,WritableFont.NO_BOLD);
				WritableCellFormat smallformat = new WritableCellFormat(smallfont);
				smallformat.setAlignment(jxl.format.Alignment.LEFT);// 把水平对齐方式指定为居中
				smallformat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
				smallformat.setWrap(true);
				smallformat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
				int loop = ReportData.length;
				for(int i=0;i<loop;i++){
					int rownum = TitleRows + i;
					int MaxLineHeight = 0;
					for(int f=0;f<DataCloums;f++){
						int startCloums = MergerColum[f];
						int stopCloums = MergerColum[f+1] - 1;
						tablesheet.mergeCells(startCloums,rownum,stopCloums,rownum);
						WritableCell cell = new Label(startCloums,rownum,ReportData[i][f]);
						cell.setCellFormat(smallformat);
						tablesheet.addCell(cell);
						int CurLineHeight = getCurLineHeightByContents(startCloums,stopCloums,ReportData[i][f]);//通过当前单元格填写数据的行数计算出来行高！
						MaxLineHeight =(MaxLineHeight < CurLineHeight)?CurLineHeight:MaxLineHeight;
					}
					//设置行高
					if (DataLineHeight > 3500) {
						DataLineHeight = 3500;
					}
					//System.out.println("DataLineHeight =" + DataLineHeight);
					tablesheet.setRowView(rownum,MaxLineHeight);
				}//for(int i=0;i<loop;i++){
			}//if (ReportData[0].length == DataCloums) {
		}//if (ReportData != null) {
	}

	/**
	 * 填写数据
	 * @param tablesheet
	 * @throws Exception
	 */
	public void geneContentMerger(WritableSheet tablesheet) throws Exception {
		if (ReportData != null) {
			if (ReportData[0].length == DataCloums) {
				// 小字体设置
				WritableFont smallfont = new WritableFont(WritableFont.ARIAL,8,WritableFont.NO_BOLD);
				WritableCellFormat smallformat = new WritableCellFormat(smallfont);
				smallformat.setAlignment(jxl.format.Alignment.LEFT);// 把水平对齐方式指定为居中
				smallformat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
				smallformat.setWrap(true);
				smallformat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
				int loop = ReportData.length;
				for(int i=0;i<loop;i++){
					int rownum = TitleRows + i;
					rownum = MergerStartRow + rownum;
					int MaxLineHeight = 0;
					for(int f=0;f<DataCloums;f++){
						int startCloums = MergerColum[f];
						int stopCloums = MergerColum[f+1] - 1;
						//System.out.println("geneContentMerger f =" + f + ",startCloums =" + startCloums + ",stopCloums =" +stopCloums + ",rownum =" +rownum);
						tablesheet.mergeCells(startCloums,rownum,stopCloums,rownum);//出错了！
						WritableCell cell = new Label(startCloums,rownum,ReportData[i][f]);
						cell.setCellFormat(smallformat);
						tablesheet.addCell(cell);
						int CurLineHeight = getCurLineHeightByContents(startCloums,stopCloums,ReportData[i][f]);//通过当前单元格填写数据的行数计算出来行高！
						MaxLineHeight =(MaxLineHeight < CurLineHeight)?CurLineHeight:MaxLineHeight;
					}
					//设置行高
					if (DataLineHeight > 3500) {
						DataLineHeight = 3500;
					}
					//System.out.println("DataLineHeight =" + DataLineHeight);

					MergerStopRow = rownum;
					tablesheet.setRowView(rownum,MaxLineHeight);
				}//for(int i=0;i<loop;i++){
			}//if (ReportData[0].length == DataCloums) {
		}//if (ReportData != null) {
	}

	/**
	 * 填写数据
	 * @param tablesheet
	 * @throws Exception
	 */
	public void geneContentSheet() throws Exception {
		if (ReportData != null) {
			if (ReportData[0].length == DataCloums) {
				WritableSheet tablesheet = Sheetwwb.getSheet(SheetNumber);
				tablesheet.getSettings().setSelected(true);
				// 小字体设置
				WritableFont smallfont = new WritableFont(WritableFont.ARIAL,8,WritableFont.NO_BOLD);
				WritableCellFormat smallformat = new WritableCellFormat(smallfont);
				smallformat.setAlignment(jxl.format.Alignment.LEFT);// 把水平对齐方式指定为居中
				smallformat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
				smallformat.setWrap(true);
				smallformat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
				int loop = ReportData.length;
				for(int i=0;i<loop;i++){
					int rownum = SheetStartRow + i;
					int MaxLineHeight = 0;
					for(int f=0;f<DataCloums;f++){
						int startCloums = MergerColum[f];
						int stopCloums = MergerColum[f+1] - 1;
						//System.out.println("geneContentMerger f =" + f + ",startCloums =" + startCloums + ",stopCloums =" +stopCloums + ",rownum =" +rownum);
						tablesheet.mergeCells(startCloums,rownum,stopCloums,rownum);//出错了！
						WritableCell cell = new Label(startCloums,rownum,ReportData[i][f]);
						cell.setCellFormat(smallformat);
						tablesheet.addCell(cell);
						int CurLineHeight = getCurLineHeightByContents(startCloums,stopCloums,ReportData[i][f]);//通过当前单元格填写数据的行数计算出来行高！
						MaxLineHeight =(MaxLineHeight < CurLineHeight)?CurLineHeight:MaxLineHeight;
					}
					//设置行高
					if (DataLineHeight > 3500) {
						DataLineHeight = 3500;
					}
					//System.out.println("DataLineHeight =" + DataLineHeight);
					//MergerStopRow = rownum;
					tablesheet.setRowView(rownum,MaxLineHeight);
				}//for(int i=0;i<loop;i++){
			}//if (ReportData[0].length == DataCloums) {
		}//if (ReportData != null) {
	}

	private int getCurLineHeightByContents(int startCloums,int stopCloums,String contents) throws Exception{
		contents =(contents == null)?"":contents.trim();
		if(contents.length() == 0) return 225;
		int LineHeight = 0;
		int CellCount = (stopCloums - startCloums) + 1;
		int LineCount = CellCount * 2;//一个单元可以放 2 个英文字符，一个汉字！
		int LineLength = 0;
		int nloop = contents.length();
		for(int n=0;n<nloop;n++){
			String st = contents.substring(n, n+1);
			boolean isHanZi = com.coolservlets.beans.method.CheckMethod.isHanZi(st);
			if(isHanZi){
				LineLength = LineLength + 2;
			}else{
				LineLength = LineLength + 1;
			}
		}
		//System.out.println("contents =" + contents + ",LineLength =" + LineLength + ",LineCount =" + LineCount);

		int iLine = (LineLength % LineCount == 0)?LineLength / LineCount:LineLength / LineCount + 1;
		//System.out.println("contents =" + contents + ",iLine =" + iLine);
		LineHeight = iLine * 180;
		return LineHeight;
	}
	/**
	 * 填写数据
	 * @param tablesheet
	 * @throws Exception
	 */
	/**
    public void geneContent(WritableSheet tablesheet) throws Exception {
		if (ReportData != null) {
			if (ReportData[0].length == DataCloums) {
				// 小字体设置
				WritableFont smallfont = new WritableFont(WritableFont.ARIAL,8,WritableFont.NO_BOLD);
				WritableCellFormat smallformat = new WritableCellFormat(smallfont);
				smallformat.setAlignment(jxl.format.Alignment.LEFT);// 把水平对齐方式指定为居中
				smallformat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
				smallformat.setWrap(true);
				smallformat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
				int loop = ReportData.length;
				for(int i=0;i<loop;i++){
					int rownum = TitleRows + i;
					for(int f=0;f<DataCloums;f++){
						int startCloums = 0;
						int stopCloums = 0;
						int tloop = vModuleKey.size();
						for(int t=0;t<tloop;t++){
							String getkey = (String)vModuleKey.elementAt(t);
							CustCell custCell= (CustCell)HmReportModule.get(getkey);
							int dataCloum = custCell.getDataCloum();
							if(f == dataCloum ){
                                //获得当前单元格的起始列列号
								startCloums = custCell.getStartCloums();
								//获得当前单元格的终止列列号
								stopCloums = custCell.getStopCloums();
								break;
							}
						}
						tablesheet.mergeCells(startCloums,rownum,stopCloums,rownum);
						WritableCell cell = new Label(startCloums,rownum,ReportData[i][f]);
						cell.setCellFormat(smallformat);
						tablesheet.addCell(cell);
					}
                    //设置行高
					if (DataLineHeight > 3500) {
						DataLineHeight = 3500;
					}
					tablesheet.setRowView(rownum,DataLineHeight);
				}//for(int i=0;i<loop;i++){
			}//if (ReportData[0].length == DataCloums) {
		}//if (ReportData != null) {
	}
	 **/

	private HashMap createReportSheetModule() throws Exception{

		//WritableSheet sheet = wwb.getSheet(0);
		vModuleKey = new Vector();
		//int SheetNumber = 0;//模板的 sheet 页编号
		WritableSheet sheet = Sheetwwb.getSheet(SheetNumber);
		sheet.getSettings().setSelected(true);
		/**
    	ByteArrayInputStream  input=new ByteArrayInputStream(ExeclModule);
		WorkbookSettings workbookSettings=new WorkbookSettings();
		workbookSettings.setEncoding("ISO-8859-1");
		Workbook workbook=Workbook.getWorkbook(input,workbookSettings);
		Sheet sheet=workbook.getSheet(0);
		 **/
		//获取Sheet表中所包含的总列数
		int MaxColumns = sheet.getColumns();
		//System.out.println("MaxColumns =" + MaxColumns);
		//获取Sheet表中所包含的总行数，示例：
		int MaxRows = sheet.getRows();
		TitleRows = MaxRows;
		//System.out.println("MaxRows="+Integer.toString(MaxRows));
		//获得报表模板中的sheet 名字
		SheetName =(SheetName == null)?sheet.getName():SheetName;
		//开始计算分析模板了，算法要简单明了
		HashMap hmReport = new HashMap();
		//有效单元格的键值
		//Vector vKey = new Vector();
		//MaxRows = 1;

		//提前将报表模板与 数据标志行 分开 Begin 
		//if(curcontents.equalsIgnoreCase(covertag)) continue;
		int ModMaxRow  = MaxRows;
		for(int row=0;row<MaxRows;row++)
		{
			Cell[] cells=sheet.getRow(row);
			int cloop = cells.length;
			boolean DoIs = false;
			for(int j=0;j<cloop;j++)
			{
				Cell cellEach=cells[j];
				String contents = cellEach.getContents();
				contents =(contents == null)?"":contents.trim();
				if(contents.length() == 0) continue;
				String first = contents.substring(0, 1);
				if(first.equalsIgnoreCase(covertag)){//如果该单元格字符串的首字母，等于设置填写数据的 字符
					ModMaxRow = row;
					//MergerSartRow = row;
					SheetStartRow = row;
					DoIs = true;
					break;
				}
			}
			if(DoIs) {
				break;
			}
		}
		//System.out.println("MergerSartRow =" + MergerSartRow);
		//提前将报表模板与 数据标志行 分开 End

		//初步分析统计报表的格式 Begin
		for(int row=0;row<ModMaxRow;row++)
		{
			Cell[] cells=sheet.getRow(row);
			int cloop = cells.length;
			String lastkey = "";
			for(int j=0;j<cloop;j++)
			{
				//System.out.println("第几行 = "+ row + ", 第几列 = " + j + ", cloop =" +cloop);
				Cell cellEach=cells[j];

				String contents = cellEach.getContents();
				//System.out.println("第几行 = "+ row + ", 第几列 = " + j + ",contents = " +contents);
				contents =(contents == null)?"":contents.trim();
				if(contents.length() > 0){
					if(lastkey.length() > 0){
						int endColums = j -1;
						CustCell custCell= (CustCell)hmReport.get(lastkey);
						//将结束列分析出来
						String ts = custCell.getContents();
						custCell.setStopCloums(endColums);
						hmReport.put(lastkey,custCell);
						//System.out.println("contents="+ts+",endColums="+Integer.toString(endColums)+","+Integer.toString(row));
					}
					String putkey = Integer.toString(row) + "@@" + Integer.toString(j) + "@@";
					CustCell custCell= new CustCell();
					//设置起始列号
					custCell.setStartCloums(j);
					//设置起始行号
					custCell.setStartRows(row);
					//设置单元格内容
					custCell.setContents(contents);
					//设置单元格字体大小
					int fontsize = cellEach.getCellFormat().getFont().getPointSize();
					custCell.setFontSize(fontsize);
					//设置单元格加重程度
					int boldWeight = cellEach.getCellFormat().getFont().getBoldWeight();
					custCell.setBoldWeight(boldWeight);
					//设置批注
					if(isHasComment(cellEach))
					{
						String comment=cellEach.getCellFeatures().getComment().toString().trim();
						custCell.setComment(comment);
					}
					//System.out.println("contents 0="+contents+",endColums="+Integer.toString(j));
					hmReport.put(putkey,custCell);
					vModuleKey.addElement(new String(putkey));
					lastkey = putkey;
				}
			}
			if(lastkey.length() > 0){
				int endColums = MaxColumns -1;
				CustCell custCell= (CustCell)hmReport.get(lastkey);
				//将结束列分析出来
				String ts = custCell.getContents();
				custCell.setStopCloums(endColums);
				hmReport.put(lastkey,custCell);
				//System.out.println("contents end="+ts+",endColums="+Integer.toString(endColums)+","+Integer.toString(row));
			}
		}
		//初步分析统计报表的格式 End

		//开始分析该单元格是否是最底层的叶子单元格 Begin
		int loop = vModuleKey.size();
		Vector vLeaf = new Vector();
		//System.out.println("loop =" + loop);
		for(int i=0;i<loop;i++){
			String getkey = (String)vModuleKey.elementAt(i);
			CustCell custCell= (CustCell)hmReport.get(getkey);
			String curcontents = custCell.getContents();
			//System.out.println("getkey =" + getkey + ",curcontents = " +curcontents);
			//if(curcontents.equalsIgnoreCase(covertag)) continue;
			//获得当前单元格的起始行行号
			int startRows = custCell.getStartRows();
			//获得当前单元格的起始列列号
			int startCloums = custCell.getStartCloums();
			//获得当前单元格的终止列列号
			int stopCloums = custCell.getStopCloums();
			//if(curcontents.equalsIgnoreCase("-1.4回收率（%）≥") || curcontents.equalsIgnoreCase("+1.8")) {
			//	System.out.println("getkey =" + getkey + ",curcontents ="+curcontents+",startRows ="+Integer.toString(startRows) + ",startCloums =" + startCloums + ",stopCloums =" +stopCloums);
			//}
			boolean leafIs = true;
			int stopRows1 = startRows;
			for(int f=0;f<loop;f++){
				String findkey = (String)vModuleKey.elementAt(f);
				CustCell fcustCell= (CustCell)hmReport.get(findkey);
				//不是自己的单元格才比较
				if(!getkey.equalsIgnoreCase(findkey)){
					//获得当前单元格的起始行行号
					int fstartRows = fcustCell.getStartRows();
					//判断是否为下一行
					if(fstartRows >= startRows+1){
						//判断该单元格是否上一个单元格的子节点
						int fstartCloums = fcustCell.getStartCloums();
						if(fstartCloums >= startCloums && fstartCloums < stopCloums){
							//说明该单元格不是叶子节点
							stopRows1 = (leafIs)?fstartRows -1:stopRows1;
							leafIs = false;
							//System.out.println("fstartRows = " +fstartRows + ",curcontents =" +curcontents);
							//获得当前单元格的终止列列号
							int fstopCloums = fcustCell.getStopCloums();
							//说明是最后一个单元格，终止列号校正
							if(fstopCloums > stopCloums){
								fcustCell.setStopCloums(stopCloums);
								hmReport.put(findkey,fcustCell);
								String fcontents = custCell.getContents();
								String contents = fcustCell.getContents();
								//System.out.println("fcontents ="+fcontents+",contents="+contents+","+Integer.toString(stopCloums));
							}
							//stopRows1 = ( stopRows1 == 0)?fstartRows -1:stopRows1;
							//System.out.println("stopRows1 ="+stopRows1);
							//break;
						}
					}//startRows+1 == fstartRows
				}// !getkey.equalsIgnoreCase(findkey)
			}//for end
			//int stopRows = (leafIs)?MaxRows-1:startRows;
			int stopRows = (leafIs)?MaxRows-1:stopRows1;
			custCell.setStopRows(stopRows);
			custCell.setLeafIs(leafIs);
			hmReport.put(getkey,custCell);
			if(leafIs){
				String putstr = getkey + Integer.toString(startCloums)+"@@";
				vLeaf.addElement(new String(putstr));
				//System.out.println("leafIs putstr = " +putstr + ",curcontents =" +curcontents);
			}
		}
		//开始分析该单元格是否是最底层的叶子单元格 End

		//记录数据合并单元格的起始列号 Begin
		//MergerColum = new int[DataCloums+1];
		Vector vMergerColum = new Vector();
		Cell[] cells=sheet.getRow(ModMaxRow);
		int cloop = cells.length;
		int iIndex = 0;
		for(int j=0;j<cloop;j++)
		{
			Cell cellEach=cells[j];
			String contents = cellEach.getContents();
			contents =(contents == null)?"":contents.trim();
			if(contents.length() > 0){
				String first = contents.substring(0, 1);
				if(first.equalsIgnoreCase(covertag)){
					//MergerColum[iIndex] = j;
					//System.out.println("合并单元格的列号 =" + j);
					vMergerColum.addElement(new String(Integer.toString(j)));
					iIndex++;
				}
			}
		}
		MergerColum = new int[iIndex+1];
		for(int f=0;f<iIndex;f++){
			String st = (String)vMergerColum.elementAt(f);
			MergerColum[f] = Integer.parseInt(st);
		}
		MergerColum[iIndex] = MaxColumns;
		//System.out.println("MergerColum[iIndex] 合并单元格的列号 =" + MergerColum[iIndex]);
		//记录数据合并单元格的起始列号 End

		//对叶子单元格按照起始列进行排序 Begin
		vLeaf = genMethod.sortValueStr(vLeaf,"@@",2,"1",null,0);
		//DataCloums = vLeaf.size();
		DataCloums = iIndex;
		//System.out.println("DataCloums ="  + DataCloums);
		//System.out.println("vLeaf.size() ="  + vLeaf.size() + ",DataCloums =" + DataCloums);
		for(int i=0;i<DataCloums;i++){
			String ts = (String)vLeaf.elementAt(i);
			String s0 = genMethod.readValueStr(ts,"@@",0);
			String s1 = genMethod.readValueStr(ts,"@@",1);
			String key = s0 + "@@" + s1 + "@@";
			CustCell custCell= (CustCell)hmReport.get(key);
			custCell.setDataCloum(i);
			hmReport.put(key,custCell);
		}
		//对叶子单元格按照起始列进行排序 End

		//int tloop = MergerColum.length;
		//for(int t=0;t<tloop;t++){
		//	System.out.println("合并单元格的列号  =" + MergerColum[t] + ",t = " + t + ",MaxColumns =" + MaxColumns + ",DataCloums =" +DataCloums);
		//}
		return hmReport;
	}

	/**
	 * 为单个报表（单一表头）输出的完整例子
	 * @param args
	 */
	public static void main00(String[] args) {
		try {
			ExcelReportMerger excelReportMerger = new ExcelReportMerger();
			//自定义 数据行标志
			excelReportMerger.setCovertag("*");
			//自定义sheet 名
			excelReportMerger.setSheetName("内转四股泉原煤");
			//创建报表模板的二进制文件流
			//java.io.File modulefile=new java.io.File("F:\\sqlrun\\bfny\\内转四股泉原煤综合.xls");
			java.io.File modulefile=new java.io.File("F:\\sqlrun\\bfny\\内转四股泉原煤.xls");
			FileInputStream fin=new FileInputStream(modulefile);
			byte [] excelModuleBytes=new byte[fin.available()];
			fin.read(excelModuleBytes);
			excelReportMerger.setModuleBytes(excelModuleBytes);
			//设置表头说明
			int iMonth = new GregorianCalendar().get(Calendar.MONTH)+1;
			String title = new GregorianCalendar().get(Calendar.YEAR) + " 年 "+ iMonth + " 月 " + " 供应商暂估明细";
			excelReportMerger.setReportTitle(title);
			//构建报表的数据
			String[][] reportdata = new String[10][23];
			reportdata[0][0] = "2017-06-10";
			reportdata[0][1] = "北京普立科企业管理咨询有限公司";
			excelReportMerger.setReportData(reportdata);
			//设置数据打印内容的字体大小
			excelReportMerger.setDataFontSize(9);
			//打印数据行内容行的行高
			excelReportMerger.setDataLineHeight(500);
			//自动生成需要打印的报表
			byte [] excelReportBytes = excelReportMerger.createExcelReport();
			//开始用固定值 替换 变量值 Begin
			String[] values = {"2017-06-10"}; 
			excelReportBytes = replaceExcelModuleTitle(excelReportBytes,values,"$DATE");
			//开始用固定值 替换 变量值 End

			//System.out.println(excelReportBytes.length);
			//File outFile = new File("F:\\sqlrun\\bfny\\内转四股泉原煤综合-新.xls");
			File outFile = new File("F:\\sqlrun\\bfny\\内转四股泉原煤-新.xls");
			FileOutputStream fos = new FileOutputStream(outFile);
			fos.write(excelReportBytes);
			fos.close();
		}catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 为多个报表（多个表头）的每个 sheet ，每个 sheet 作为一张报表，不进行合并！
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			//构建要合并成唯一的报表 Sheet Begin
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			// 设置WorkbookSettings，获得excel文件中的工作薄
			WorkbookSettings wss = new WorkbookSettings();
			wss.setEncoding("iso-8859-1");
			WritableWorkbook workbook = Workbook.createWorkbook((OutputStream) baos, wss);
			// 获得工作薄的数量
			WritableSheet tablesheet = workbook.createSheet("集团质量日报",0);
			for (int i = 0; i < 100; i++) 
			{
				tablesheet.setColumnView(i, 3);
			}
			tablesheet.getSettings().setPrintTitlesRow(0, 2);
			tablesheet.getSettings().setShowGridLines(false);
			//构建要合并成唯一的报表 Sheet End
			//创建报表模板的二进制文件流
			//java.io.File modulefile=new java.io.File("F:\\sqlrun\\bfny\\集团报表模板20170617.xls");
			java.io.File modulefile=new java.io.File("F:\\sqlrun\\bfny\\集团报表模板20171111.xls");
			FileInputStream fin=new FileInputStream(modulefile);
			byte [] excelModuleBytes=new byte[fin.available()];
			fin.read(excelModuleBytes);

			//编码后写入另外一个文件 Begin
			ByteArrayInputStream  winput=new ByteArrayInputStream(excelModuleBytes);
			WorkbookSettings wworkbookSettings=new WorkbookSettings();
			wworkbookSettings.setEncoding("ISO-8859-1");
			jxl.Workbook rw = jxl.Workbook.getWorkbook(winput,wworkbookSettings);
			ByteArrayOutputStream fbaos = new ByteArrayOutputStream();
			jxl.write.WritableWorkbook wwb = Workbook.createWorkbook((OutputStream) fbaos,rw);
			Sheet[] sheets = wwb.getSheets();
			int mloop = sheets.length;
			//编码后写入另外一个文件 End

			//构建第一个单个报表 Begin
			ExcelReportSheets excelReportSheets = new ExcelReportSheets();
			//设置读取模板 excel
			excelReportSheets.setModuleBytes(excelModuleBytes);

			//设置表头说明
			int iMonth = new GregorianCalendar().get(Calendar.MONTH)+1;
			String title = new GregorianCalendar().get(Calendar.YEAR) + " 年 "+ iMonth + " 月 " + " 宁夏宝丰集团质量日报";
			excelReportSheets.setReportTitle(title);
			String[] values = {"2018-04-11"}; 
			excelReportSheets.replaceExcelModuleTitle(0,values,"$DATE");
			for(int i = 0; i < mloop; i++)
			{
				Sheet fsheet = wwb.getSheet(i); // 一个工作表
				String SheetName = fsheet.getName();

				//设置读取模板的 sheet 页编号 Begin
				excelReportSheets.setSheetNumber(i);

				//自定义 数据行标志
				excelReportSheets.setCovertag("*");

				//对某个 Sheet 的模板进行分析 Begin
				excelReportSheets.AnalysisSheetBytes();

				//获得该模板下面的 数据记录的 有多少列？Begin
				int DataColums = excelReportSheets.getDataCloums();
				System.out.println("SheetName =" + SheetName + ",SheetNumber =" + Integer.toString(i) + ",DataColums =" + DataColums);
				String[][] reportdata = new String[10][DataColums];
				reportdata[0][0] = "2018-04-11";
				reportdata[0][1] = "北京普立科企业管理咨询有限公司";
				excelReportSheets.setReportData(reportdata);
				//设置数据打印内容的字体大小
				excelReportSheets.setDataFontSize(9);
				//打印数据行内容行的行高
				excelReportSheets.setDataLineHeight(500);
				//自动生成需要打印的报表的 Sheet
				excelReportSheets.createExcelReportSheet();//
			}
			byte [] ExcelReport = excelReportSheets.getSheetReportExcel();
			File outFile = new File("F:\\sqlrun\\bfny\\集团报表模板20171111-鑫鑫.xls");
			FileOutputStream fos = new FileOutputStream(outFile);
			fos.write(ExcelReport);
			fos.close();

		}catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 为多个报表（多个表头）合并在一个excel 报表输出的完整例子，模板为多个 sheet 对应的一个子报表！
	 * @param args
	 */
	public static void main1(String[] args) {
		try {
			//构建要合并成唯一的报表 Sheet Begin
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			// 设置WorkbookSettings，获得excel文件中的工作薄
			WorkbookSettings wss = new WorkbookSettings();
			wss.setEncoding("iso-8859-1");
			WritableWorkbook workbook = Workbook.createWorkbook((OutputStream) baos, wss);
			// 获得工作薄的数量
			WritableSheet tablesheet = workbook.createSheet("集团质量日报",0);
			for (int i = 0; i < 100; i++) 
			{
				tablesheet.setColumnView(i, 3);
			}
			tablesheet.getSettings().setPrintTitlesRow(0, 2);
			tablesheet.getSettings().setShowGridLines(false);
			//构建要合并成唯一的报表 Sheet End
			//创建报表模板的二进制文件流
			//java.io.File modulefile=new java.io.File("F:\\sqlrun\\bfny\\集团报表模板20170617.xls");
			java.io.File modulefile=new java.io.File("F:\\sqlrun\\bfny\\集团报表模板20171111.xls");
			FileInputStream fin=new FileInputStream(modulefile);
			byte [] excelModuleBytes=new byte[fin.available()];
			fin.read(excelModuleBytes);

			//编码后写入另外一个文件 Begin
			ByteArrayInputStream  winput=new ByteArrayInputStream(excelModuleBytes);
			WorkbookSettings wworkbookSettings=new WorkbookSettings();
			wworkbookSettings.setEncoding("ISO-8859-1");
			jxl.Workbook rw = jxl.Workbook.getWorkbook(winput,wworkbookSettings);
			ByteArrayOutputStream fbaos = new ByteArrayOutputStream();
			jxl.write.WritableWorkbook wwb = Workbook.createWorkbook((OutputStream) fbaos,rw);
			Sheet[] sheets = wwb.getSheets();
			int mloop = sheets.length;
			//编码后写入另外一个文件 End

			int ReportStopRow = -1;//上个报表结束行！
			//nloop = 2;
			//mloop = 9;
			for(int i = 0; i < mloop; i++)
			{
				//if(i !=0 && i !=8) continue;
				//构建第一个单个报表 Begin
				ExcelReportMerger excelReportMerger = new ExcelReportMerger();
				//设置 多个excel 合并为一个 excel 的 sheet
				excelReportMerger.setMergerSheet(tablesheet);
				//多个excel 合并为一个 excel 的 sheet 上创建 新行的起始行号
				excelReportMerger.setMergerStartRow(ReportStopRow +1);
				//自定义 数据行标志
				excelReportMerger.setCovertag("*");
				//自定义sheet 名
				excelReportMerger.setSheetName("集团质量日报");
				//设置读取模板的 sheet 页编号
				excelReportMerger.setSheetNumber(i);
				//设置读取模板 excel
				excelReportMerger.setModuleBytes(excelModuleBytes);
				//设置表头说明
				int iMonth = new GregorianCalendar().get(Calendar.MONTH)+1;
				String title = new GregorianCalendar().get(Calendar.YEAR) + " 年 "+ iMonth + " 月 " + " 宁夏宝丰集团质量日报";
				excelReportMerger.setReportTitle(title);
				//获得该模板下面的 数据记录的 有多少列？
				int DataColums = excelReportMerger.getDataCloums();

				Sheet fsheet = wwb.getSheet(i); // 一个工作表
				String SheetName = fsheet.getName();
				System.out.println("SheetName =" + SheetName + ",SheetNumber =" + Integer.toString(i) + ",DataColums= " +DataColums);
				//取出模板中读取数据的 标记行 Begin
				int MergerSartRow = excelReportMerger.getMergerSartRow();
				String[][] reportdata = new String[10][DataColums];
				reportdata[0][0] = "2018-04-11";
				reportdata[0][1] = "北京普立科企业管理咨询有限公司";
				excelReportMerger.setReportData(reportdata);
				//构建报表的数据 End

				//设置数据打印内容的字体大小
				excelReportMerger.setDataFontSize(9);
				//打印数据行内容行的行高
				excelReportMerger.setDataLineHeight(500);
				//自动生成需要打印的报表的 Sheet
				//byte [] excelReportBytes = excelReportMerger.createExcelReport();
				tablesheet = excelReportMerger.createExcelReportMerger();//
				ReportStopRow = excelReportMerger.getMergerStopRow();
			}
			//合并完成 开始构建合并后的报表 Begin
			workbook.write();
			workbook.close();
			byte [] ExcelReport = baos.toByteArray();
			//开始用固定值 替换 变量值 Begin
			String[] values = {"2018-04-11" + " 内转四股泉原煤"}; 
			ExcelReport = ExcelReportMerger.replaceExcelModuleTitle(ExcelReport,values,"$DATE");
			File outFile = new File("F:\\sqlrun\\bfny\\集团报表模板20171111-鑫鑫.xls");
			FileOutputStream fos = new FileOutputStream(outFile);
			fos.write(ExcelReport);
			fos.close();
		}catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	/**
	 * 为多个报表（多个表头）合并在一个excel 报表输出的完整例子，模板为多个 sheet 对应的一个子报表！
	 * @param args
	 */
	public static void main0000(String[] args) {
		try {
			//构建要合并成唯一的报表 Sheet Begin
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			// 设置WorkbookSettings，获得excel文件中的工作薄
			WorkbookSettings wss = new WorkbookSettings();
			wss.setEncoding("iso-8859-1");
			WritableWorkbook workbook = Workbook.createWorkbook((OutputStream) baos, wss);
			// 获得工作薄的数量
			WritableSheet tablesheet = workbook.createSheet("集团质量日报",0);
			for (int i = 0; i < 100; i++) 
			{
				tablesheet.setColumnView(i, 3);
			}
			tablesheet.getSettings().setPrintTitlesRow(0, 2);
			//构建要合并成唯一的报表 Sheet End
			//创建报表模板的二进制文件流
			java.io.File modulefile=new java.io.File("F:\\sqlrun\\bfny\\集团报表模板20170612.xls");
			FileInputStream fin=new FileInputStream(modulefile);
			byte [] excelModuleBytes=new byte[fin.available()];
			fin.read(excelModuleBytes);

			//构建第一个单个报表 Begin
			ExcelReportMerger excelReportMerger = new ExcelReportMerger();
			//setMergerSheet(WritableSheet vMergerSheet)
			//设置 多个excel 合并为一个 excel 的 sheet
			excelReportMerger.setMergerSheet(tablesheet);
			//多个excel 合并为一个 excel 的 sheet 上创建 新行的起始行号
			excelReportMerger.setMergerStartRow(0);
			//自定义 数据行标志
			excelReportMerger.setCovertag("*");
			//自定义sheet 名
			excelReportMerger.setSheetName("集团质量日报");
			//excelReportMerger.setm
			//设置读取模板的 sheet 页编号
			excelReportMerger.setSheetNumber(0);

			excelReportMerger.setModuleBytes(excelModuleBytes);
			//设置表头说明
			int iMonth = new GregorianCalendar().get(Calendar.MONTH)+1;
			String title = new GregorianCalendar().get(Calendar.YEAR) + " 年 "+ iMonth + " 月 " + " 供应商暂估明细";
			excelReportMerger.setReportTitle(title);
			//构建报表的数据
			String[][] reportdata = new String[10][24];
			reportdata[0][0] = "2017-06-10";
			reportdata[0][1] = "北京普立科企业管理咨询有限公司";
			excelReportMerger.setReportData(reportdata);
			//设置数据打印内容的字体大小
			excelReportMerger.setDataFontSize(9);
			//打印数据行内容行的行高
			excelReportMerger.setDataLineHeight(500);
			//自动生成需要打印的报表的 Sheet
			//byte [] excelReportBytes = excelReportMerger.createExcelReport();
			WritableSheet excelReportSheet = excelReportMerger.createExcelReportMerger();
			int ReportStopRow = excelReportMerger.getMergerStopRow();
			System.out.println("ReportStopRow =" +ReportStopRow);
			//构建第一个单个报表 End

			//构建第二个单个报表 Begin
			ExcelReportMerger excelReportMerger1 = new ExcelReportMerger();
			//setMergerSheet(WritableSheet vMergerSheet)
			//设置 多个excel 合并为一个 excel 的 sheet
			excelReportMerger1.setMergerSheet(excelReportSheet);
			//多个excel 合并为一个 excel 的 sheet 上创建 新行的起始行号
			excelReportMerger1.setMergerStartRow(ReportStopRow+1);
			//自定义 数据行标志
			excelReportMerger1.setCovertag("*");
			//自定义sheet 名
			excelReportMerger1.setSheetName("集团质量日报");
			//创建报表模板的二进制文件流
			//java.io.File modulefile1=new java.io.File("F:\\sqlrun\\bfny\\内转四股泉原煤综合.xls");
			//FileInputStream fin1=new FileInputStream(modulefile1);
			//byte [] excelModuleBytes1=new byte[fin1.available()];
			//fin1.read(excelModuleBytes1);
			//设置读取模板的 sheet 页编号
			excelReportMerger1.setSheetNumber(1);
			excelReportMerger1.setModuleBytes(excelModuleBytes);
			excelReportMerger1.setReportTitle(title);
			//构建报表的数据
			String[][] reportdata1 = new String[10][16];
			reportdata1[0][0] = "2017-06-10";
			reportdata1[0][1] = "北京普立科企业管理咨询有限公司";
			excelReportMerger1.setReportData(reportdata1);
			//设置数据打印内容的字体大小
			excelReportMerger1.setDataFontSize(9);
			//打印数据行内容行的行高
			excelReportMerger1.setDataLineHeight(500);
			//自动生成需要打印的报表的 Sheet
			//byte [] excelReportBytes = excelReportMerger.createExcelReport();
			WritableSheet excelReportSheet1 = excelReportMerger1.createExcelReportMerger();
			int ReportStopRow1 = excelReportMerger1.getMergerStopRow();
			System.out.println("ReportStopRow1 =" +ReportStopRow1);
			//构建第二个单个报表 End

			//合并完成 开始构建合并后的报表 Begin
			workbook.write();
			workbook.close();
			byte [] excelReportBytes = baos.toByteArray();
			//开始用固定值 替换 变量值 Begin
			String[] values = {"2017-06-10","内转四股泉原煤综合"}; 
			excelReportBytes = replaceExcelModuleTitle(excelReportBytes,values,"$DATE");
			//开始用固定值 替换 变量值 End
			//System.out.println(excelReportBytes.length);
			File outFile = new File("F:\\sqlrun\\bfny\\集团报表模板20170612-鑫鑫.xls");
			FileOutputStream fos = new FileOutputStream(outFile);
			fos.write(excelReportBytes);
			fos.close();
			//合并完成 开始构建合并后的报表 Begin
		}catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	/**
	 * 为多个报表（多个表头）合并在一个excel 报表输出的完整例子
	 * @param args
	 */
	public static void main000(String[] args) {
		try {
			//构建要合并成唯一的报表 Sheet Begin
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			// 设置WorkbookSettings，获得excel文件中的工作薄
			WorkbookSettings wss = new WorkbookSettings();
			wss.setEncoding("iso-8859-1");
			WritableWorkbook workbook = Workbook.createWorkbook((OutputStream) baos, wss);
			// 获得工作薄的数量
			WritableSheet tablesheet = workbook.createSheet("集团质量日报",0);
			for (int i = 0; i < 100; i++) 
			{
				tablesheet.setColumnView(i, 3);
			}
			tablesheet.getSettings().setPrintTitlesRow(0, 2);
			//构建要合并成唯一的报表 Sheet End

			//构建第一个单个报表 Begin
			ExcelReportMerger excelReportMerger = new ExcelReportMerger();
			//setMergerSheet(WritableSheet vMergerSheet)
			//设置 多个excel 合并为一个 excel 的 sheet
			excelReportMerger.setMergerSheet(tablesheet);
			//多个excel 合并为一个 excel 的 sheet 上创建 新行的起始行号
			excelReportMerger.setMergerStartRow(0);
			//自定义 数据行标志
			excelReportMerger.setCovertag("*");
			//自定义sheet 名
			excelReportMerger.setSheetName("集团质量日报");
			//创建报表模板的二进制文件流
			java.io.File modulefile=new java.io.File("F:\\sqlrun\\bfny\\内转四股泉原煤.xls");
			FileInputStream fin=new FileInputStream(modulefile);
			byte [] excelModuleBytes=new byte[fin.available()];
			fin.read(excelModuleBytes);
			excelReportMerger.setModuleBytes(excelModuleBytes);
			//设置表头说明
			int iMonth = new GregorianCalendar().get(Calendar.MONTH)+1;
			String title = new GregorianCalendar().get(Calendar.YEAR) + " 年 "+ iMonth + " 月 " + " 供应商暂估明细";
			excelReportMerger.setReportTitle(title);
			//构建报表的数据
			String[][] reportdata = new String[10][23];
			reportdata[0][0] = "2017-06-10";
			reportdata[0][1] = "北京普立科企业管理咨询有限公司";
			excelReportMerger.setReportData(reportdata);
			//设置数据打印内容的字体大小
			excelReportMerger.setDataFontSize(9);
			//打印数据行内容行的行高
			excelReportMerger.setDataLineHeight(500);
			//自动生成需要打印的报表的 Sheet
			//byte [] excelReportBytes = excelReportMerger.createExcelReport();
			WritableSheet excelReportSheet = excelReportMerger.createExcelReportMerger();
			int ReportStopRow = excelReportMerger.getMergerStopRow();
			//System.out.println("ReportStopRow =" +ReportStopRow);
			//构建第一个单个报表 End

			//构建第二个单个报表 Begin
			ExcelReportMerger excelReportMerger1 = new ExcelReportMerger();
			//setMergerSheet(WritableSheet vMergerSheet)
			//设置 多个excel 合并为一个 excel 的 sheet
			excelReportMerger1.setMergerSheet(excelReportSheet);
			//多个excel 合并为一个 excel 的 sheet 上创建 新行的起始行号
			excelReportMerger1.setMergerStartRow(ReportStopRow+1);
			//自定义 数据行标志
			excelReportMerger1.setCovertag("**");
			//自定义sheet 名
			excelReportMerger1.setSheetName("集团质量日报");
			//创建报表模板的二进制文件流
			java.io.File modulefile1=new java.io.File("F:\\sqlrun\\bfny\\内转四股泉原煤综合.xls");
			FileInputStream fin1=new FileInputStream(modulefile1);
			byte [] excelModuleBytes1=new byte[fin1.available()];
			fin1.read(excelModuleBytes1);
			excelReportMerger1.setModuleBytes(excelModuleBytes1);
			excelReportMerger1.setReportTitle(title);
			//构建报表的数据
			String[][] reportdata1 = new String[10][16];
			reportdata1[0][0] = "2017-06-10";
			reportdata1[0][1] = "北京普立科企业管理咨询有限公司";
			excelReportMerger1.setReportData(reportdata1);
			//设置数据打印内容的字体大小
			excelReportMerger1.setDataFontSize(9);
			//打印数据行内容行的行高
			excelReportMerger1.setDataLineHeight(500);
			//自动生成需要打印的报表的 Sheet
			//byte [] excelReportBytes = excelReportMerger.createExcelReport();
			WritableSheet excelReportSheet1 = excelReportMerger1.createExcelReportMerger();
			int ReportStopRow1 = excelReportMerger1.getMergerStopRow();
			System.out.println("ReportStopRow1 =" +ReportStopRow1);
			//构建第二个单个报表 End

			//合并完成 开始构建合并后的报表 Begin
			workbook.write();
			workbook.close();
			byte [] excelReportBytes = baos.toByteArray();
			//开始用固定值 替换 变量值 Begin
			String[] values = {"2017-06-10","内转四股泉原煤综合"}; 
			excelReportBytes = replaceExcelModuleTitle(excelReportBytes,values,"$DATE");
			//开始用固定值 替换 变量值 End
			//System.out.println(excelReportBytes.length);
			File outFile = new File("F:\\sqlrun\\bfny\\内转四股泉原煤-新新.xls");
			FileOutputStream fos = new FileOutputStream(outFile);
			fos.write(excelReportBytes);
			fos.close();
			//合并完成 开始构建合并后的报表 Begin
		}catch (Exception ex) {
			ex.printStackTrace();
		}


	}

	public static void maintest01(String[] args) {
		//TODO Auto-generated method stub
		try {   
			File inFile = new File("D:\\sqlrun\\app\\report\\excelreport.xls");
			File outFile = new File("D:\\sqlrun\\app\\report\\excelreportnew.xls");
			FileInputStream fis = new FileInputStream(inFile);
			byte [] bytes=new byte[fis.available()];
			fis.read(bytes); 
			String str = new String(bytes,"iso8859-1");
			//byte [] cbytes = "明细说明".getBytes("iso8859-1");
			byte [] cbytes = "data[0]<".getBytes("iso8859-1");
			String cstr = new String(cbytes,"iso8859-1");
			System.out.println(cstr);
			if(str.indexOf(cstr,0) > -1){
				System.out.println(cstr);
				str = com.coolservlets.beans.method.genMethod.replaceValueStr(str,"data[0]<","明细说明[0]<");
			}
			FileOutputStream fos = new FileOutputStream(outFile);
			fos.write(str.getBytes("iso8859-1"));
			fos.close();
			fis.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace(); 
		}   
	}
	/**
	 * 由一个已经创建的电子表格模板，创建新的电子表格，并替换其中的单元格内容
	 * @param args
	 */
	public static void main05(String[] args) {
		//TODO Auto-generated method stub   
		try { 
			//byte[] contentBytes = null;
			//ByteArrayInputStream  input=new ByteArrayInputStream(contentBytes);

			File inFile = new File("D:\\sqlrun\\app\\report\\excelreport.xls");
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			File outFile = new File("D:\\sqlrun\\app\\report\\excelreportnew.xls");

			//jxl.Workbook rw = jxl.Workbook.getWorkbook(input);
			jxl.Workbook rw = jxl.Workbook.getWorkbook(inFile);
			jxl.write.WritableWorkbook wwb = Workbook.createWorkbook((OutputStream) baos,rw);
			jxl.write.WritableSheet ws = wwb.getSheet(0);
			jxl.write.WritableCell wc = ws.getWritableCell(1,0);
			//判断单元格的类型, 做出相应的转化
			if(wc.getType() == CellType.LABEL){
				Label l = (Label)wc;
				l.setString("替换字符内容");
			}
			//写入Excel对象
			wwb.write();
			//关闭可写入的Excel对象
			wwb.close();
			//关闭只读的Excel对象
			rw.close();
			FileOutputStream fos = new FileOutputStream(outFile);
			fos.write(baos.toByteArray());
			fos.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace(); 
		}   
	}
	public static void main07(String[] args) {
		try {
			//创建报表模板的二进制文件流
			java.io.File modulefile=new java.io.File("D:\\sqlrun\\app\\report\\excelreport.xls");
			FileInputStream fin=new FileInputStream(modulefile);
			byte [] excelModuleBytes=new byte[fin.available()];
			fin.read(excelModuleBytes);
			String[] value = new String[1];
			value[0] = "2010-3-13";
			excelModuleBytes = replaceModuleByValue(excelModuleBytes,value);
			File outFile = new File("D:\\sqlrun\\app\\report\\excelreportnew.xls");
			FileOutputStream fos = new FileOutputStream(outFile);
			fos.write(excelModuleBytes);
			fos.close();
		}catch (Exception ex) {
			ex.printStackTrace();
		}


	}
	public static void main08(String[] args) {
		try {
			ExcelReport excelReport = new ExcelReport();
			//构建模板文件
			excelReport.setBytesExcelModule("ImpTaskMonth.xls");
			String aligt[][] = {{"0","CENTER"}};
			String[][] data = {{"1","厂长办公室","","厂部重点任务","做好厂部重点工作的督察工作。","2012-06-30","0",""},{"1","厂长办公室","","厂部重点任务","做好厂部重点工作的督察工作。","2012-06-30","0",""}};
			byte[] ExcelDataReport = excelReport.getCollectBytes(data,"3,0;",aligt);
			System.out.println("ExcelDataReport size="+ExcelDataReport.length);
		}catch (Exception ex) {
			ex.printStackTrace();
		}


	}
	/**
	 * 用值替换模板中有固定标志的值（***）
	 * @param contentBytes :二进制文档模板文件
	 * @param value：替换模板中固定标志的值
	 * @return
	 */
	public static byte[] replaceModuleByValue(byte[] contentBytes,String[] value) {
		try { 
			ByteArrayInputStream  hinput=new ByteArrayInputStream(contentBytes);
			jxl.Workbook wb = jxl.Workbook.getWorkbook(hinput);
			//Workbook wb = Workbook.getWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\ImpTaskMonth.xls"));
			WorkbookSettings settings = new WorkbookSettings ();
			settings.setWriteAccess(null);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			//WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\DataImpTaskMonth.xls"),wb,settings);
			WritableWorkbook wwb = Workbook.createWorkbook((OutputStream)baos,wb,settings);
			WritableSheet ws = wwb.getSheet(0);
			ws.getSettings().setSelected(true);
			//获取Sheet表中所包含的总列数
			int MaxColumns = ws.getColumns();
			//获取Sheet表中所包含的总行数，示例：
			int MaxRows = ws.getRows();
			int iR = 0;
			for(int row=0;row<MaxRows;row++)
			{
				for(int j=0;j<MaxColumns;j++)
				{
					jxl.write.WritableCell wc = ws.getWritableCell(j,row);
					//判断单元格的类型, 做出相应的转化
					if(wc.getType() == CellType.LABEL){
						Label l = (Label)wc;
						String ls = l.getString();
						if(ls.equalsIgnoreCase("***")){
							l.setString(value[iR]);
							iR++;
						}
					}
				}
			}
			//写入Excel对象
			wwb.write();
			//关闭可写入的Excel对象
			wwb.close();
			//关闭只读的Excel对象
			wb.close();
			return baos.toByteArray();

		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace();
			return null;
		} 
	}

	/**
	 * 用值替换模板中有固定标志的值（***）
	 * @param contentBytes :二进制文档模板文件
	 * @param value：替换模板中固定标志的值
	 * @return
	 */
	public static byte[] replaceExcelModuleTitle(byte[] contentBytes,String[] value,String flag) {
		try {
			ByteArrayInputStream  hinput=new ByteArrayInputStream(contentBytes);
			jxl.Workbook wb = jxl.Workbook.getWorkbook(hinput);
			//Workbook wb = Workbook.getWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\ImpTaskMonth.xls"));
			WorkbookSettings settings = new WorkbookSettings ();
			settings.setWriteAccess(null);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			//WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\DataImpTaskMonth.xls"),wb,settings);
			WritableWorkbook wwb = Workbook.createWorkbook((OutputStream)baos,wb,settings);
			WritableSheet ws = wwb.getSheet(0);
			ws.getSettings().setSelected(true);
			//获取Sheet表中所包含的总列数
			int MaxColumns = ws.getColumns();
			//获取Sheet表中所包含的总行数，示例：
			int MaxRows = ws.getRows();
			int iR = 0;
			for(int row=0;row<MaxRows;row++)
			{
				for(int j=0;j<MaxColumns;j++)
				{
					jxl.write.WritableCell wc = ws.getWritableCell(j,row);
					//判断单元格的类型, 做出相应的转化
					if(wc.getType() == CellType.LABEL){
						Label l = (Label)wc;
						String ls = l.getString();
						if(ls.indexOf(flag,0) > -1){
							String st = genMethod.replaceValueStr(ls,flag,value[iR]);
							l.setString(st);
							iR++;
						}
					}
				}
			}
			//写入Excel对象
			wwb.write();
			//关闭可写入的Excel对象
			wwb.close();
			//关闭只读的Excel对象
			wb.close();
			return baos.toByteArray();

		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace();
			return null;
		} 
	}
	/**
	 * 用值替换模板中有固定标志的值（***）
	 * @param contentBytes :二进制文档模板文件
	 * @param value：替换模板中固定标志的值
	 * @return
	 */
	public static byte[] replaceModuleByValue(byte[] contentBytes,String[] value,String modflag) {
		try { 
			ByteArrayInputStream  hinput=new ByteArrayInputStream(contentBytes);
			jxl.Workbook wb = jxl.Workbook.getWorkbook(hinput);
			//Workbook wb = Workbook.getWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\ImpTaskMonth.xls"));
			WorkbookSettings settings = new WorkbookSettings ();
			settings.setWriteAccess(null);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			//WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\DataImpTaskMonth.xls"),wb,settings);
			WritableWorkbook wwb = Workbook.createWorkbook((OutputStream)baos,wb,settings);
			WritableSheet ws = wwb.getSheet(0);
			ws.getSettings().setSelected(true);
			//开始执行
			String[] aRow = genMethod.split(modflag,";");
			int loop = aRow.length;
			for(int i=0;i<loop;i++){
				String rows = aRow[i];
				String[] lArray = genMethod.split(rows,",");
				jxl.write.WritableCell wc = ws.getWritableCell(Integer.parseInt(lArray[1]),Integer.parseInt(lArray[0]));
				//判断单元格的类型, 做出相应的转化
				if(wc.getType() == CellType.LABEL){
					Label l = (Label)wc;
					l.setString(value[i]);
				}
			}
			//写入Excel对象
			wwb.write();
			//关闭可写入的Excel对象
			wwb.close();
			//关闭只读的Excel对象
			wb.close();
			return baos.toByteArray();

		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace();
			return null;
		} 
	}
	/**
	 * 用值替换模板中有固定标志的值（***）
	 * @param contentBytes :二进制文档模板文件
	 * @param value：替换模板中固定标志的值
	 * @return
	 */
	public static byte[] getCollectBytes(byte[] contentBytes,Vector vValues,String startLoc) {
		try { 
			ByteArrayInputStream  hinput=new ByteArrayInputStream(contentBytes);
			jxl.Workbook wb = jxl.Workbook.getWorkbook(hinput);
			//Workbook wb = Workbook.getWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\ImpTaskMonth.xls"));
			WorkbookSettings settings = new WorkbookSettings ();
			settings.setWriteAccess(null);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			//WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\DataImpTaskMonth.xls"),wb,settings);
			WritableWorkbook wwb = Workbook.createWorkbook((OutputStream)baos,wb,settings);
			WritableSheet ws = wwb.getSheet(0);
			ws.getSettings().setSelected(true);
			//小字体设置
			WritableFont smallfont = new WritableFont(WritableFont.ARIAL,9,WritableFont.NO_BOLD);
			WritableCellFormat smallformat = new WritableCellFormat(smallfont);
			smallformat.setAlignment(jxl.format.Alignment.LEFT);// 把水平对齐方式指定为居中
			smallformat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
			smallformat.setWrap(true);
			smallformat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
			String[] aStart =genMethod.split(startLoc,";");
			String[] fStart = genMethod.split(aStart[0],",");
			int iStartRow = Integer.parseInt(fStart[0]);
			int iStartCol = Integer.parseInt(fStart[1]);
			int loop = vValues.size();
			for(int i=0;i<loop;i++){
				String ls = (String)vValues.elementAt(i);
				String[] aValues = genMethod.split(ls,"@@");
				int nloop = aValues.length;
				for(int j=0;j<nloop;j++){
					WritableCell cell = new Label(iStartCol+j,iStartRow+i,aValues[j]);
					cell.setCellFormat(smallformat);
					ws.addCell(cell);
				}
			}
			//写入Excel对象
			wwb.write();
			//关闭可写入的Excel对象
			wwb.close();
			//关闭只读的Excel对象
			wb.close();
			return baos.toByteArray();

		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace();
			return null;
		} 
	}

	/**
	 * 将数据赋给模板文件中，并生成打印的Excel 文件
	 * @param contentBytes :二进制文档模板文件
	 * @param value：替换模板中固定标志的值
	 * @startLoc 开始打印的起始行列
	 * @return
	 */
	public static byte[] getCollectBytes(byte[] contentBytes,String[][] aValues,String startLoc) {
		try { 
			ByteArrayInputStream  hinput=new ByteArrayInputStream(contentBytes);
			jxl.Workbook wb = jxl.Workbook.getWorkbook(hinput);
			//Workbook wb = Workbook.getWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\ImpTaskMonth.xls"));
			WorkbookSettings settings = new WorkbookSettings ();
			settings.setWriteAccess(null);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			//WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\DataImpTaskMonth.xls"),wb,settings);
			WritableWorkbook wwb = Workbook.createWorkbook((OutputStream)baos,wb,settings);
			WritableSheet ws = wwb.getSheet(0);
			ws.getSettings().setSelected(true);
			//小字体设置
			WritableFont smallfont = new WritableFont(WritableFont.ARIAL,9,WritableFont.NO_BOLD);
			WritableCellFormat smallformat = new WritableCellFormat(smallfont);
			smallformat.setAlignment(jxl.format.Alignment.LEFT);// 把水平对齐方式指定为居中
			smallformat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
			smallformat.setWrap(true);
			smallformat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
			String[] aStart =genMethod.split(startLoc,";");
			String[] fStart =genMethod.split(aStart[0],",");
			int iStartRow = Integer.parseInt(fStart[0]);
			int iStartCol = Integer.parseInt(fStart[1]);
			int loop = aValues.length;
			for(int i=0;i<loop;i++){
				int nloop = aValues[i].length;
				for(int j=0;j<nloop;j++){
					WritableCell cell = new Label(iStartCol+j,iStartRow+i,aValues[i][j]);
					cell.setCellFormat(smallformat);
					ws.addCell(cell);
				}
			}
			//写入Excel对象
			wwb.write();
			//关闭可写入的Excel对象
			wwb.close();
			//关闭只读的Excel对象
			wb.close();
			return baos.toByteArray();

		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace();
			return null;
		} 
	}
	/**
	 * 将数据赋给模板文件中，并生成打印的Excel 文件
	 * @param contentBytes :二进制文档模板文件
	 * @param value：替换模板中固定标志的值
	 * @startLoc 开始打印的起始行列
	 * @return
	 */
	public  byte[] getCollectBytes(String[][] aValues,String startLoc) {
		try { 
			ByteArrayInputStream  hinput=new ByteArrayInputStream(BytesExcelModule);
			jxl.Workbook wb = jxl.Workbook.getWorkbook(hinput);
			//Workbook wb = Workbook.getWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\ImpTaskMonth.xls"));
			WorkbookSettings settings = new WorkbookSettings ();
			settings.setWriteAccess(null);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			//WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\DataImpTaskMonth.xls"),wb,settings);
			WritableWorkbook wwb = Workbook.createWorkbook((OutputStream)baos,wb,settings);
			WritableSheet ws = wwb.getSheet(0);
			ws.getSettings().setSelected(true);
			//小字体设置
			WritableFont smallfont = new WritableFont(WritableFont.ARIAL,9,WritableFont.NO_BOLD);
			WritableCellFormat smallformat = new WritableCellFormat(smallfont);
			smallformat.setAlignment(jxl.format.Alignment.RIGHT);// 把水平对齐方式指定为 LFT
			smallformat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
			smallformat.setWrap(true);
			smallformat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
			String[] aStart =genMethod.split(startLoc,";");
			String[] fStart =genMethod.split(aStart[0],",");
			int iStartRow = Integer.parseInt(fStart[0]);
			int iStartCol = Integer.parseInt(fStart[1]);
			int loop = aValues.length;
			for(int i=0;i<loop;i++){
				int nloop = aValues[i].length;
				for(int j=0;j<nloop;j++){
					WritableCell cell = new Label(iStartCol+j,iStartRow+i,aValues[i][j]);
					cell.setCellFormat(smallformat);
					//System.out.println(aValues[i][j]);
					ws.addCell(cell);
				}
			}
			//写入Excel对象
			wwb.write();
			//关闭可写入的Excel对象
			wwb.close();
			//关闭只读的Excel对象
			wb.close();
			return baos.toByteArray();

		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace();
			return null;
		} 
	}
	public  byte[] getCollectBytes(String[][] aValues,String startLoc,String[][] aSetAlignment) {
		try {
			ByteArrayInputStream  hinput=new ByteArrayInputStream(BytesExcelModule);
			jxl.Workbook wb = jxl.Workbook.getWorkbook(hinput);
			//Workbook wb = Workbook.getWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\ImpTaskMonth.xls"));
			WorkbookSettings settings = new WorkbookSettings ();
			settings.setWriteAccess(null);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			//WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\DataImpTaskMonth.xls"),wb,settings);
			WritableWorkbook workbook = Workbook.createWorkbook((OutputStream)baos,wb,settings);
			WritableSheet sheet = workbook.getSheet(0);
			sheet.getSettings().setSelected(true);
			//小字体设置
			WritableFont smallfont = new WritableFont(WritableFont.ARIAL,9,WritableFont.NO_BOLD);
			//把水平对齐方式指定为 LFT
			WritableCellFormat smallformatleft = new WritableCellFormat(smallfont);
			smallformatleft.setAlignment(jxl.format.Alignment.LEFT);// 把水平对齐方式指定为 LFT
			smallformatleft.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
			smallformatleft.setWrap(true);
			smallformatleft.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
			//把水平对齐方式指定为 RIGHT
			WritableCellFormat smallformatright = new WritableCellFormat(smallfont);
			smallformatright.setAlignment(jxl.format.Alignment.RIGHT);// 把水平对齐方式指定为 LFT
			smallformatright.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
			smallformatright.setWrap(true);
			smallformatright.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
			//把水平对齐方式指定为 CENTER
			WritableCellFormat smallformatcenter = new WritableCellFormat(smallfont);
			smallformatcenter.setAlignment(jxl.format.Alignment.CENTRE);// 把水平对齐方式指定为 LFT
			smallformatcenter.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
			smallformatcenter.setWrap(true);
			smallformatcenter.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
			String[] aStart =genMethod.split(startLoc,";");
			String[] fStart =genMethod.split(aStart[0],",");
			int iStartRow = Integer.parseInt(fStart[0]);
			int iStartCol = Integer.parseInt(fStart[1]);
			int loop = aValues.length;
			for(int i=0;i<loop;i++){
				int nloop = aValues[i].length;
				for(int j=0;j<nloop;j++){
					WritableCell cell = new Label(iStartCol+j,iStartRow+i,aValues[i][j]);
					String setAlig = "";
					int floop = aSetAlignment.length;
					for(int f=0;f<floop;f++){
						String sIndex = aSetAlignment[f][0];
						int index = Integer.parseInt(sIndex);
						if(j == index){
							setAlig = aSetAlignment[f][1];
							break;
						}
					}
					if(setAlig.length() > 0){
						if(setAlig.equalsIgnoreCase("LEFT")){
							cell.setCellFormat(smallformatleft);
						}else{
							if(setAlig.equalsIgnoreCase("RIGHT")){
								cell.setCellFormat(smallformatright);
							}else{
								if(setAlig.equalsIgnoreCase("CENTER")){
									cell.setCellFormat(smallformatcenter);
								}else{
									cell.setCellFormat(smallformatleft);
								}
							}
						}
					}else{
						cell.setCellFormat(smallformatleft);
					}
					sheet.addCell(cell);
				}
			}
			workbook.write();
			workbook.close();
			//关闭只读的Excel对象
			wb.close();
			return baos.toByteArray();
		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace();
			try {
				//throw new Exception("SQL: \"" + "\" 执行出错，原因是：" + e.getMessage());
			}catch (Exception ioexception) {
				IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
				Fake(illegalargumentexception,ioexception);
				throw illegalargumentexception;
			}
			return null;
		} 
		//return BytesExcelModule;
	}
	/**
	 * 用值替换模板中有固定标志的值（***）
	 * @param contentBytes :二进制文档模板文件
	 * @param value：替换模板中固定标志的值
	 * @return
	 */
	public static String getValuesByBytes(byte[] contentBytes,String locations) {
		String rs = "";
		try { 
			ByteArrayInputStream  input=new ByteArrayInputStream(contentBytes);
			WorkbookSettings workbookSettings=new WorkbookSettings();
			workbookSettings.setEncoding("ISO-8859-1");
			Workbook workbook=Workbook.getWorkbook(input,workbookSettings);
			Sheet sheet=workbook.getSheet(0);
			String[] aRow =genMethod.split(locations,";");
			int loop = aRow.length;
			//System.out.println("loop="+loop);
			for(int i=0;i<loop;i++){
				String rows = aRow[i];
				String[] lArray = genMethod.split(rows,",");
				//System.out.println("lArray[0]="+lArray[0]);
				//System.out.println("lArray[1]="+lArray[1]);
				String content = sheet.getCell(Integer.parseInt(lArray[1]),Integer.parseInt(lArray[0])).getContents();
				rs = rs + content + "@@";
				//vector.addElement(new String(content));
			}
			return rs;

		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace();
			return null;
		} 
	}

	/**
	 * 用值替换模板中有固定标志的值（***）
	 * @param contentBytes :二进制文档模板文件
	 * @param value：替换模板中固定标志的值
	 * @return
	 */
	public static String getModuleFlag(byte[] contentBytes) {
		String moduleflag = "";
		try { 
			ByteArrayInputStream  input=new ByteArrayInputStream(contentBytes);
			WorkbookSettings workbookSettings=new WorkbookSettings();
			workbookSettings.setEncoding("ISO-8859-1");
			jxl.Workbook rw = jxl.Workbook.getWorkbook(input,workbookSettings);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			jxl.write.WritableWorkbook wwb = Workbook.createWorkbook((OutputStream) baos,rw);
			jxl.write.WritableSheet ws = wwb.getSheet(0);
			//获取Sheet表中所包含的总列数
			int MaxColumns = ws.getColumns();
			//获取Sheet表中所包含的总行数，示例：
			int MaxRows = ws.getRows();
			int iR = 0;
			for(int row=0;row<MaxRows;row++)
			{
				for(int j=0;j<MaxColumns;j++)
				{
					jxl.write.WritableCell wc = ws.getWritableCell(j,row);
					//判断单元格的类型, 做出相应的转化
					if(wc.getType() == CellType.LABEL){
						Label l = (Label)wc;
						String ls = l.getString();
						if(ls.equalsIgnoreCase("***")){
							String st = Integer.toString(row) + "," + Integer.toString(j);
							moduleflag = moduleflag + st + ";";
						}
					}
				}
			}
			//关闭可写入的Excel对象
			wwb.close();
			//关闭只读的Excel对象
			rw.close();
			return moduleflag;

		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace();
			return null;
		} 
	}

	/**
	 * 用值替换模板中有固定标志的值（***）
	 * @param contentBytes :二进制文档模板文件
	 * @param value：替换模板中固定标志的值
	 * @return
	 */
	public static String getModuleFlag(byte[] contentBytes,String Flag) {
		String moduleflag = "";
		try { 
			ByteArrayInputStream  input=new ByteArrayInputStream(contentBytes);
			WorkbookSettings workbookSettings=new WorkbookSettings();
			workbookSettings.setEncoding("ISO-8859-1");
			jxl.Workbook rw = jxl.Workbook.getWorkbook(input,workbookSettings);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			jxl.write.WritableWorkbook wwb = Workbook.createWorkbook((OutputStream) baos,rw);
			jxl.write.WritableSheet ws = wwb.getSheet(0);
			//获取Sheet表中所包含的总列数
			int MaxColumns = ws.getColumns();
			//获取Sheet表中所包含的总行数，示例：
			int MaxRows = ws.getRows();
			int iR = 0;
			for(int row=0;row<MaxRows;row++)
			{
				for(int j=0;j<MaxColumns;j++)
				{
					jxl.write.WritableCell wc = ws.getWritableCell(j,row);
					//判断单元格的类型, 做出相应的转化
					if(wc.getType() == CellType.LABEL){
						Label l = (Label)wc;
						String ls = l.getString();
						if(ls.equalsIgnoreCase(Flag)){
							String st = Integer.toString(row) + "," + Integer.toString(j);
							moduleflag = moduleflag + st + ";";
						}
					}
				}
			}
			//关闭可写入的Excel对象
			wwb.close();
			//关闭只读的Excel对象
			rw.close();
			return moduleflag;

		} catch (Exception e) {
			// TODO Auto-generated catch block   
			e.printStackTrace();
			return null;
		} 
	}
	//判断单元格是否含有批注   
	private static boolean isHasComment(Cell cell)   
	{   
		String text="";   

		try{   
			text=cell.getCellFeatures().getComment().toString();   
			return true;   
		}catch(Exception e)   
		{   
			return false;   
		}   
	}
	/**
	 * 
	 * @throws Exception
	 */
	public void setBytesExcelModuleTitle() throws Exception{
		//替换表前端内容
		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;
			}
			BytesExcelModule = replaceExcelModuleTitle(BytesExcelModule,rplValue,keyList);
		}
	}
	//每页行数
	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 reportTitle) {
		this.ReportTitle = 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[][] reportdata) {
		this.ReportData = reportdata;
	}
	//替换成数据的标记
	public void setCovertag(String p_covertag) {
		this.covertag = p_covertag;
	}

	//报表模板文件名
	public void setModuleBytes(byte[] vExcelModule) {
		this.ExeclModule = vExcelModule;
		try {
			byte [] reportbytes = null;
			ByteArrayInputStream  hinput=new ByteArrayInputStream(ExeclModule);
			jxl.Workbook wb = jxl.Workbook.getWorkbook(hinput);
			//Workbook wb = Workbook.getWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\ImpTaskMonth.xls"));
			WorkbookSettings settings = new WorkbookSettings ();
			settings.setWriteAccess(null);
			baos = new ByteArrayOutputStream();
			//WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\ibm\\SMP\\maximo\\reports\\pluck\\module\\DataImpTaskMonth.xls"),wb,settings);
			Sheetwwb = Workbook.createWorkbook((OutputStream)baos,wb,settings);
		}catch(Exception e)   
		{   
			e.printStackTrace();
		}
	}

	public void replaceExcelModuleTitle(int sheetnum,String[] value,String flag) {
		WritableSheet sheet = Sheetwwb.getSheet(sheetnum);
		sheet.getSettings().setSelected(true);
		int MaxColumns = sheet.getColumns();
		//获取Sheet表中所包含的总行数，示例：
		int MaxRows = sheet.getRows();
		int iR = 0;
		for(int row=0;row<MaxRows;row++)
		{
			for(int j=0;j<MaxColumns;j++)
			{
				jxl.write.WritableCell wc = sheet.getWritableCell(j,row);
				//判断单元格的类型, 做出相应的转化
				if(wc.getType() == CellType.LABEL){
					Label l = (Label)wc;
					String ls = l.getString();
					if(ls.indexOf(flag,0) > -1){
						String st = genMethod.replaceValueStr(ls,flag,value[iR]);
						l.setString(st);
						iR++;
					}
				}
			}
		}
	}
	//分析报表某个sheet 页模板
	public void AnalysisSheetBytes() {
		if(ExeclModule != null){
			//创建的报表模板
			try {
				HmReportModule = createReportSheetModule();
			}catch(Exception e)   
			{   
				e.printStackTrace();
			}
		}
	}

	//模板的 sheet 页编号
	public void setSheetNumber(int p_sheetnum){
		this.SheetNumber = p_sheetnum;
	}

	//记录填写数据记录的起始行号
	public int getMergerSartRow() {
		return this.MergerSartRow;
	}
	//记录填写数据记录的起始行号
	public int getSheetSartRow() {
		return this.SheetStartRow;
	}
	//报表模板文件名
	public void setBytesExcelModule(String ExcelModule) {
		try {
			//规定报表模板必须放在maximo 的resources\defaults
			String sReportpath = loadConfigValue("com/config/report.cfg","reportpath");
			String modflm   = sReportpath+ System.getProperties().getProperty("file.separator")+ExcelModule;
			//创建报表模板的二进制文件流
			java.io.File modulefile=new java.io.File(modflm);
			FileInputStream fin=new FileInputStream(modulefile);
			byte [] contentBytes=new byte[fin.available()];
			fin.read(contentBytes);
			this.BytesExcelModule = contentBytes;
		}catch(Exception e)   
		{   
			e.printStackTrace();
		}   
	}
	//新报表的sheet 名
	public void setSheetName(String vSheetname) {
		this.SheetName = vSheetname;
	}
	//打印数据行内容字体大小
	public void setDataFontSize(int dataFontSize) {
		this.DataFontSize = dataFontSize;
	}
	//打印数据行内容行的行高
	public void setDataLineHeight(int dataLineHeight) {
		this.DataLineHeight = dataLineHeight;
	}

	//多个excel 合并为一个 excel 的 sheet
	//WritableSheet MergerSheet = null;
	public void setMergerSheet(WritableSheet vMergerSheet) {
		this.MergerSheet = vMergerSheet;
	}

	//多个excel 合并为一个 excel 的 sheet 上创建 新行的起始行号
	//int MergerStartRow = 0;
	public void setMergerStartRow(int vMergerStartRow) {
		this.MergerStartRow = vMergerStartRow;
	}
	//多个excel 合并为一个 excel 的 sheet 填写完数据的最后行行号
	//int MergerStopRow = 0;
	public int getMergerStopRow() {
		return this.MergerStopRow;
	}

	//DataCloums
	public int getDataCloums() {
		return this.DataCloums;
	}
	//读出配置文件
	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;
	}
	///   <summary> 
	///   将流保存为文件 
	///   </summary> 
	///   <returns> </returns> 
	private boolean SaveStreamToFile(byte[] excelModuleBytes,String path) 
	{ 
		try {
			FileOutputStream fos = new FileOutputStream(path);
			fos.write(excelModuleBytes);
			fos.close();
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		return true; 
	}
	public static Throwable Fake(Throwable throwable, Throwable throwable1) {
		try {
			throwable.getClass().getMethod("initCause", new Class[] {
					java.lang.Throwable.class
			}).invoke(throwable, new Object[] {
					throwable1
			});
		}
		catch (Exception ioexception) {
			IllegalArgumentException illegalargumentexception = new IllegalArgumentException(ioexception.toString());
			Fake(illegalargumentexception,ioexception);
			throw illegalargumentexception;
		}
		return throwable;
	}
}