package download;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import jxl.Workbook;
import jxl.format.Border;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class DownloadTools {
    
  private DownloadTools aninyDao;
  private InputStream excelStream;
  public InputStream getExcelStream() {
    return excelStream;
  }
  public void setExcelStream(InputStream excelStream) {
    this.excelStream = excelStream;
  }

  /**
    * 醉翁之意不在酒
    */

  public DownloadTools(DownloadTools aninydao) {
    super();
    this.aninyDao =aninydao;
  }
    
  public String expZip() throws IOException{
    InputStream[] excelStream2=getDownloadInputStream(getDateList());
    String filepath = "D:/Log.zip";
    File f = new File(filepath);
    f.createNewFile();
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
    out.putNextEntry(new ZipEntry("/"));
    for (int i = 0; i < excelStream2.length; i++) {
    //FileInputStream In = excelStream2[i];//分批以流的方式读取要压缩的文件
      out.putNextEntry(new ZipEntry((i+1)+".xls")); // 开始写入新的 ZIP 分别为每一个文件添加名称
      int b;
      while ((b = excelStream2[i].read()) != -1) {
        out.write(b);
      }
      excelStream2[i].close();
    
    }
    out.flush();
    out.close();

    InputStream fis = new BufferedInputStream(new FileInputStream(filepath));
    this.setExcelStream(fis);
    f.delete();
    return "toDownloadZipActionResult";
  }
  /**
    *    
    */

  public InputStream[] getDownloadInputStream(List list)
  {
    int count=aninyDao.getDateCount(list);
    int weekRows=500;//每页500行
    int page = (count + weekRows - 1) / weekRows;
    ByteArrayInputStream[] inputStream = new ByteArrayInputStream[page];
    for(int p = 0 ; p < page ; p++){
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      try{
        WritableFont font= new WritableFont(WritableFont.createFont("宋体".trim()),10,WritableFont.BOLD);
        WritableCellFormat cellfmt = new WritableCellFormat(font);            
        cellfmt.setBorder(Border.ALL, jxl.format.BorderLineStyle.THIN);
        cellfmt.setBackground(Colour.WHITE);    
        cellfmt.setAlignment(jxl.format.Alignment.CENTRE);    
            WritableWorkbook wwb = Workbook.createWorkbook(out);
            WritableSheet ws = wwb.createSheet(String.valueOf("excel 页签"+p).trim(),0);
            //Label x,y,z,e---对应第x列,第y行,显示z字符,使用e格式
            ws.addCell(new Label(0,0, "用户姓名".trim(), cellfmt));
            List<Map<Integer,String>> DateList=getDateList();
            if(DateList!=null&&DateList.size()>0)
            {
              for(int i=0;i<DateList.size();i++)
              {
                ws.addCell(new Label(0,i+1,DateList.get(i).get(i+1).toString()));
              }
            }
                    wwb.write();
        wwb.close();
        out.flush();
        out.close();
        } catch (IOException e) {
          e.printStackTrace();
        } catch (WriteException e) {
          e.printStackTrace();
        }
        inputStream[p]=new ByteArrayInputStream(out.toByteArray());
        return inputStream;
      }
    return null;
  }
  public int getDateCount(List...objects)
  {
    /*
     * 逻辑处理
     * 按需而定,此处不做处理
     */

    return objects.length;
  }
  public List<Map<Integer,String>> getDateList()
  {
    
    Map<Integer, String> map=new HashMap<Integer, String>();
    map.put(Integer.valueOf("1"),"宋健!!!");
    map.put(Integer.valueOf("2"),"宋健@@@");
    map.put(Integer.valueOf("3"),"宋健###");
    map.put(Integer.valueOf("4"),"宋健$$$");
    map.put(Integer.valueOf("5"),"宋健^^^");
    List<Map<Integer,String>> dateList=new ArrayList<Map<Integer,String>>();
    dateList.add(map);
    return dateList;
  }
}
希望能有更多的Tools出来