项目所用技术回顾之excel导出(基于poi)

public String export(DataTable dataTable ,List tableColumns, List values) throws Exception {
        String simpleName = Calendar.getInstance().get(Calendar.SECOND)+".xls";
        String fileName = SysParameter.EXPORT_TEMP + simpleName;
        HSSFWorkbook workbook;//工作本
        HSSFSheet sheet;// 当前表Sheet
        HSSFRow row;//当前行Row
        HSSFCell cell;//当前单元格Cell
        HSSFCellStyle titleStyle , cellStyle;
        HSSFFont font;
        int rowIndex = 0;
        int cellIndex = 0;
        Iterator iterator,valueIterator;
        TableColumn column;
        DataTableSample data;
        FileOutputStream outputStream  = null;
        try{

            workbook = new HSSFWorkbook();
            sheet = workbook.createSheet(dataTable.getTableCname());
            valueIterator = values.iterator();

            cellStyle =  workbook.createCellStyle();
            cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);

            titleStyle = workbook.createCellStyle();
            titleStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
            titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
            titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
            titleStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
            font = workbook.createFont();
            font.setColor(HSSFColor.WHITE.index);
            titleStyle.setFont(font);

            row = sheet.createRow(rowIndex++);
            cellIndex = 0;
            iterator = tableColumns.iterator();
            while(iterator.hasNext()){
                column = (TableColumn)iterator.next();
                cell = row.createCell(cellIndex++);



                cell.setCellStyle(titleStyle);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(column.getColumnCname());
            }



            while(valueIterator.hasNext()){
                row = sheet.createRow(rowIndex++);
                cellIndex = 0;
                iterator = tableColumns.iterator();
                data = (DataTableSample)valueIterator.next();

                while(iterator.hasNext()){
                    column = (TableColumn)iterator.next();
                    cell = row.createCell(cellIndex++);
                    cell.setCellStyle(cellStyle);
                    if(column.getColumnName().toLowerCase().startsWith("ex")){
                        if( data.getEx(Integer.parseInt(column.getColumnName().substring(2)))!=null){
                            cell.setCellValue(  data.getEx(  Integer.parseInt(column.getColumnName().substring(2))) );
                        }
                    }else if(column.getColumnName().toLowerCase().startsWith("ix")){
                        if(data.getIx(Integer.parseInt(column.getColumnName().substring(2)))!=null){
                            cell.setCellValue( data.getIx(Integer.parseInt(column.getColumnName().substring(2))));
                        }
                    }else if(column.getColumnName().toLowerCase().startsWith("dx")){
                        if(data.getDx(Integer.parseInt(column.getColumnName().substring(2)))!=null){
                            cell.setCellValue( data.getDx(Integer.parseInt(column.getColumnName().substring(2))));
                        }
                    }

                }

            }
            <span style="color:#ff0000;">outputStream = new FileOutputStream(fileName);
            workbook.write(outputStream);</span>


        }finally{
            if(outputStream!=null){
                outputStream.close();
            }
        }
        return simpleName;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值