jasper+ireport实现table控件用list作为数据源
需求是主报表中加入两个或者两个以上的table,也是刚刚发现table可以水平放置。主报表的数据源是json,table的是list<String>,这里没有用javabean所以当你有一组数据的时候,建议你建立一个size的List,这样才不会丢失数据,这里贴出代码,知道拷贝翟铁就可以出结果。
本文版权归__馋猫和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
先给出效果图,效果图如下↓
Java代码TableJsonTest.java如下↓
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package a.test.xmlfile; 2 3 import java.io.BufferedReader; 4 import java.io.ByteArrayInputStream; 5 import java.io.FileInputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 9 import java.util.ArrayList; 10 import java.util.HashMap; 11 import java.util.List; 12 import java.util.Locale; 13 14 import net.sf.jasperreports.engine.JRParameter; 15 import net.sf.jasperreports.engine.JasperCompileManager; 16 import net.sf.jasperreports.engine.JasperFillManager; 17 import net.sf.jasperreports.engine.JasperPrint; 18 import net.sf.jasperreports.engine.JasperReport; 19 import net.sf.jasperreports.engine.design.JasperDesign; 20 import net.sf.jasperreports.engine.query.JsonQueryExecuterFactory; 21 import net.sf.jasperreports.engine.xml.JRXmlLoader; 22 import net.sf.jasperreports.view.JasperViewer; 23 24 public class TableJsonTest { 25 public static String ReadFile(String Path) { 26 BufferedReader reader = null; 27 String laststr = ""; 28 try { 29 FileInputStream fileInputStream = new FileInputStream(Path); 30 InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); 31 reader = new BufferedReader(inputStreamReader); 32 String tempString = null; 33 while ((tempString = reader.readLine()) != null) { 34 laststr += tempString; 35 } 36 reader.close(); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } finally { 40 if (reader != null) { 41 try { 42 reader.close(); 43 } catch (IOException e) { 44 e.printStackTrace(); 45 } 46 } 47 } 48 return laststr; 49 } 50 51 public static void main(String[] args) { 52 53 List<String> nameList = new ArrayList<>(); 54 nameList.add("name1"); 55 nameList.add("name2"); 56 nameList.add("name3"); 57 nameList.add("name4"); 58 nameList.add("name5"); 59 60 List<String> ageList = new ArrayList<>(); 61 ageList.add("age1"); 62 ageList.add("age2"); 63 ageList.add("age3"); 64 ageList.add("age4"); 65 ageList.add("age5"); 66 67 List<String> codeList = new ArrayList<>(); 68 codeList.add("code1"); 69 codeList.add("code2"); 70 codeList.add("code3"); 71 codeList.add("code4"); 72 codeList.add("code5"); 73 codeList.add("code6"); 74 75 List<String> personList = new ArrayList<>(); 76 personList.add("person1"); 77 personList.add("person2"); 78 personList.add("person3"); 79 personList.add("person4"); 80 personList.add("person5"); 81 personList.add("person6"); 82 // 注意更改路径 83 String s = ReadFile("F:\\Users\\Desktop\\lib\\tianjin.json"); 84 JasperDesign design; 85 try { 86 // 注意更改路径 87 design = JRXmlLoader.load("F:\\reportTest\\tableTest.jrxml"); 88 HashMap<String, Object> paramsMap = new HashMap<String, Object>(); 89 // 第二种配置数据源的方式 InputStream 90 InputStream is = new ByteArrayInputStream(s.getBytes("UTF-8")); 91 paramsMap.put("JSON_INPUT_STREAM", is); 92 paramsMap.put("ageList", ageList); 93 paramsMap.put("nameList", nameList); 94 paramsMap.put("codeList", codeList); 95 paramsMap.put("personList", personList); 96 // 配置其他参数 97 paramsMap.put(JsonQueryExecuterFactory.JSON_LOCALE, Locale.ENGLISH); 98 paramsMap.put(JRParameter.REPORT_LOCALE, Locale.US); 99 // 加载模板 100 JasperReport report = JasperCompileManager.compileReport(design); 101 // 填充数据 102 JasperPrint print = JasperFillManager.fillReport(report, paramsMap); 103 // 预览显示 104 JasperViewer.viewReport(print); 105 106 } catch (Exception e) { 107 // TODO Auto-generated catch block 108 e.printStackTrace(); 109 } 110 } 111 112 }
报表文件tableTest.jrxml如下↓
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="tableTest" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true" uuid="0c826da3-1634-4cec-8a87-8b0603713c5a"> 3 <property name="ireport.zoom" value="1.0"/> 4 <property name="ireport.x" value="0"/> 5 <property name="ireport.y" value="0"/> 6 <style name="table"> 7 <box> 8 <pen lineWidth="1.0" lineColor="#000000"/> 9 </box> 10 </style> 11 <style name="table_TH" mode="Opaque" backcolor="#F0F8FF"> 12 <box> 13 <pen lineWidth="0.5" lineColor="#000000"/> 14 </box> 15 </style> 16 <style name="table_CH" mode="Opaque" backcolor="#BFE1FF"> 17 <box> 18 <pen lineWidth="0.5" lineColor="#000000"/> 19 </box> 20 </style> 21 <style name="table_TD" mode="Opaque" backcolor="#FFFFFF"> 22 <box> 23 <pen lineWidth="0.5" lineColor="#000000"/> 24 </box> 25 </style> 26 <style name="table 1"> 27 <box> 28 <pen lineWidth="1.0" lineColor="#000000"/> 29 </box> 30 </style> 31 <style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF"> 32 <box> 33 <pen lineWidth="0.5" lineColor="#000000"/> 34 </box> 35 </style> 36 <style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF"> 37 <box> 38 <pen lineWidth="0.5" lineColor="#000000"/> 39 </box> 40 </style> 41 <style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF"> 42 <box> 43 <pen lineWidth="0.5" lineColor="#000000"/> 44 </box> 45 </style> 46 <subDataset name="Table Dataset 1" uuid="18c82022-8fbe-4952-87fb-71cf8c9b6351"> 47 <parameter name="ageList" class="java.util.List"/> 48 <parameter name="nameList" class="java.util.List"/> 49 <variable name="index" class="java.lang.Integer" calculation="Count"> 50 <variableExpression><![CDATA[$V{index}.valueOf( 01 )]]></variableExpression> 51 </variable> 52 </subDataset> 53 <subDataset name="Table Dataset 2" uuid="3aea438c-0171-4117-8e08-60e643f9b7f5"> 54 <parameter name="codeList" class="java.util.List"/> 55 <parameter name="personList" class="java.util.List"/> 56 <variable name="index" class="java.lang.Integer" calculation="Count"> 57 <variableExpression><![CDATA[$V{index}.valueOf( 01 )]]></variableExpression> 58 </variable> 59 </subDataset> 60 <parameter name="ageList" class="java.util.List"/> 61 <parameter name="nameList" class="java.util.List"/> 62 <parameter name="codeList" class="java.util.List"/> 63 <parameter name="personList" class="java.util.List"/> 64 <queryString language="json"> 65 <![CDATA[sales.item]]> 66 </queryString> 67 <field name="count" class="java.lang.String"/> 68 <field name="project" class="java.lang.String"/> 69 <field name="equipment" class="java.lang.String"/> 70 <title> 71 <band height="79" splitType="Stretch"> 72 <textField pattern="yyyy/MM/dd"> 73 <reportElement x="73" y="59" width="200" height="20" uuid="21653315-fb23-462a-b6c3-c881b33e5879"/> 74 <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression> 75 </textField> 76 <textField pattern="HH.mm.ss"> 77 <reportElement x="73" y="39" width="200" height="20" uuid="44e19c2e-cc04-4470-b335-a0ab04bb7660"/> 78 <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression> 79 </textField> 80 </band> 81 </title> 82 <columnHeader> 83 <band height="20" splitType="Stretch"> 84 <staticText> 85 <reportElement x="73" y="0" width="100" height="20" uuid="47e6b88e-bec1-45a5-8ce9-cb9ae0b711f0"/> 86 <box> 87 <topPen lineWidth="1.0"/> 88 <leftPen lineWidth="1.0"/> 89 <bottomPen lineWidth="1.0"/> 90 <rightPen lineWidth="1.0"/> 91 </box> 92 <text><![CDATA[count]]></text> 93 </staticText> 94 <staticText> 95 <reportElement x="173" y="0" width="100" height="20" uuid="655c61ce-59d3-4dc9-893e-3e88c2ea975e"/> 96 <box> 97 <topPen lineWidth="1.0"/> 98 <leftPen lineWidth="1.0"/> 99 <bottomPen lineWidth="1.0"/> 100 <rightPen lineWidth="1.0"/> 101 </box> 102 <text><![CDATA[project]]></text> 103 </staticText> 104 <staticText> 105 <reportElement x="273" y="0" width="100" height="20" uuid="9ddc2b36-8df8-4bd9-95ff-7c5229397f07"/> 106 <box> 107 <topPen lineWidth="1.0"/> 108 <leftPen lineWidth="1.0"/> 109 <bottomPen lineWidth="1.0"/> 110 <rightPen lineWidth="1.0"/> 111 </box> 112 <text><![CDATA[equipment]]></text> 113 </staticText> 114 </band> 115 </columnHeader> 116 <detail> 117 <band height="20" splitType="Stretch"> 118 <textField> 119 <reportElement x="73" y="0" width="100" height="20" uuid="064ef273-80ca-415b-a8ac-c920c89a9a3c"/> 120 <box> 121 <topPen lineWidth="0.75"/> 122 <leftPen lineWidth="0.75"/> 123 <bottomPen lineWidth="0.75"/> 124 <rightPen lineWidth="0.75"/> 125 </box> 126 <textFieldExpression><![CDATA[$F{count}]]></textFieldExpression> 127 </textField> 128 <textField> 129 <reportElement x="173" y="0" width="100" height="20" uuid="270b47b8-8435-49b3-afa2-5c78ed1209b8"/> 130 <box> 131 <topPen lineWidth="0.75"/> 132 <leftPen lineWidth="0.75"/> 133 <bottomPen lineWidth="0.75"/> 134 <rightPen lineWidth="0.75"/> 135 </box> 136 <textFieldExpression><![CDATA[$F{project}]]></textFieldExpression> 137 </textField> 138 <textField> 139 <reportElement x="273" y="0" width="100" height="20" uuid="5aa82e43-25e6-4cf1-8b61-9ed652c8bc66"/> 140 <box> 141 <topPen lineWidth="0.75"/> 142 <leftPen lineWidth="0.75"/> 143 <bottomPen lineWidth="0.75"/> 144 <rightPen lineWidth="0.75"/> 145 </box> 146 <textFieldExpression><![CDATA[$F{equipment}]]></textFieldExpression> 147 </textField> 148 </band> 149 </detail> 150 <columnFooter> 151 <band height="295"> 152 <componentElement> 153 <reportElement key="table 1" style="table 1" stretchType="RelativeToTallestObject" x="0" y="0" width="285" height="138" isPrintWhenDetailOverflows="true" uuid="b1044ef3-a755-42af-8288-e405e052d8fa"/> 154 <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd"> 155 <datasetRun subDataset="Table Dataset 2" uuid="aa932109-8a1e-4b4a-963f-9d3d27b0d141"> 156 <datasetParameter name="codeList"> 157 <datasetParameterExpression><![CDATA[$P{codeList}]]></datasetParameterExpression> 158 </datasetParameter> 159 <datasetParameter name="personList"> 160 <datasetParameterExpression><![CDATA[$P{personList}]]></datasetParameterExpression> 161 </datasetParameter> 162 <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{codeList})]]></dataSourceExpression> 163 </datasetRun> 164 <jr:column width="90" uuid="45380ad9-3fb3-4e77-94e6-b135adec06d5"> 165 <jr:columnHeader style="table 1_CH" height="30" rowSpan="1"> 166 <staticText> 167 <reportElement x="0" y="0" width="90" height="30" uuid="4b5aafe9-3993-4ab8-8258-fef3a832520b"/> 168 <text><![CDATA[NO.]]></text> 169 </staticText> 170 </jr:columnHeader> 171 <jr:detailCell style="table 1_TD" height="20" rowSpan="1"> 172 <textField> 173 <reportElement x="0" y="0" width="90" height="20" uuid="d98deaa6-6efe-4778-acc0-a2275007794e"/> 174 <textFieldExpression><![CDATA[$V{index}]]></textFieldExpression> 175 </textField> 176 </jr:detailCell> 177 </jr:column> 178 <jr:column width="90" uuid="542b5246-a72e-4bd4-aad2-fd72581733e9"> 179 <jr:columnHeader style="table 1_CH" height="30" rowSpan="1"> 180 <staticText> 181 <reportElement x="0" y="0" width="90" height="30" uuid="d406af75-214a-481a-9902-00d83686d64f"/> 182 <text><![CDATA[code]]></text> 183 </staticText> 184 </jr:columnHeader> 185 <jr:detailCell style="table 1_TD" height="20" rowSpan="1"> 186 <textField> 187 <reportElement x="0" y="0" width="90" height="20" uuid="f87906d8-ca12-476d-b842-9c81cdfa6acc"/> 188 <textFieldExpression><![CDATA[""+$P{codeList}.get($V{index}-01)]]></textFieldExpression> 189 </textField> 190 </jr:detailCell> 191 </jr:column> 192 <jr:column width="90" uuid="9f2cdf79-d001-4d20-b51c-42191421bca6"> 193 <jr:columnHeader style="table 1_CH" height="30" rowSpan="1"> 194 <staticText> 195 <reportElement x="0" y="0" width="90" height="30" uuid="1d835d3b-2c8d-4d15-8a0d-44af3fe660df"/> 196 <text><![CDATA[person]]></text> 197 </staticText> 198 </jr:columnHeader> 199 <jr:detailCell style="table 1_TD" height="20" rowSpan="1"> 200 <textField> 201 <reportElement x="0" y="0" width="90" height="20" uuid="c0f78257-a44b-4575-a2b2-1dbcd1ce72a5"/> 202 <textFieldExpression><![CDATA[""+$P{personList}.get($V{index}-01)]]></textFieldExpression> 203 </textField> 204 </jr:detailCell> 205 </jr:column> 206 </jr:table> 207 </componentElement> 208 <componentElement> 209 <reportElement key="table" style="table" stretchType="RelativeToTallestObject" x="285" y="0" width="270" height="138" isPrintWhenDetailOverflows="true" uuid="9df29de1-0e84-4220-9267-75e045eefccd"/> 210 <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd"> 211 <datasetRun subDataset="Table Dataset 1" uuid="6e7a2173-23d9-469c-9f64-d7f6b45d893e"> 212 <datasetParameter name="ageList"> 213 <datasetParameterExpression><![CDATA[$P{ageList}]]></datasetParameterExpression> 214 </datasetParameter> 215 <datasetParameter name="nameList"> 216 <datasetParameterExpression><![CDATA[$P{nameList}]]></datasetParameterExpression> 217 </datasetParameter> 218 <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{nameList})]]></dataSourceExpression> 219 </datasetRun> 220 <jr:column width="90" uuid="2d1eff98-3a41-4c3e-8271-8fefc1c47fad"> 221 <jr:columnHeader style="table_CH" height="30" rowSpan="1"> 222 <staticText> 223 <reportElement x="0" y="0" width="90" height="30" uuid="3fa6bc74-6ac4-47c9-8cae-8c86fbc1d3be"/> 224 <text><![CDATA[No]]></text> 225 </staticText> 226 </jr:columnHeader> 227 <jr:detailCell style="table_TD" height="20" rowSpan="1"/> 228 </jr:column> 229 <jr:column width="90" uuid="bc412dc8-a633-4698-af07-3418de941110"> 230 <jr:columnHeader style="table_CH" height="30" rowSpan="1"> 231 <staticText> 232 <reportElement x="0" y="0" width="90" height="30" uuid="6965be42-36a4-49a6-8ec4-b48f6159c55f"/> 233 <text><![CDATA[age]]></text> 234 </staticText> 235 </jr:columnHeader> 236 <jr:detailCell style="table_TD" height="20" rowSpan="1"> 237 <textField> 238 <reportElement x="0" y="0" width="90" height="20" uuid="61d33b77-b578-45e5-886c-6b1bbea4eef0"/> 239 <textFieldExpression><![CDATA[""+$P{ageList}.get($V{index}-01)]]></textFieldExpression> 240 </textField> 241 </jr:detailCell> 242 </jr:column> 243 <jr:column width="90" uuid="06d5ec3f-f901-4651-b67d-82c9dc8ec575"> 244 <jr:columnHeader style="table_CH" height="30" rowSpan="1"> 245 <staticText> 246 <reportElement x="0" y="0" width="90" height="30" uuid="61e1d91e-55e7-4598-83e5-fdf8c46dc11c"/> 247 <text><![CDATA[name]]></text> 248 </staticText> 249 </jr:columnHeader> 250 <jr:detailCell style="table_TD" height="20" rowSpan="1"> 251 <textField> 252 <reportElement x="0" y="0" width="90" height="20" uuid="b06ae43f-cd55-4b09-917c-3aea38c9cabb"/> 253 <textFieldExpression><![CDATA[""+$P{nameList}.get($V{index}-01)]]></textFieldExpression> 254 </textField> 255 </jr:detailCell> 256 </jr:column> 257 </jr:table> 258 </componentElement> 259 </band> 260 </columnFooter> 261 </jasperReport>
json数据源文件tianjin.json如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 { "sales": { 2 "item": [ 3 { 4 5 "equipment": "equipment1", 6 "project": "project1", 7 "count": "1" 8 }, 9 { 10 11 "equipment": "equipment2", 12 "project": "project2", 13 "count": "1" 14 }, 15 { 16 17 "equipment": "equipment3", 18 "project": "project3", 19 "count": "1" 20 }, 21 { 22 23 "equipment": "equipment4", 24 "project": "project4", 25 "count": "1" 26 }, 27 { 28 29 "equipment": "equipment5", 30 "project": "project5", 31 "count": "1" 32 }, 33 { 34 35 "equipment": "equipment6", 36 "project": "project6", 37 "count": "1" 38 } 39 ] 40 }}
本文版权归__馋猫和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。