开源Excel报表工具:jxls

    用Excel出报表是我们办公中经常有的事情。如何通过查询数据库然后生成报表呢?现在有很多开源的工具可以实现这个需求,当然也有很多收费的工具。介绍一下开源Excel报表工具jxls。jxls的官方网站(http://jxls.sourceforge.net/index.html)。jxls内部是用的POI,目前对应的版本是3.2。

  jxls主要有以下特点:

  • Using SQL queries directly in XLS templates
  • Simple property access notation
  • Full expression language support
  • Complex object graph export
  • Flexible collection export
  • Flow-Control Tags support
  • Dynamic grouping of data
  • Export of a single collection into multiple worksheets
  • Adjacent tables support!
  • Complex formulas support
  • Charts, Macros and many other Excel features in XLS template
  • Dynamic Outlines
  • Dynamic Columns Hiding
  • Dynamic Cell Style processing through custom Processors
  • JDBC ResultSet export
  • Merged Cells support
  • Multiple bean properties in a single cell
  • Reading XLS files

   要使用jxls的话,需要另外的一些相关的包。

  • POI 3.2
  • Commons BeanUtils
  • Commons Collections
  • Commons JEXL
  • Commons Logging
  • Commons Digester

   官方的下载包里面有很多例子。可以参照例子自己建Excel模板,然后通过程序填充数据就可以生成报表了。

程序例子:

public class ChartSample {
    private static String templateFileName = "d:/chart.xls";
    private static String destFileName = "d:/chart_output.xls";

    public static void main(String[] args) throws IOException, ParsePropertyException {
        if (args.length >= 2) {
            templateFileName = args[0];
            destFileName = args[1];
        }
        List staff = new ArrayList();
        staff.add(new Employee("Derek", 35, 3000, 0.30));
        staff.add(new Employee("Elsa", 28, 1500, 0.15));
        staff.add(new Employee("Oleg", 32, 2300, 0.25));
        staff.add(new Employee("Neil", 34, 2500, 0.00));
        staff.add(new Employee("Maria", 34, 1700, 0.15));
        staff.add(new Employee("John", 35, 2800, 0.20));
        staff.add(new Employee("Leonid", 29, 1700, 0.20));
        Map beans = new HashMap();
        beans.put("employee", staff);
        XLSTransformer transformer = new XLSTransformer();
        transformer.markAsFixedSizeCollection("employee");
        transformer.transformXLS(templateFileName, beans, destFileName);
    }
}

 

生成结果如图所示:

开源Excel报表工具:jxls_第1张图片

你可能感兴趣的:(properties,String,Excel,HashMap,工具,报表)