本帖最后由 liuyaotao 于 2010-05-27 01:20:54 编辑

解决方案 »

  1.   

    先给一个导出到excel中的主方法(输入有data:从数据库中查询出来的结果转化成二维数组,path表示路径;sheetName表示excel名字)
    //导出到Excel中
    public void write(String[][]data,String path,String sheetName){
    ArrayList<userModel> list = new ArrayList<userModel>();
    list = this.getList();
    init();
    try {
    WritableWorkbook book = Workbook.createWorkbook((new File(path )));
    WritableSheet sheet = book.createSheet(sheetName, 0);

    for (int i = 0; i < 18; i++) {
    sheet.setColumnView(i, 20);  // 设置列宽
    }
    sheet.setRowView(0, 600, false); // 设置行高 for (int i = 0; i < data.length; i++) {
    for (int j = 0; j < data[i].length; j++) {
    int num = 0;
    try {
    num = Integer.parseInt(data[i][j]);
    sheet.addCell(new Number(j, i, num, centerStyle));
    } catch (NumberFormatException e) {
    sheet.addCell(new Label(j, i, data[i][j], centerStyle));
    }
    }
    }
    book.write();
    book.close();
    } catch (RowsExceededException e) {
    e.printStackTrace();
    } catch (WriteException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
      

  2.   

    另外你要先下载jxl.jar,这个jar主要实现excel相关操作