zoukankan      html  css  js  c++  java
  • ExcelReport第一篇:使用ExcelReport导出Excel

    转自: https://www.cnblogs.com/hanzhaoxin/p/4232575.html 

    导航

    目   录:基于NPOI的报表引擎——ExcelReport

    下一篇:ExcelReport源码解析

    概述

    本篇将通过导出学生成绩的示例演示“使用ExcelReport导出Excel”的步骤。

    示例(导出学生成绩明细)

    步骤一:设计Excel模板

    首先,使用Excel软件设计模板(我没有安装MSOffice,用LibreOffice Calc做的演示)。在模板中要填充数据的位置用$[ParameterName]代替。

    image

    步骤二:生成模板填充规则文件

    使用模板填充规则文件生成工具(ERTool)为步骤一设计的Excel模板生成填充规则文件。

    image

    步骤三:为模板填充数据

     

    实现代码(导出到本地):

    //实例化一个参数容器,并加载模板填充规则文件
    ParameterCollection collection = new ParameterCollection();
    collection.Load(@"TemplateTemplate.xml");
     
    //实例化一个元素格式化器列表
    List<ElementFormatter> formatters = new List<ElementFormatter>();
    formatters.Add(new CellFormatter(collection["GradeDetail", "Dept"], "某某学院"));   //添加一个单元格格式化器
    formatters.Add(new CellFormatter(collection["GradeDetail", "Class"], "某某班级"));
    formatters.Add(new CellFormatter(collection["GradeDetail", "StudNo"], "2009*****"));
    formatters.Add(new CellFormatter(collection["GradeDetail", "StudName"], "韩兆新"));
    formatters.Add(new CellFormatter(collection["GradeDetail", "ExportDate"], DateTime.Now));
     
    List<GradeInfo> gradeInfoList = new List<GradeInfo>();
    gradeInfoList.Add(new GradeInfo() { CGPA = 18, CourseID = "KC-0001", CourseName = "高等数学", CourseType = "理论课", Credit = 6, EvaluationMode = "考试", GainCredit = 6, GPA = 3, Grade = 86, StudyNature = "初修", Type = "必修课" });
    gradeInfoList.Add(new GradeInfo() { CGPA = 2, CourseID = "KC-0002", CourseName = "计算机应用基础", CourseType = "理论课", Credit = 2, EvaluationMode = "考试", GainCredit = 2, GPA = 1, Grade = 93, StudyNature = "初修", Type = "必修课" });
    gradeInfoList.Add(new GradeInfo() { CGPA = 9, CourseID = "KC-0003", CourseName = "C程序设计", CourseType = "理论课", Credit = 3, EvaluationMode = "考试", GainCredit = 3, GPA = 3, Grade = 97, StudyNature = "初修", Type = "必修课", Remark = "备注信息" });
     
    //添加一个Table格式化器
    formatters.Add(new TableFormatter<GradeInfo>(collection["GradeDetail", "CourseID"].X, gradeInfoList,
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CGPA"].Y, t => t.CGPA),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseID"].Y, t => t.CourseID),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseName"].Y, t => t.CourseName),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseType"].Y, t => t.CourseType),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Credit"].Y, t => t.Credit),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "EvaluationMode"].Y, t => t.EvaluationMode),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "GainCredit"].Y, t => t.GainCredit),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "GPA"].Y, t => t.GPA),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Grade"].Y, t => t.Grade),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Remark"].Y, t => t.Remark),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "StudyNature"].Y, t => t.StudyNature),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Type"].Y, t => t.Type)
        ));
    //导出文件到本地
    Export.ExportToLocal(@"TemplateTemplate.xls", saveFileDlg.FileName,
        new SheetFormatterContainer("GradeDetail", formatters));

    实现代码(导出到Web):

    //实例化一个参数容器,并加载模板填充规则文件
    ExcelReport.ParameterCollection collection = new ExcelReport.ParameterCollection();
    collection.Load(Server.MapPath(@"TemplateTemplate.xml"));
     
    //实例化一个元素格式化器列表
    List<ElementFormatter> formatters = new List<ElementFormatter>();
    formatters.Add(new CellFormatter(collection["GradeDetail", "Dept"], "某某学院"));   //添加一个单元格格式化器
    formatters.Add(new CellFormatter(collection["GradeDetail", "Class"], "某某班级"));
    formatters.Add(new CellFormatter(collection["GradeDetail", "StudNo"], "2009*****"));
    formatters.Add(new CellFormatter(collection["GradeDetail", "StudName"], "韩兆新"));
    formatters.Add(new CellFormatter(collection["GradeDetail", "ExportDate"], DateTime.Now));
     
    List<GradeInfo> gradeInfoList = new List<GradeInfo>();
    gradeInfoList.Add(new GradeInfo() { CGPA = 18, CourseID = "KC-0001", CourseName = "高等数学", CourseType = "理论课", Credit = 6, EvaluationMode = "考试", GainCredit = 6, GPA = 3, Grade = 86, StudyNature = "初修", Type = "必修课" });
    gradeInfoList.Add(new GradeInfo() { CGPA = 2, CourseID = "KC-0002", CourseName = "计算机应用基础", CourseType = "理论课", Credit = 2, EvaluationMode = "考试", GainCredit = 2, GPA = 1, Grade = 93, StudyNature = "初修", Type = "必修课" });
    gradeInfoList.Add(new GradeInfo() { CGPA = 9, CourseID = "KC-0003", CourseName = "C程序设计", CourseType = "理论课", Credit = 3, EvaluationMode = "考试", GainCredit = 3, GPA = 3, Grade = 97, StudyNature = "初修", Type = "必修课", Remark = "备注信息" });
     
    //添加一个Table格式化器
    formatters.Add(new TableFormatter<GradeInfo>(collection["GradeDetail", "CourseID"].X, gradeInfoList,
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CGPA"].Y, t => t.CGPA),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseID"].Y, t => t.CourseID),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseName"].Y, t => t.CourseName),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseType"].Y, t => t.CourseType),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Credit"].Y, t => t.Credit),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "EvaluationMode"].Y, t => t.EvaluationMode),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "GainCredit"].Y, t => t.GainCredit),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "GPA"].Y, t => t.GPA),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Grade"].Y, t => t.Grade),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Remark"].Y, t => t.Remark),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "StudyNature"].Y, t => t.StudyNature),
        new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Type"].Y, t => t.Type)
        ));
    //导出文件到Web
    Export.ExportToWeb(Server.MapPath(@"TemplateTemplate.xls"), "GradeDetail",
        new SheetFormatterContainer("GradeDetail", formatters));

    导出文件截图:

    image

    源码下载:

    image

  • 相关阅读:
    Android广播机制概述
    [AJAX系列]$.post(url,[data],[fn],[type])
    [AJAX系列]$.get(url,[data],[fn],[type])
    [AJAX系列]onreadystatechange事件
    [AJAX系列]XMLHttpResponse对象
    [AJAX系列]XMLHttpRequest请求
    [Ajax系列]Ajax介绍
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.4:create (default-cli) on project standalone-pom: Unable to parse configuration of 3: mojo org.apache.maven.plugins:
    Failed to execute goal org.codehaus.cargo:cargo-maven2-plugin:1.2.3:run (default-cli) on project Maven_WebTest: Execution default-cli of goal org.codehaus.cargo:cargo-maven2-plugin:1.2.3:run failed: C
    The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path。
  • 原文地址:https://www.cnblogs.com/turnip/p/12916925.html
Copyright © 2011-2022 走看看