日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ireport 使用list数据源

發(fā)布時間:2023/12/18 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ireport 使用list数据源 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

剛開始學(xué)ireport是使用sql作為數(shù)據(jù)源,因為直接sql是最簡單也最容易上手的,但是sql已經(jīng)無法滿足現(xiàn)在的業(yè)務(wù)需求了,于是乎尋找其他的解決方案,于是乎找到了JRDataSource數(shù)據(jù)源。個人覺得sql數(shù)據(jù)源最簡單,其次就是JRDataSource數(shù)據(jù)源了。

一、使用JRDataSource數(shù)據(jù)源首先要實現(xiàn) JRRewindableDataSource 接口

/*** @Author: BlueSky* @CreateTime: 2020-01-06 13:50* @Description:*/import java.util.List;import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRField; import net.sf.jasperreports.engine.JRRewindableDataSource;public class GenericDataSource implements JRRewindableDataSource{int index;List<PrintRecord> records;public GenericDataSource(List<PrintRecord> records) {this.records = records;this.index = -1;}@Overridepublic Object getFieldValue(JRField jrField) throws JRException {String fieldName = jrField.getName();return records.get(index).getValue(fieldName);}@Overridepublic boolean next() throws JRException {++index;return index < records.size();}public void moveFirst() {} }

二、新建一個通用打印的類

/*** @Author: BlueSky* @CreateTime: 2020-01-06 13:51* @Description:*/import java.util.HashMap; import java.util.Map;public class PrintRecord {public static final int KEY_NOT_FOUND = -1;Map<String, Object> vals;public PrintRecord() {vals = new HashMap<String, Object>();}public void setValue(String key, Object value) {vals.put(key, value);}public Object getValue(String key) {Object val = vals.get(key);if (val == null)return "";elsereturn val;}public int getKeyNum() {return vals.size();} }

三、在java中封裝所需要打印的參數(shù)數(shù)據(jù)(ps:詳情見第六步代碼)

四、在需要打印的報表中新建數(shù)據(jù)接收參數(shù)

  • parameters -> add?
  • list集合參數(shù)需要注意下參數(shù)類型,這里需要把?Parameter Class 設(shè)置為?net.sf.jasperreports.engine.JRDataSource 類型。(ps:下拉框中沒有的話直接復(fù)制上述類型粘貼到它下拉框中)
  • 五、剛剛新建接收的list數(shù)據(jù)還無法直接在當(dāng)前輸出,我們還需要新建一個子報表,通過它來輸出list內(nèi)容

  • 窗口 -> 打開組件面板?-> subreport 拉取它到相應(yīng)位置
  • 設(shè)置子報表Parameters 為裝list的參數(shù)
  • 去往子報表中 field 添加list集合中的屬性
  • 然后把添加的 field 拖到 Detail Brand中就能顯示了
  • 六、查看成果

    public static void main(String args[]) {try {// 獲取源文件String path = "C:\\Users\\25109\\Desktop\\    \\iReportModel\\銷售合同報表√\\javabean\\testBean.jasper";TestModel testModel = new TestModel();testModel.setProvice("湖南");testModel.setCity("長沙");TestModel testModel2 = new TestModel();testModel2.setProvice("湖南");testModel2.setCity("株洲");TestModel testModel3 = new TestModel();testModel3.setProvice("湖北");testModel3.setCity("武漢");List<TestModel> testModelList = new ArrayList<>(); //需要打印的list數(shù)據(jù)testModelList.add(testModel);testModelList.add(testModel2);testModelList.add(testModel3);HashMap<String, Object> map = new HashMap<String, Object>();List<PrintRecord> printRecordList = new ArrayList<>(); // 封裝后的數(shù)據(jù)集合for (TestModel p : testModelList) {PrintRecord pr = new PrintRecord();pr.setValue("provice",p.getProvice()); // 子報表需要建立對應(yīng)的fieldpr.setValue("city",p.getCity());printRecordList.add(pr);}map.put("data",new GenericDataSource(printRecordList)); // 主報表需要建立名為data的 Parametersmap.put("staticParam","靜態(tài)參數(shù)");JasperPrint jasperPrint = JasperFillManager.fillReport(path, map, new JREmptyDataSource());if(jasperPrint != null && jasperPrint.getPages().size() > 0){JasperViewer jasperViewer = new JasperViewer(jasperPrint,false); //以視圖方式進(jìn)行預(yù)覽jasperViewer.setVisible(true);jasperViewer.setAlwaysOnTop(true);jasperViewer.getGraphicsConfiguration().getDevice().setFullScreenWindow(jasperViewer);}else {System.err.println("報表內(nèi)容為空暫不輸出");}}catch (Exception e){System.err.println(e.getMessage());e.printStackTrace();}}

    點擊下載本案例

    總結(jié)

    以上是生活随笔為你收集整理的ireport 使用list数据源的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。