关于Excel导入导出的总结(JXL)

来源:互联网 发布:unity3d 游戏特效 编辑:程序博客网 时间:2024/06/06 16:33

导出EXCEL

public class ExportExcel {private static ExportExcel instance = null;private static synchronized void insInit(){if(null==instance){instance = new ExportExcel();}}public static ExportExcel getInstance(){if(null==instance){insInit();}return instance;}/** * 生成Excel文件 * @param response response对象 * @param fileName 文件名 * @param objData 数据列表 * @param sheetName 第一个工作表的名称 * @param columns 表头数组 * @param fields 列数组 * @return */public int exportToExcel(HttpServletResponse response,String fileName,  List objData, String sheetName,List<String> columns,List<String> fields) {int flag = 0;        response.setContentType("application/vnd.ms-excel");        try {            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }         try {            //根据传进来的file对象创建可写入的Excel工作薄            OutputStream os = response.getOutputStream();            //声明工作簿jxl.write.WritableWorkbook            WritableWorkbook wwb = Workbook.createWorkbook(os);            /*             * 创建一个工作表、sheetName为工作表的名称、"0"为第一个工作表             * 打开Excel的时候会看到左下角默认有3个sheet、"sheet1、sheet2、sheet3"这样             * 代码中的"0"就是sheet1、其它的一一对应。             * createSheet(sheetName, 0)一个是工作表的名称,另一个是工作表在工作薄中的位置             */            WritableSheet ws = wwb.createSheet(sheetName, 0);            SheetSettings ss = ws.getSettings();            ss.setVerticalFreeze(1);//冻结表头            WritableFont font1 =new WritableFont(WritableFont.createFont("微软雅黑"), 10 ,WritableFont.BOLD);            WritableFont font2 =new WritableFont(WritableFont.createFont("微软雅黑"), 9 ,WritableFont.NO_BOLD);            WritableCellFormat wcf = new WritableCellFormat(font1);            WritableCellFormat wcf2 = new WritableCellFormat(font2);            WritableCellFormat wcf3 = new WritableCellFormat(font2);//设置样式,字体            //创建单元格样式            //WritableCellFormat wcf = new WritableCellFormat();            //背景颜色            wcf.setBackground(jxl.format.Colour.YELLOW);            wcf.setAlignment(Alignment.CENTRE);  //平行居中            wcf.setVerticalAlignment(VerticalAlignment.CENTRE);  //垂直居中            wcf3.setAlignment(Alignment.CENTRE);  //平行居中            wcf3.setVerticalAlignment(VerticalAlignment.CENTRE);  //垂直居中            wcf3.setBackground(Colour.LIGHT_ORANGE);            wcf2.setAlignment(Alignment.CENTRE);  //平行居中            wcf2.setVerticalAlignment(VerticalAlignment.CENTRE);  //垂直居中            //判断一下表头数组是否有数据            if (columns != null && !columns.isEmpty()) {                //循环写入表头                for (int i = 0; i < columns.size(); i++) {                ws.setColumnView(i, 20);//设置单元格宽度                    /*                     * 添加单元格(Cell)内容addCell()                     * 添加Label对象Label()                     * 数据的类型有很多种、在这里你需要什么类型就导入什么类型                     * 如:jxl.write.DateTime 、jxl.write.Number、jxl.write.Label                     * Label(i, 0, columns[i], wcf)                     * 其中i为列、0为行、columns[i]为数据、wcf为样式                     * 合起来就是说将columns[i]添加到第一行(行、列下标都是从0开始)第i列、样式为什么"色"内容居中                     */                    ws.addCell(new Label(i, 0, columns.get(i), wcf));                }                //判断表中是否有数据                if (objData != null && !objData.isEmpty()) {                    //循环写入表中数据                    for (int i = 0; i < objData.size(); i++) {                        //转换成Object对象                        Object obj = objData.get(i);                        //循环输出Object对象 属性值                        for (int j = 0; j < fields.size(); j++) {                        String str =  fields.get(j);                        str ="get"+ str.replaceFirst(str.substring(0, 1), str.substring(0, 1).toUpperCase())  ; //首字符转成大写 对应属性getFiled 方法                        String value = (String) obj.getClass().getMethod(str,null).invoke(obj, null);                        if(value==null){                        value="";                        }                        ws.addCell(new Label(j,i+1,String.valueOf(value)));}                    }                }else{                    flag = -1;                }                //写入Exel工作表                wwb.write();                //关闭Excel工作薄对象                 wwb.close();                                //关闭流                os.flush();                os.close();                                os =null;            }        }catch (IllegalStateException e) {            e.printStackTrace();        }        catch (Exception ex) {            flag = 0;            ex.printStackTrace();        }        return flag;    }}


0 0
原创粉丝点击