Excel生成及下载的JSF实现!

package cn.ccb.elms.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
* <p>功能说明:用于文件下载的相关操作</p>
* @author gjg
* @version 1.0 2006-04-27
*/
public class DownloadUtil
{
  /**
   * 用二维数组的格式生成Excel文件
   * @param dataList String[][]
   */
  public static void genericExcel(String dataList[][])
  {
    String strFileName = "";
    try
    {
      // 生成Excel文件并保存在服务器上
      SSConfig ssc = new SSConfig();
      String strZFileName = "Query_" + ssc.getSysTimeStamp(); // 主文件名
      strFileName = strZFileName + ".xls"; // 文件全名
      FileOutputStream fileOut = new FileOutputStream(strFileName);
      HSSFWorkbook wb = new HSSFWorkbook();
      HSSFCellStyle cs = wb.createCellStyle(); // 格式对象
      HSSFFont fCol = wb.createFont(); // 字体对象,表头
      fCol.setFontHeightInPoints( (short) 10);
      fCol.setBoldweight(fCol.BOLDWEIGHT_BOLD);
      // 工作表
      HSSFSheet sheet = wb.createSheet(0 + "");
      wb.setSheetName(0, strZFileName, (short) 1);
      // 第一行,放表头
      HSSFRow row = null;

      int nRowNum = 0;
      for (int nh = 0; nh < dataList.length; nh++)
      {
        row = sheet.createRow(nRowNum); // 创建行
        for (int nk = 0; nk < dataList[0].length; nk++)
        {
          HSSFCell cell = row.createCell( (short) (nk));
          cell.setEncoding(HSSFCell.ENCODING_UTF_16);
          cell.setCellValue(dataList[nh][nk]); // 写入单元格
          //将表头设为粗体居中
          if (nRowNum == 0)
          {
            cs.setFont(fCol);
            cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cell.setCellStyle(cs);
          }
        }
        nRowNum++;
      }
      wb.write(fileOut);
      fileOut.close();

    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    //根据提供的文件名下载文件
    downloadFile(strFileName);
  }

  /**
   * 用List格式生成Excel文件
   * @deprecated
   * @param ColName_zh List
   * @param data List
   */
  public static void genericExcel2(List ColName_zh, List data)
  {
    String strFileName = "";
    try
    {
      if (ColName_zh == null || data == null)
      {
        throw new Exception("表头列数或数据列数为空!");
      }

      // 生成Excel文件并保存在服务器上
      SSConfig ssc = new SSConfig();
      String strZFileName = "Query_" + ssc.getSysTimeStamp(); // 主文件名
      strFileName = strZFileName + ".xls"; // 文件全名
      FileOutputStream fileOut = new FileOutputStream(strFileName);
      HSSFWorkbook wb = new HSSFWorkbook();
      HSSFCellStyle cs = wb.createCellStyle(); // 格式对象
      HSSFFont fCol = wb.createFont(); // 字体对象,表头
      fCol.setFontHeightInPoints( (short) 10);
      fCol.setBoldweight(fCol.BOLDWEIGHT_BOLD);
      // 工作表
      HSSFSheet sheet = wb.createSheet(0 + "");
      wb.setSheetName(0, strZFileName, (short) 1);
      // 第一行,放表头
      HSSFRow row = null;
      row = sheet.createRow( (short) 0);
      int ny = 0;
      for (Iterator iter = ColName_zh.iterator(); iter.hasNext(); )
      {
        HSSFCell cell = row.createCell( (short) ny);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cs.setFont(fCol);
        cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cell.setCellStyle(cs);
        cell.setCellValue(iter.next() + "");
        ny++;
      }
      // x代表数据的起始行数
      // y代表数据从第哪一列开始填充
      int nx = 1; // 数据从第一行开始
      for (Iterator itert = data.iterator(); itert.hasNext(); )
      {
        ny = 0;
        row = sheet.createRow( (short) nx);
        ArrayList al2 = (ArrayList) itert.next();
        for (Iterator iter2 = al2.iterator(); iter2.hasNext(); )
        {
          HSSFCell cell = row.createCell( (short) ny);
          cell.setEncoding(HSSFCell.ENCODING_UTF_16);
          // cs.setFont(fCol);
          cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
          // cell.setCellStyle(cs);
          cell.setCellValue(iter2.next() + "");
          ny++;
        }
        nx++;
      }
      wb.write(fileOut);
      fileOut.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    //根据提供的文件名下载文件
    downloadFile(strFileName);
  }

  /**
   * <p>功能说明:根据提供的文件名下载文件</p>
   * @param strfileName String
   * @return void
   */
  private static void downloadFile(String strfileName)
  {
    try
    {
      File exportFile = new File(strfileName);
      HttpServletResponse httpServletResponse = (HttpServletResponse)
          FacesContext.getCurrentInstance().
          getExternalContext().getResponse();
      ServletOutputStream servletOutputStream = httpServletResponse.
          getOutputStream();
      httpServletResponse.setHeader("Content-disposition",
                                    "attachment; filename=" + strfileName);
      httpServletResponse.setContentLength( (int) exportFile.length());
      httpServletResponse.setContentType("application/x-download");
      byte[] b = new byte[1024];
      int i = 0;
      FileInputStream fis = new java.io.FileInputStream(exportFile);
      while ( (i = fis.read(b)) > 0)
      {
        servletOutputStream.write(b, 0, i);
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    FacesContext.getCurrentInstance().responseComplete();
  }

  public static void main(String[] args)
  {
    //genericExcel()用法
    String s[][] = new String[][]
        {
        {
        "列1", "列2", "列3", "列4"},
        {
        "A", "22", "33", "44"}
    };
    genericExcel(s);

    //genericExcel2()用法
    //al代表表头
    ArrayList al = new ArrayList();
    al.add("a");
    al.add("b");
    al.add("c");
    // al2代表多行数据
    ArrayList al2 = new ArrayList();
    // al3代表一行数据
    ArrayList al3 = new ArrayList();
    al3.add("1");
    al3.add("2");
    al3.add("3");
    al2.add(al3);
    al3 = new ArrayList(); // 此处必须new一个新的ArrayList对象
    al3.add("11");
    al3.add("22");
    al3.add("33");
    al2.add(al3);
    genericExcel2(al, al2);
  }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值