jxl 导出Excel

来源:互联网 发布:数据的物理独立性 编辑:程序博客网 时间:2024/05/16 00:51

创建Excel  

public static void putRow(WritableSheet ws, int rowNum, Object[] cells,
   int index[]) throws Exception {

 Label cell = null;
   for (int i = 0; i < cells.length; i++) {
    StringBuffer sbf = new StringBuffer();
    if (cells[i] == null || cells[i].toString() == ""
      || cells[i].toString() == "null"
      || cells[i].toString().length() == 0) {
     sbf.append("");
    } else {
     sbf.append(cells[i]);
    }
   
     cell = new Label(i, rowNum, String.valueOf(sbf));
    

    ws.addCell(cell);
   }

}

 

public static void ExcelExport(OutputStream out, List list, String head[],
   String row[], String BackRound[]) {
  try {

   if (list != null && list.size() > 0) {
    if (row != null&&row.length > 0) {
     List list1 = ExcelExportUtil.packageList(list, row);

     WritableWorkbook workbook = Workbook.createWorkbook(out);
     WritableSheet ws = workbook.createSheet("sheet 1", 0);
     int rowNum = 0;
     if (head != null) {
      putRow(ws, 0, head, null);
      rowNum = 1;
     }
     for (int i = 0; i < list1.size(); i++) {
      Object[] cells = (Object[]) list1.get(i);
      putRow(ws, rowNum, cells, index);
      rowNum++;
     }
     workbook.write();
     workbook.close();
     out.close();
    }
   } else {
    System.out.println("ExcelExportUtil.ExcelExport error !参数错误");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 

原创粉丝点击