Jxl导出Excel表 仅供参考

public class JxlExcelExport{
    private Logger logger = Logger.getLogger(this.getClass());

    private int[] createHeader(WritableSheet sheet,String[] attributes)
                            throws RowsExceededException, WriteException {
        
        int[] columnWidth = new int[attributes.length];
        WritableCellFormat cellFormat = new WritableCellFormat(new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false));
        cellFormat.setAlignment(Alignment.CENTRE);
        Label label;
        for(int i = 0; i < attributes.length; i++) {
            label = new Label(i, 0, attributes[i], cellFormat);
            sheet.addCell(label);
            // Set column width
            columnWidth[i] = (int) (attributes[i].length() * 1.5);
        }
        return columnWidth;
    }
    
    private int[] createBody(WritableSheet sheet,
                            List list,
                            String[] attributes,
                            Class[] types,
                            int[] columnWidth,
                            int currRow)
            throws RowsExceededException, WriteException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Iterator it = list.iterator();
        Object vo;
        Object obj;
        Label label;
        Number number;
        DateTime dateCell;
        int rowIndex = currRow;
        DateFormat customDateFormat = new DateFormat("MM/dd/yyyy HH:mm");
        while(it.hasNext()) {
            vo = it.next();
            
            for(int i = 0; i < attributes.length; i++) {
                obj  = BeanUtilsBean.getInstance().getProperty(vo, attributes[i]);
                obj = (obj == null)? "" : obj;
                label = new Label(i, rowIndex, obj.toString());
                sheet.addCell(label);
                
                if(types[i].equals(Date.class)) {
                    if(obj instanceof Date) {
                        // Format date
                        dateCell = new DateTime(i, rowIndex, (Date)obj, new WritableCellFormat(customDateFormat));
                        sheet.addCell(dateCell);
                        columnWidth[i] = checkMaxWidth(columnWidth[i], dateCell.getBytes().length);
                    } else {
                        label = new Label(i, rowIndex, obj.toString());
                        sheet.addCell(label);
                        columnWidth[i] = checkMaxWidth(columnWidth[i], obj.toString().length());
                    }
                } else if(types[i].equals(Integer.class)) {
                    // Set number cell
                    number = new Number(i, rowIndex, new Double((String)obj).doubleValue());
                    sheet.addCell(number);
                    columnWidth[i] = checkMaxWidth(columnWidth[i], obj.toString().length());
                } else {
                    label = new Label(i, rowIndex, obj.toString());
                    sheet.addCell(label);
                    columnWidth[i] = checkMaxWidth(columnWidth[i], StringUtils.trim(obj.toString()).length());
                }
            }
            rowIndex ++;
        }
        return columnWidth;
    }

    private void autoWidthColumns(WritableSheet sheet, int[] columnWidth) {
        for(int i = 0; i < columnWidth.length; i++) {
            sheet.setColumnView(i, columnWidth[i]);
        }
    }
    
    private int checkMaxWidth(int orignWidth, int comparedWdith) {
        // Column width will be different for font family
        return (int) (orignWidth > comparedWdith * 1.2? orignWidth : comparedWdith * 1.2);
    }

    
    public void export(HttpServletRequest request,
                       HttpServletResponse response,
                       User user,
                       String[] propertyNames,
                       Class[] types,
                       List list,
                       String fileName){
        WritableWorkbook workbook = null;
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename="+ fileName +".xls");
            
            workbook = Workbook.createWorkbook(response.getOutputStream());
            WritableSheet sheet = workbook.createSheet(fileName, 0);
            int[] columnWidth;
            columnWidth = createHeader(sheet,propertyNames);

            int currRow = 1;
            autoWidthColumns(sheet, columnWidth);
            workbook.write();

        } catch (Exception e) {
            logger.error("JXLExcelExporter error, ", e);
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (Exception e2) {
                }
            }
        }
    }
}


// Action
    userList = userService.findUser(user);
    String[] attributes = new String[] { "userID", "userName","sex", "age", "address", "phoneNumber",};
    Class[] types = new Class[] {String.class, String.class, String.class,String.class, String.class, String.class};
    JxlExcelExport jxlExcelExport = new JxlExcelExport();
    jxlExcelExport.export(request, response,attributes,types, userList, "user");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值