zoukankan      html  css  js  c++  java
  • [转载]Java导出Excel

    一、需求介绍

      当前B/S模式已成为应用开发的主流,而在开发企业办公系统的过程中,常常有客户这样子要求:把系统数据库中的数据导出到Excel,用户查看报表时直接用Excel打开。或者是:用户已经习惯用Excel打印,也就要求必须把数据导入到Excel文件。这样在我们实际的开发中,很多时候需要实现导出Excel的应用。目前从网上找到的比较常用的实现Java导出Excel的技术有三种 POI、JXL、PageOffice,(CSV技术就不讨论了,它只是生成以逗号间隔的文本文件)下面我就分析一下这三种方案。

    二、方案分析

      POI 是apache的子项目,目标是处理ole2对象,它提供了一组操纵Windows文档的Java API。用于操作Excel格式文件的是HSSF接口,处理MS Excel对象,它不象我们用CSV生成的仅仅是没有格式的可以由Excel转换的文本文件,而是模拟操作Excel对象,你可以控制一些属性如sheet,cell等等。HSSF 是Horrible SpreadSheet Format的缩写,翻译过来就是“讨厌的电子表格格式”。 HSSF的名字很不严肃,但通过HSSF,你可以用Java代码来修改Excel文件内容。HSSF 为读取操作提供了两类API:usermodel和eventusermodel,即“用户模型”和“事件-用户模型”,接口比较抽象,不好理解。 

      JXL属于开放源码项目,通过JXL同样可以修改Excel文件中的数据内容。JXL 由于其小巧 易用的特点, 逐渐已经取代了 POI-excel的地位, 成为更多的java开发人员生成excel文件的首选。JXL的特点:支持Excel 2000标准格式、支持字体、数字、日期格式化操作 、支持对单元格加阴影和加色彩、比POI更小更省内存。这些功能能够大致满足用户的一般需求。

      PageOffice封装Office VBA接口为Java调用的Excel简化对象模型,指的是完全用Java实现的一组Excel存取对象,便于服务器端Java代码直接调用。这套对象模型不是简单地模仿Excel的VBA对象接口,而是从开发者常用的编程代码角度,结合数据库数据访问的特点抽象出的一套实用、调用简单的对象库,把原本复杂的Excel读写操作简单化了,同时解决了Excel文件的在线打开问题,用户可以直接在网页中查看和编辑Excel,无需繁琐的上传下载操作。

      篇幅有限,也为了节约大家的阅读时间,下面只讨论相对比较好的PageOffice方案。

      用PageOffice可以用简单调用方式生成Excel文件,也可以用高级调用方式生成。简单调用方式是指在Excel模板文件中先设置好所有的表格样式,开发的时候只是编写代码给Excel模板填充数据即可,此方式编写的代码量最少,效果并不差,这也是PageOffice推荐的调用方式;高级调用方式可以实现在一个空白的Excel文件中生成很漂亮的表格(甚至给单元格赋值公式),但是编程的工作量就大多了。

    三、简单调用方式

      1. 制作excel模板:

      

      2. 编写代码:

    复制代码
        PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
        poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); 
        //定义Workbook对象
        Workbook workBook = new Workbook();
        //定义Sheet对象,"Sheet1"是打开的Excel表单的名称
        Sheet sheet = workBook.openSheet("Sheet1");
        //定义Table对象
        Table table = sheet.openTable("B4:F13");
        for(int i=0; i < 50; i++)
        { 
            table.getDataFields().get(0).setValue("产品 " + i);
            table.getDataFields().get(1).setValue("100");
            table.getDataFields().get(2).setValue(String.valueOf(100+i));
            table.nextRow();
        }
        table.close();
            
        poCtrl1.setWriter(workBook);
        //打开Excel文件
        poCtrl1.webOpen("doc/test.xls", OpenModeType.xlsNormalEdit, "张三");
    复制代码

      3. 生成文件的效果:

      

    四、高级调用方式

      1. 编码生成Excel

    复制代码
        Workbook wb = new Workbook();
        // 设置背景
        Table backGroundTable = wb.openSheet("Sheet1").openTable("A1:P200");
        backGroundTable.getBorder().setLineColor(Color.white);
    
        // 设置标题
        wb.openSheet("Sheet1").openTable("A1:H2").merge();
        wb.openSheet("Sheet1").openTable("A1:H2").setRowHeight(30);
        Cell A1 = wb.openSheet("Sheet1").openCell("A1");
        A1.setHorizontalAlignment(XlHAlign.xlHAlignCenter);
        A1.setVerticalAlignment(XlVAlign.xlVAlignCenter);
        A1.setForeColor(new Color(0, 128, 128));
        A1.setValue("出差开支预算");
        
        //设置字体
        wb.openSheet("Sheet1").openTable("A1:A1").getFont().setBold(true);
        wb.openSheet("Sheet1").openTable("A1:A1").getFont().setSize(25);
        
        // 画表头    
        Border C4Border = wb.openSheet("Sheet1").openTable("C4:C4").getBorder();
        C4Border.setWeight(XlBorderWeight.xlThick);
        C4Border.setLineColor(Color.yellow);
        
        Table titleTable = wb.openSheet("Sheet1").openTable("B4:H5");
        titleTable.getBorder().setBorderType(XlBorderType.xlAllEdges);
        titleTable.getBorder().setWeight(XlBorderWeight.xlThick);
        titleTable.getBorder().setLineColor(new Color(0, 128, 128));
        
        // 画表体
        Table bodyTable = wb.openSheet("Sheet1").openTable("B6:H15");
        bodyTable.getBorder().setLineColor(Color.gray);
        bodyTable.getBorder().setWeight(XlBorderWeight.xlHairline);
    
        Border B7Border = wb.openSheet("Sheet1").openTable("B7:B7").getBorder();
        B7Border.setLineColor(Color.white);
    
        Border B9Border = wb.openSheet("Sheet1").openTable("B9:B9").getBorder();
        B9Border.setBorderType(XlBorderType.xlBottomEdge);
        B9Border.setLineColor(Color.white);
    
        Border C6C15BorderLeft = wb.openSheet("Sheet1").openTable("C6:C15").getBorder();
        C6C15BorderLeft.setLineColor(Color.white);
        C6C15BorderLeft.setBorderType(XlBorderType.xlLeftEdge);
        
        Border C6C15BorderRight = wb.openSheet("Sheet1").openTable("C6:C15").getBorder();
        C6C15BorderRight.setLineColor(Color.yellow);
        C6C15BorderRight.setLineStyle(XlBorderLineStyle.xlDot);
        C6C15BorderRight.setBorderType(XlBorderType.xlRightEdge);
    
        Border E6E15Border = wb.openSheet("Sheet1").openTable("E6:E15").getBorder();
        E6E15Border.setLineStyle(XlBorderLineStyle.xlDot);
        E6E15Border.setBorderType(XlBorderType.xlAllEdges);
        E6E15Border.setLineColor(Color.yellow);
    
        Border G6G15BorderRight = wb.openSheet("Sheet1").openTable("G6:G15").getBorder();
        G6G15BorderRight.setBorderType(XlBorderType.xlRightEdge);
        G6G15BorderRight.setLineColor(Color.white);
    
        Border G6G15BorderLeft = wb.openSheet("Sheet1").openTable("G6:G15").getBorder();
        G6G15BorderLeft.setLineStyle(XlBorderLineStyle.xlDot);
        G6G15BorderLeft.setBorderType(XlBorderType.xlLeftEdge);
        G6G15BorderLeft.setLineColor(Color.yellow);
    
        Table bodyTable2 = wb.openSheet("Sheet1").openTable("B6:H15");
        bodyTable2.getBorder().setWeight(XlBorderWeight.xlThick);
        bodyTable2.getBorder().setLineColor(new Color(0, 128, 128));
        bodyTable2.getBorder().setBorderType(XlBorderType.xlAllEdges);
    
        // 画表尾
        Border H16H17Border = wb.openSheet("Sheet1").openTable("H16:H17").getBorder();
        H16H17Border.setLineColor(new Color(204, 255, 204));
    
        Border E16G17Border = wb.openSheet("Sheet1").openTable("E16:G17").getBorder();
        E16G17Border.setLineColor(new Color(0, 128, 128));
    
        Table footTable = wb.openSheet("Sheet1").openTable("B16:H17");
        footTable.getBorder().setWeight(XlBorderWeight.xlThick);
        footTable.getBorder().setLineColor(new Color(0, 128, 128));
        footTable.getBorder().setBorderType(XlBorderType.xlAllEdges);
    
        // 设置行高列宽
        wb.openSheet("Sheet1").openTable("A1:A1").setColumnWidth(1);
        wb.openSheet("Sheet1").openTable("B1:B1").setColumnWidth(20);
        wb.openSheet("Sheet1").openTable("C1:C1").setColumnWidth(15);
        wb.openSheet("Sheet1").openTable("D1:D1").setColumnWidth(10);
        wb.openSheet("Sheet1").openTable("E1:E1").setColumnWidth(8);
        wb.openSheet("Sheet1").openTable("F1:F1").setColumnWidth(3);
        wb.openSheet("Sheet1").openTable("G1:G1").setColumnWidth(12);
        wb.openSheet("Sheet1").openTable("H1:H1").setColumnWidth(20);
    
        wb.openSheet("Sheet1").openTable("A16:A16").setRowHeight(20);
        wb.openSheet("Sheet1").openTable("A17:A17").setRowHeight(20);
    
        // 设置表格中字体大小为10
        for (int i = 0; i < 12; i++) {//excel表格行号
            for (int j = 0; j < 7; j++) {//excel表格列号
                wb.openSheet("Sheet1").openCellRC(4 + i, 2 + j).getFont().setSize(10);
            }
        }
    
        // 填充单元格背景颜色
        for (int i = 0; i < 10; i++) {
            wb.openSheet("Sheet1").openCell("H" + (6 + i)).setBackColor(new Color(255, 255, 153));
        }
    
        wb.openSheet("Sheet1").openCell("E16").setBackColor(new Color(0, 128, 128));
        wb.openSheet("Sheet1").openCell("F16").setBackColor(new Color(0, 128, 128));
        wb.openSheet("Sheet1").openCell("G16").setBackColor(new Color(0, 128, 128));
        wb.openSheet("Sheet1").openCell("E17").setBackColor(new Color(0, 128, 128));
        wb.openSheet("Sheet1").openCell("F17").setBackColor(new Color(0, 128, 128));
        wb.openSheet("Sheet1").openCell("G17").setBackColor(new Color(0, 128, 128));
        wb.openSheet("Sheet1").openCell("H16").setBackColor(new Color(204, 255, 204));
        wb.openSheet("Sheet1").openCell("H17").setBackColor(new Color(204, 255, 204));
    
        //填充单元格文本和公式
        Cell B4 = wb.openSheet("Sheet1").openCell("B4");
        B4.getFont().setBold(true);
        B4.setValue("出差开支预算");
        Cell H5 = wb.openSheet("Sheet1").openCell("H5");
        H5.getFont().setBold(true);
        H5.setValue("总计");
        H5.setHorizontalAlignment(XlHAlign.xlHAlignCenter);
        Cell B6 = wb.openSheet("Sheet1").openCell("B6");
        B6.getFont().setBold(true);
        B6.setValue("飞机票价");
        Cell B9 = wb.openSheet("Sheet1").openCell("B9");
        B9.getFont().setBold(true);
        B9.setValue("酒店");
        Cell B11 = wb.openSheet("Sheet1").openCell("B11");
        B11.getFont().setBold(true);
        B11.setValue("餐饮");
        Cell B12 = wb.openSheet("Sheet1").openCell("B12");
        B12.getFont().setBold(true);
        B12.setValue("交通费用");
        Cell B13 = wb.openSheet("Sheet1").openCell("B13");
        B13.getFont().setBold(true);
        B13.setValue("休闲娱乐");
        Cell B14 = wb.openSheet("Sheet1").openCell("B14");
        B14.getFont().setBold(true);
        B14.setValue("礼品");
        Cell B15 = wb.openSheet("Sheet1").openCell("B15");
        B15.getFont().setBold(true);
        B15.getFont().setSize(10);
        B15.setValue("其他费用");
    
        wb.openSheet("Sheet1").openCell("C6").setValue("机票单价(往)");
        wb.openSheet("Sheet1").openCell("C7").setValue("机票单价(返)");
        wb.openSheet("Sheet1").openCell("C8").setValue("其他");
        wb.openSheet("Sheet1").openCell("C9").setValue("每晚费用");
        wb.openSheet("Sheet1").openCell("C10").setValue("其他");
        wb.openSheet("Sheet1").openCell("C11").setValue("每天费用");
        wb.openSheet("Sheet1").openCell("C12").setValue("每天费用");
        wb.openSheet("Sheet1").openCell("C13").setValue("总计");
        wb.openSheet("Sheet1").openCell("C14").setValue("总计");
        wb.openSheet("Sheet1").openCell("C15").setValue("总计");
    
        wb.openSheet("Sheet1").openCell("G6").setValue("  张");
        wb.openSheet("Sheet1").openCell("G7").setValue("  张");
        wb.openSheet("Sheet1").openCell("G9").setValue("  晚");
        wb.openSheet("Sheet1").openCell("G10").setValue("  晚");
        wb.openSheet("Sheet1").openCell("G11").setValue("  天");
        wb.openSheet("Sheet1").openCell("G12").setValue("  天");
    
        wb.openSheet("Sheet1").openCell("H6").setFormula("=D6*F6");
        wb.openSheet("Sheet1").openCell("H7").setFormula("=D7*F7");
        wb.openSheet("Sheet1").openCell("H8").setFormula("=D8*F8");
        wb.openSheet("Sheet1").openCell("H9").setFormula("=D9*F9");
        wb.openSheet("Sheet1").openCell("H10").setFormula("=D10*F10");
        wb.openSheet("Sheet1").openCell("H11").setFormula("=D11*F11");
        wb.openSheet("Sheet1").openCell("H12").setFormula("=D12*F12");
        wb.openSheet("Sheet1").openCell("H13").setFormula("=D13*F13");
        wb.openSheet("Sheet1").openCell("H14").setFormula("=D14*F14");
        wb.openSheet("Sheet1").openCell("H15").setFormula("=D15*F15");
    
        for (int i = 0; i < 10; i++) {
            //设置数据以货币形式显示
            wb.openSheet("Sheet1").openCell("D" + (6 + i)).setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
            wb.openSheet("Sheet1").openCell("H" + (6 + i)).setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
        }
    
        Cell E16 = wb.openSheet("Sheet1").openCell("E16");
        E16.getFont().setBold(true);
        E16.getFont().setSize(11);
        E16.setForeColor(Color.white);
        E16.setValue("出差开支总费用");
        E16.setVerticalAlignment(XlVAlign.xlVAlignCenter);
        Cell E17 = wb.openSheet("Sheet1").openCell("E17");
        E17.getFont().setBold(true);
        E17.getFont().setSize(11);
        E17.setForeColor(Color.white);
        E17.setFormula("=IF(C4>H16,"低于预算","超出预算")");
        E17.setVerticalAlignment(XlVAlign.xlVAlignCenter);
        Cell H16 = wb.openSheet("Sheet1").openCell("H16");
        H16.setVerticalAlignment(XlVAlign.xlVAlignCenter);
        H16.setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
        H16.getFont().setName("Arial");
        H16.getFont().setSize(11);
        H16.getFont().setBold(true);
        H16.setFormula("=SUM(H6:H15)");
        Cell H17 = wb.openSheet("Sheet1").openCell("H17");
        H17.setVerticalAlignment(XlVAlign.xlVAlignCenter);
        H17.setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
        H17.getFont().setName("Arial");
        H17.getFont().setSize(11);
        H17.getFont().setBold(true);
        H17.setFormula("=(C4-H16)");
    
        // 填充数据
        Cell C4 = wb.openSheet("Sheet1").openCell("C4");
        C4.setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
        C4.setValue("2500");
        Cell D6 = wb.openSheet("Sheet1").openCell("D6");
        D6.setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
        D6.setValue("1200");
        wb.openSheet("Sheet1").openCell("F6").getFont().setSize(10);
        wb.openSheet("Sheet1").openCell("F6").setValue("1");
        Cell D7 = wb.openSheet("Sheet1").openCell("D7");
        D7.setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
        D7.setValue("875");
        wb.openSheet("Sheet1").openCell("F7").setValue("1");
    
        //打开文件
        PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
        poCtrl1.setWriter(wb);
        poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); 
        poCtrl1.webOpen("doc/test.xls", OpenModeType.xlsNormalEdit, "");
    复制代码

      2. 生成Excel的效果:

      

    五、源码下载

      1. 访问:http://www.zhuozhengsoft.com/dowm/,下载PageOffice for Java;

      2. 解压PageOffice开发包,拷贝Samples4文件夹到Tomcat的Webapps目录下,访问:http://localhost:8080/Samples4/index.html;

      3. 查看示例:一、23、对Excel中的一块区域赋值,并自动增加行;三、8、完全编程实现动态生成Excel文件

  • 相关阅读:
    vue中form表单监听回车事件
    vue子组件传值父组件,父组件传值子组件
    决策树(统计学习方法(李航))的贷款的例子的实现
    pycharm2019破解
    当vps服务器被墙,如果用xshell连接
    富强民主文明和谐 点击事件变颜色
    java实现字符串翻转
    宝塔面板管理阿里云服务器FTP不能用
    linux学习历程
    计算页码,统计0到9数字出现的次数
  • 原文地址:https://www.cnblogs.com/qq742655/p/9024364.html
Copyright © 2011-2022 走看看