FineReport應用 – 編程實現普通分組報表 | #報表

來源:互聯網
上載者:User

 

  我在 FineReport應用 - 程式網路報表Hello,World 一文中,闡述了編程實現基本報表的方法,在本文中,我將介紹如何建立一個帶有資料集的分組資料報表。

 

設計目標

  設計一張報表,按付款條件分組查出訂單編號和金額,最終效果如:

 

 

 

 

配置資料連線

  要使用資料集,需要先在伺服器中配置資料連線。設定檔位於

 

  %FR_HOME%/WebReport/ WEB-INF/resources/datasource.xml

 

  以FRDemo為例,配置如下:

 

 

<?xml version="1.0" encoding="UTF-8"?>

<DatasourceManager>

  <ConnectionMap>

    <Connection name="FRDemo" class="com.fr.data.impl.JDBCDatabaseConnection">

      <DatabaseAttr />

      <JDBCDatabaseAttr url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:/FineReport6.5/WebReport/WEB-INF/resources/data/FRDemo.mdb" driver="sun.jdbc.odbc.JdbcOdbcDriver" user="" password="" encryptPassword="true" />

    </Connection>

  </ConnectionMap>

</DatasourceManager> 

 

添加報表資料集

  在前文中,已經介紹過如何建立一個基本的程式報表,這時不再贅述。

 

  建立一個報表

 

 

 

package fr.report;      

 

import com.fr.base.Constants;

import com.fr.base.FRContext;

import com.fr.base.NameStyle;

//...

 

/**    

 * 普通分組報表

 * 

 * http://localhost:8080/FineReport/ReportServer?reportlet=fr.report.GroupReportletDemo

 */     

public class GroupReportletDemo implements Reportlet {      

 

    public Report createReport(ReportletRequest req) {      

        // 建立報表      

        WorkSheet workSheet = new WorkSheet();

        return workSheet;

    }

}

 

 要添加資料集,在createReport(...)方法中添加如下代碼:

 

 

try {      

// 使用資料連線FRDemo

DatabaseConnection dbConn = new NameDatabaseConnection("FRDemo");

// 定義資料集,這裡我們只取前20條記錄     

TableData tableData = new DBTableData(dbConn, "SELECT top 20 * FROM ORDERS");

     // 將定義的資料集添加到報表中,命名為ds1      

     workSheet.putTableData("ds1", tableData);      

} catch (Exception exp) {

FRContext.getLogger().severe(exp.getMessage());

    exp.printStackTrace();      

}      

添加資料列

  表樣:


  

 

  資料列:

 

  

 

 添加資料列:

// 建立擴充屬性並指定為從上到下擴充

CellExpandAttr cellExpandAttr = new CellExpandAttr();

cellExpandAttr.setDirection(Constants.TOP_TO_BOTTOM);

// 建立一個取自ds1資料集PAYMETHOD欄位的資料列

DSColumn a2data = new DSColumn();

a2data.setDSName("ds1");

a2data.setColumn(TableDataColumn.createColumn("PAYMETHOD"));

// 設定資料配置為“分組 普通”

a2data.setGrouper(new FunctionGrouper());

// 將建立的資料資料行繫結到A2儲存格中

CellElement a2 = new DefaultCellElement(0, 1, a2data);

// 應用擴充屬性

a2.setCellExpandAttr(cellExpandAttr);

// 添加儲存格到報表中

workSheet.addCellElement(a2);

  依例設定b2、c2儲存格,添加表頭的方法前文已經介紹過,這裡略過。

 

   注意

 

new DefaultCellElement(col, row, value)中,col和row分別為列和行的索引,如0,0表示a1,1,1表示B2
 美化報表

  通常為了美觀,我們會給不同的儲存格加上樣式,FineReport為我們提供了幾種預定義的樣式,

  見:%FR_HOME%/WebReport/WEB-INF/resources/config.xml

  要使用預定義的樣式,只需要加入如下的代碼即可:

 // 表頭樣式

Style headStyle = NameStyle.getInstance("Head");

// 表體樣式

Style cellStyle = NameStyle.getInstance("Cell");

a1.setStyle(headStyle);

a2.setStyle(headStyle);

完整代碼
package fr.report;      import com.fr.base.Constants;import com.fr.base.FRContext;import com.fr.base.NameStyle;import com.fr.base.Style;import com.fr.data.TableData;import com.fr.data.impl.DBTableData;import com.fr.data.impl.DatabaseConnection;import com.fr.data.impl.NameDatabaseConnection;import com.fr.report.CellElement;import com.fr.report.DefaultCellElement;import com.fr.report.Report;import com.fr.report.WorkSheet;import com.fr.report.cellElement.CellExpandAttr;import com.fr.report.cellElement.TableDataColumn;import com.fr.report.cellElement.core.DSColumn;import com.fr.report.cellElement.core.FunctionGrouper;import com.fr.web.Reportlet;import com.fr.web.ReportletRequest;/**     * 普通分組報表 *  * http://localhost:8080/FineReport/ReportServer?reportlet=fr.report.GroupReportletDemo */     public class GroupReportletDemo implements Reportlet {          public Report createReport(ReportletRequest req) {              // 建立報表              WorkSheet workSheet = new WorkSheet();        try {              // 定義資料連線        DatabaseConnection dbConn = new NameDatabaseConnection("FRDemo");                    // 定義資料集              TableData tableData = new DBTableData(dbConn, "SELECT top 20 * FROM ORDERS");            // 添加資料集ds1                  workSheet.putTableData("ds1", tableData);                              // 表頭樣式            Style headStyle = NameStyle.getInstance("Head");            // 表體樣式            Style cellStyle = NameStyle.getInstance("cell");                        // 添加表頭        CellElement a1 = new DefaultCellElement(0, 0, "PAYMETHOD");        a1.setStyle(headStyle);    workSheet.addCellElement(a1);         CellElement b1 = new DefaultCellElement(1, 0, "ORDERID");    b1.setStyle(headStyle);    workSheet.addCellElement(b1);        CellElement c1 = new DefaultCellElement(2, 0, "AMOUNT");    c1.setStyle(headStyle);    workSheet.addCellElement(c1);        // 添加表體        CellExpandAttr cellExpandAttr = new CellExpandAttr();        cellExpandAttr.setDirection(Constants.TOP_TO_BOTTOM);            DSColumn a2data = new DSColumn();    a2data.setDSName("ds1");    a2data.setGrouper(new FunctionGrouper());    a2data.setColumn(TableDataColumn.createColumn("PAYMETHOD"));            CellElement a2 = new DefaultCellElement(0, 1, a2data);    a2.setCellExpandAttr(cellExpandAttr);    workSheet.addCellElement(a2);    a2.setStyle(cellStyle);     DSColumn b2data = new DSColumn();    b2data.setDSName("ds1");    b2data.setGrouper(new FunctionGrouper());    b2data.setColumn(TableDataColumn.createColumn("ORDERID"));            CellElement b2 = new DefaultCellElement(1, 1, b2data);    b2.setCellExpandAttr(cellExpandAttr);    workSheet.addCellElement(b2);    b2.setStyle(cellStyle);        DSColumn c2data = new DSColumn();    c2data.setDSName("ds1");    c2data.setGrouper(new FunctionGrouper());    c2data.setColumn(TableDataColumn.createColumn("AMOUNT"));            CellElement c2 = new DefaultCellElement(2, 1, c2data);    c2.setCellExpandAttr(cellExpandAttr);    workSheet.addCellElement(c2);    c2.setStyle(cellStyle);        } catch (Exception exp) {        FRContext.getLogger().severe(exp.getMessage());            exp.printStackTrace();              }              return workSheet;    }}

 

  要預覽報表,訪問

 

  http://localhost:8080/FineReport/ReportServer?reportlet=fr.report.GroupReportletDemo

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.