Example #1
0
  /**
   * @param SheetName excel里面的sheet的名字 (可以任意写,一般和excelname一样)
   * @param response 要写的流
   * @param HeadName 数据的表头 即列名
   * @param data 数据 二维数组
   * @return
   * @throws IOException
   */
  public static OutputStream creatExcel(
      String SheetName, File file, List<String> HeadName, String[][] data, Integer[] ColumnWidth)
      throws IOException {
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet(SheetName);
    CellStyle s = new CellStyle();

    Font f12 = new Font();
    HSSFFont font12 = ExcelUtil.getHSSFFont(workbook, f12);
    HSSFCellStyle style = ExcelUtil.getNewStyle(workbook, s, font12);

    // sheet.setDefaultColumnWidth(50);

    sheet.setDefaultRowHeight((short) 400);
    sheet.setColumnWidth(0, 18 * 256);
    OutputStream os = new FileOutputStream(file);
    // 设置excel的表头
    for (int i = 0; i < HeadName.size(); i++) {
      ExcelUtil.setCells(0, i, HeadName.get(i), sheet, style);
    }
    // 设置excel数据
    for (int i = 0; i < data.length; i++) {
      for (int j = 0; j < data[i].length; j++) {
        ExcelUtil.setCells(i + 1, j, data[i][j], sheet, style);
      }
    }
    // 设置excel 表格的列宽
    for (int i = 0; i < HeadName.size(); i++) {
      sheet.setColumnWidth(i, ColumnWidth[i]);
    }
    workbook.write(os);
    return os;
  }
Example #2
0
  public static OutputStream creatExcel(
      String SheetName,
      String excelName,
      HttpServletResponse response,
      List<String> HeadName,
      String[][] data,
      List<Integer> colunmWidth)
      throws IOException {
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet(SheetName);
    CellStyle s = new CellStyle();
    Font f12 = new Font();
    HSSFFont font12 = ExcelUtil.getHSSFFont(workbook, f12);
    HSSFCellStyle style = ExcelUtil.getNewStyles(workbook, s, font12);
    sheet.setDefaultColumnWidth(50);

    sheet.setDefaultRowHeight((short) 400);
    response.setContentType("application/ms-excel;");
    response.setHeader(
        "Content-Disposition", "attachment;Filename=" + ExcelUtil.toUtf8String(excelName));
    sheet.setColumnWidth(0, (HeadName.size()) * 256);
    OutputStream os = response.getOutputStream();
    // 设置excel的表头
    for (int i = 0; i < HeadName.size(); i++) {
      ExcelUtil.setCells(0, i, HeadName.get(i), sheet, style);
    }
    // 设置excel数据
    for (int i = 0; i < data.length; i++) {
      for (int j = 0; j < data[i].length; j++) {
        ExcelUtil.setCells(i + 1, j, data[i][j], sheet, style);
      }
    }
    // 设置excel 表格的列宽
    for (int i = 0; i < HeadName.size(); i++) {
      sheet.setColumnWidth(i, colunmWidth.get(i));
    }
    workbook.write(os);
    return os;
  }