一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java 中jasperReport實現(xiàn)動態(tài)列打印的實現(xiàn)代碼

Java 中jasperReport實現(xiàn)動態(tài)列打印的實現(xiàn)代碼

2021-01-04 16:11zmx Java教程

這篇文章主要介紹了Java 中jasperReport實現(xiàn)動態(tài)列打印的實現(xiàn)代碼的相關資料,希望通過本文大家能掌握這部分內容,需要的朋友可以參考下

Java 中jasperReport實現(xiàn)動態(tài)列打印的實現(xiàn)代碼

        以下代碼中注釋說明很清楚,希望能幫助到大家,大家參考下。

示例代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
public ActionResult projectPrint() {
  String[] printValue = null;
  // 從頁面中獲得要查詢的字段
  String reqPrintValue = getRequest().getParameter("printValue");
  // 沒有選擇則默認全打印
  if (null == reqPrintValue || StringUtils.isEmpty(reqPrintValue)) {
   printValue = new String[] { "pnumber", "pname", "pdepart", "pdecision", "pthrow", "plastmonth", "pfund", "ploan" };
  } else {
   printValue = reqPrintValue.split(",");
  }
  // 查詢統(tǒng)計數(shù)據
  List<Object[]> projectList = getEntityManager().queryPrintProjectInfo(printValue);
 
  // 將數(shù)據轉換為Map對象,換化成Map對象
  List<Map> reportDataList = new ArrayList<Map>();
 
  for (int i = 0; i < projectList.size(); i++) {
   Object[] personStr = projectList.get(i);
   Map reportData = new HashMap();
   for (int j = 0; j < personStr.length; j++) {
    reportData.put("field_" + j, String.valueOf(personStr[j]));
   }
   reportDataList.add(reportData);
  }
 
  int columCount = 0;// 數(shù)據列
  int fieldCount = 0;// 字段列數(shù)(因為pname比較長所以想讓pname比其它的列長些,故設計這個變量)
  int pnameCount = -1;// 記錄下pname的序號
  for (int i = 0; i < printValue.length; i++) {
   // pthrow下面有兩列
   if ("pthrow".equals(printValue[i])) {
    columCount = columCount + 2;
    fieldCount = fieldCount + 2;
    // ploan下面也有兩列
   } else if ("ploan".equals(printValue[i])) {
    columCount = columCount + 2;
    fieldCount = fieldCount + 2;
    // 故意讓pname也占兩列
   } else if ("pname".equals(printValue[i])) {
    pnameCount = i;// 記錄下pname的序號
    columCount = columCount + 1;
    fieldCount = fieldCount + 2;
   } else {
    // 其它的列都占一個單位
    columCount = columCount + 1;
    fieldCount = fieldCount + 1;
   }
  }
 
  InputStream is = null;
  try {
   // 從資源文件中讀取報表
   is = this.getClass().getResourceAsStream("/reports/project.jrxml");
   JasperDesign jasperDesign = (JasperDesign) JRXmlLoader.load(is);
 
   Map styleMap = jasperDesign.getStylesMap();
   // column header 對應的樣式
   JRDesignStyle theaderStyle = (JRDesignStyle) styleMap.get("theader");
   // column detail 對應的樣式
   JRDesignStyle tbodyStyle = (JRDesignStyle) styleMap.get("tboby");
   // pagefoot 對應的樣式
   JRDesignStyle tfootStyle = (JRDesignStyle) styleMap.get("tfoot");
 
   int _START_X_ = 20;// x軸的起始位置
   int startX = _START_X_; // x軸的起始位置
   // 單列的寬度
   // 535是jasepreReport報表column最大的寬度
   int columnWidth = 535 / fieldCount;
   // 20,24,15是報表中已設置的,一定與之相同
   final int columnHeadBandHeight = 20;
   final int detailHeight = 24;
   final int pagefootHeight = 15;
 
   // 設置報表字段
   for (int idx = 0; idx < columCount; idx++) {
    JRDesignField field = new JRDesignField();
    field.setName("field_" + idx);
    field.setValueClass(java.lang.String.class);
    jasperDesign.addField(field);
   }
 
   JRDesignBand columnHeadBand = (JRDesignBand) jasperDesign.getColumnHeader();
   // 繪制表頭
   for (int idx = 0; idx < printValue.length; idx++) {
    if ("pnumber".equals(printValue[idx])) {
     JRDesignStaticText staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     staticText.setWidth(columnWidth);
     staticText.setY(0);
     staticText.setX(startX);
     staticText.setHeight(2 * columnHeadBandHeight);
     staticText.setText("序號");
     columnHeadBand.addElement(staticText);
     startX += columnWidth;
    } else if ("pname".equals(printValue[idx])) {
     JRDesignStaticText staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     // 項目名稱的寬度是其它的寬度的2倍
     staticText.setWidth(columnWidth * 2);
     staticText.setY(0);
     staticText.setX(startX);
     staticText.setHeight(2 * columnHeadBandHeight);
     staticText.setText("項目名稱");
     columnHeadBand.addElement(staticText);
     startX += columnWidth * 2;
    } else if ("pdepart".equals(printValue[idx])) {
     JRDesignStaticText staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     staticText.setWidth(columnWidth);
     staticText.setY(0);
     staticText.setX(startX);
     staticText.setHeight(2 * columnHeadBandHeight);
     staticText.setText("部門");
     columnHeadBand.addElement(staticText);
     startX += columnWidth;
    } else if ("pdecision".equals(printValue[idx])) {
     JRDesignStaticText staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     staticText.setWidth(columnWidth);
     staticText.setY(0);
     staticText.setX(startX);
     staticText.setHeight(2 * columnHeadBandHeight);
     staticText.setText("已決策");
     columnHeadBand.addElement(staticText);
     startX += columnWidth;
    } else if ("pthrow".equals(printValue[idx])) {
     // 投審會下面有兩列
     JRDesignStaticText staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     staticText.setWidth(columnWidth * 2);
     staticText.setY(0);
     staticText.setX(startX);
     staticText.setHeight(columnHeadBandHeight);
     staticText.setText("投審會");
     columnHeadBand.addElement(staticText);
 
     staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     columnHeadBand.addElement(staticText);
     staticText.setWidth(columnWidth);
     staticText.setY(columnHeadBandHeight);
     staticText.setX(startX);
     staticText.setHeight(columnHeadBandHeight);
     staticText.setText("12月初");
 
     staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     columnHeadBand.addElement(staticText);
     staticText.setWidth(columnWidth);
     staticText.setY(columnHeadBandHeight);
     staticText.setX(startX + columnWidth);
     staticText.setHeight(columnHeadBandHeight);
     staticText.setText("12月中");
     columnHeadBand.addElement(staticText);
     startX += 2 * columnWidth;
    } else if ("plastmonth".equals(printValue[idx])) {
     // 投決會下面有一列
     JRDesignStaticText staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     staticText.setWidth(columnWidth);
     staticText.setY(0);
     staticText.setX(startX);
     staticText.setHeight(columnHeadBandHeight);
     staticText.setText("投決會");
     columnHeadBand.addElement(staticText);
 
     staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     columnHeadBand.addElement(staticText);
     staticText.setWidth(columnWidth);
     staticText.setY(columnHeadBandHeight);
     staticText.setX(startX);
     staticText.setHeight(columnHeadBandHeight);
     staticText.setText("12月下");
     columnHeadBand.addElement(staticText);
     startX += columnWidth;
    } else if ("pfund".equals(printValue[idx])) {
     JRDesignStaticText staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     staticText.setWidth(columnWidth);
     staticText.setY(0);
     staticText.setX(startX);
     staticText.setHeight(2 * columnHeadBandHeight);
     staticText.setText("基金投資額");
     columnHeadBand.addElement(staticText);
     startX += columnWidth;
    } else if ("ploan".equals(printValue[idx])) {
     // 投貸協(xié)同額下面有兩列
     JRDesignStaticText staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     staticText.setWidth(columnWidth * 2);
     staticText.setY(0);
     staticText.setX(startX);
     staticText.setHeight(columnHeadBandHeight);
     staticText.setText("投貸協(xié)同額");
     columnHeadBand.addElement(staticText);
 
     staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     columnHeadBand.addElement(staticText);
     staticText.setWidth(columnWidth);
     staticText.setY(columnHeadBandHeight);
     staticText.setX(startX);
     staticText.setHeight(columnHeadBandHeight);
     staticText.setText("金額");
 
     staticText = new JRDesignStaticText();
     staticText.setStyle(theaderStyle);
     columnHeadBand.addElement(staticText);
     staticText.setWidth(columnWidth);
     staticText.setY(columnHeadBandHeight);
     staticText.setX(startX + columnWidth);
     staticText.setHeight(columnHeadBandHeight);
     staticText.setText("入庫情況");
     columnHeadBand.addElement(staticText);
     startX += 2 * columnWidth;
    }
   }
 
   // 繪制Detail部門
   startX = _START_X_;
   JRDesignBand columnDetailBand = (JRDesignBand) jasperDesign.getDetail();
   for (int idx = 0; idx < columCount; idx++) {
    JRDesignTextField textField = new JRDesignTextField();
    textField.setStretchWithOverflow(true);
    textField.setX(startX);
    textField.setY(0);
    if (pnameCount == idx) {
     textField.setWidth(2 * columnWidth);
     startX += 2 * columnWidth;
    } else {
     textField.setWidth(columnWidth);
     startX += columnWidth;
    }
    textField.setHeight(detailHeight);
    textField.setPositionType(JRElement.POSITION_TYPE_FLOAT);
    textField.setStyle(tbodyStyle);
    textField.setBlankWhenNull(true);
    JRDesignExpression expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("$F{field_" + idx + "}");
    textField.setExpression(expression);
    columnDetailBand.addElement(textField);
   }
 
   JRDesignBand pageFootBand = (JRDesignBand) jasperDesign.getPageFooter();
   // 合計數(shù)據,本應統(tǒng)計的
   List<Object[]> pageCountList = new ArrayList<Object[]>();
   Object[] obj = new String[] { "合計", "15299", "", "", "67121", "92420", "155877", };
   pageCountList.add(obj);
   obj = new String[] { "", "", "", "XXX小計", "", "24473", "16470", };
   pageCountList.add(obj);
   obj = new String[] { "", "", "", "WWW小計", "", "7289", "1674", };
   pageCountList.add(obj);
   obj = new String[] { "", "", "", "ZZZ小計", "", "32700", "13000", };
   pageCountList.add(obj);
   obj = new String[] { "", "", "", "YYY小計", "", "12733", "120733", };
   pageCountList.add(obj);
   obj = new String[] { "", "", "", "AAA小計", "", "2225", "120733", };
   pageCountList.add(obj);
   obj = new String[] { "", "", "", "BBB小計", "", "3000", "0", };
   pageCountList.add(obj);
   int footWidth = 535 / 7;
   for (int p = 0; p < pageCountList.size(); p++) {
    for (int k = 0; k < 7; k++) {
     Object[] ob = pageCountList.get(p);
     JRDesignStaticText staticText = new JRDesignStaticText();
     staticText.setStyle(tfootStyle);
     staticText.setWidth(footWidth);
     staticText.setY(pagefootHeight * p);
     staticText.setX(k * footWidth + _START_X_);
     staticText.setHeight(pagefootHeight);
     staticText.setText(String.valueOf(ob[k]));
     pageFootBand.addElement(staticText);
    }
   }
 
   // 編譯報表
   JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
   String type = this.getRequest().getParameter("type");//pdf格式
   JasperUtils.prepareReport(jasperReport, type);
   // 報表數(shù)據源
   JRDataSource dataSource = new JRBeanCollectionDataSource(reportDataList);
   JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);
   HttpServletResponse response = this.getResponse();
   JasperUtils.export(jasperPrint, response, getRequest(), type);
  } catch (Exception e) {
   e.printStackTrace();
  }
 
  return null;
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
public static void export(JasperPrint jasperPrint, HttpServletResponse response, HttpServletRequest request,
   String type) throws IOException {
  if (EXCEL_TYPE.equals(type)) {
   exportExcel(jasperPrint, response, request);
  } else if (PDF_TYPE.equals(type)) {
   exportPDF(jasperPrint, response, request);
  } else if (HTML_TYPE.equals(type)) {
   exportHTML(jasperPrint, response, request);
  } else {
   exportPrint(jasperPrint, response, request);
  }
 
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public static void exportExcel(JasperPrint jasperPrint, HttpServletResponse response, HttpServletRequest request)
   throws IOException {
  response.setContentType("application/vnd.ms-excel");
  String filename = DownloadHelper.encodeFilename("未命名.xls", request);
  response.setHeader("Content-disposition", "attachment;filename=" + filename);
  ServletOutputStream ouputStream = response.getOutputStream();
  JRXlsExporter exporter = new JRXlsExporter();
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
  exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
  exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
  exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
  try {
   exporter.exportReport();
  } catch (JRException e) {
   e.printStackTrace();
  }
  ouputStream.flush();
  ouputStream.close();
 }
 
 public static void exportPDF(JasperPrint jasperPrint, HttpServletResponse response, HttpServletRequest request)
   throws IOException {
  response.setContentType("application/pdf");
  String filename = DownloadHelper.encodeFilename("未命名.pdf", request);
  response.setHeader("Content-disposition", "attachment;filename=" + filename);
  ServletOutputStream ouputStream = response.getOutputStream();
  try {
   JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream);
  } catch (JRException e) {
   e.printStackTrace();
  }
  ouputStream.flush();
  ouputStream.close();
 }

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://zmx.iteye.com/blog/622546

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 香蕉91| 免费亚洲视频 | 天天色天天色天天色 | 免费一看一级欧美 | 欧美女孩13一14v | 99久久精品自在自看国产 | 国产欧美一区二区三区免费看 | 精品久久久久久久久免费影院 | 深夜日韩 | 日韩免费视频播播 | 884aa在线看片| 日韩久久影院 | 深夜啪啪网站 | 精品欧美小视频在线观看 | 亚州精品视频 | 日本免费一二区 | 日本制服丝袜 | 亚州在线视频 | 欧美国产高清 | 门卫老张和女警花小说 | 玩高中女同桌肉色短丝袜脚文 | 日韩人成 | 欧美性f | 久久视频在线视频观看精品15 | 国产91页 | 国产盗摄wc厕所撒尿视频 | 国产美女亚洲精品久久久综合 | 丰满艳妇亲伦视频 | 国产乱码免费卡1卡二卡3卡四 | 午夜AV国产欧美亚洲高清在线 | 日本老妇乱子伦中文视频 | 美女机机对机机的视频(免费) | 91资源站| 亚洲国产在线视频中文字 | 大杳蕉在线影院在线播放 | 操爽| 日本丰满www色 | 国产精品久久久久久久福利院 | 無码一区中文字幕少妇熟女H | 国产良心大作白丝精厕 | 奇米影视99 |