解决方案 »

  1.   

    导出好做 ,我用的poi做的,
      

  2.   

    用poi导出Excel
    public void exportExcel(String title, List<ExcelHeader> headers,
    Collection<T> dataset, HttpServletResponse response) {
    // 声明一个工作薄
    HSSFWorkbook workbook = new HSSFWorkbook();
    // 生成一个表格
    HSSFSheet sheet = workbook.createSheet(title);
    // 产生表格标题行
    HSSFRow row = sheet.createRow(0);
    List<String> fields = new ArrayList<String>();
    for (int i = 0; i < headers.size(); i++) {
    ExcelHeader header = headers.get(i);
    HSSFCell cell = row.createCell(i);
    HSSFRichTextString text = new HSSFRichTextString(
    header.getHeaderName());
    cell.setCellValue(text);
    fields.add(header.getObjField());
    } // 遍历集合数据,产生数据行
    Iterator<T> it = dataset.iterator();
    int index = 0;
    while (it.hasNext()) {
    index++;
    row = sheet.createRow(index);
    T t = (T) it.next();
    // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
    for (int j = 0; j < fields.size(); j++) {
    HSSFCell cell = row.createCell(j);
    String fieldName = fields.get(j);
    String getMethodName = "get"
    + fieldName.substring(0, 1).toUpperCase()
    + fieldName.substring(1);
    try {
    Class tCls = t.getClass();
    Method getMethod = tCls.getMethod(getMethodName,
    new Class[] {});
    Object value = getMethod.invoke(t, new Object[] {});
    if (value instanceof Integer) {
    cell.setCellValue((Integer) value);
    } else if (value instanceof String) {
    cell.setCellValue(value.toString());
    } else if (value instanceof Double) {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setGroupingUsed(false);
    cell.setCellValue(nf.format((Double) value));
    } else if (value instanceof Date) {
    SimpleDateFormat format = new SimpleDateFormat(
    "yyyy-MM-dd HH:mm:ss");
    cell.setCellValue(format.format(value));
    } else if (value instanceof Float) {
    cell.setCellValue((Float) value);
    } else {
    cell.setCellValue(value.toString());
    }
    } catch (SecurityException e) {
    e.printStackTrace();
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    } catch (IllegalArgumentException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    } finally {
    // 清理资源
    }
    }
    }
    ServletOutputStream outStream = null;
    try {
    response.reset();
    response.setContentType("application/x-msdownload");
    response.setHeader("Content-Disposition", "attachment; filename="
    + new String(title.getBytes("gb2312"), "ISO-8859-1")
    + ".xls");
    outStream = response.getOutputStream();
    workbook.write(outStream);
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    try {
    outStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    public class ExcelHeader { /**
     * 对象属性名称
     * */
    private String objField;

    /**
     * 对象属性对应的表头名称
     * */
    private String headerName;
    }
      

  3.   

     百度下poi的用法就行了,网上例子很多,难度不大