📄 reportservlet.java
字号:
}
}
}
}
if (exportType == null || exportType.equals("")
|| exportType.equalsIgnoreCase("null")) {
if (hasError) {
this.outErrHtml(response, errMessage);
} else {
this.outHtmlStart(response);
this.outBodyStart(response, reportId);
outString(response, tableHtml.toString());
this.outBodyEnd(response);
this.outHtmlEnd(response);
}
} else {
if (replaceScript != null) {
rptMain.setRptScript(replaceScript);
}
rptMain.setRptTitle(title);
if (EXTORT_XLS.equalsIgnoreCase(exportType)) {
exportType = EXTORT_XLS;
} else {
exportType = EXTORT_PDF;
}
ServletContext context = this.getServletConfig()
.getServletContext();
response.setCharacterEncoding("GBK");
String userId = (String) request.getSession().getAttribute(
"enativeid");
if (userId == null) {
userId = "common";
}
String fileName = context.getRealPath("/reports/" + userId
+ "/");
File f = new File(fileName);
if (!f.exists()) {
f.mkdir();
}
fileName = fileName + "\\template.jasper";
rptMain.setRptExpfileType(exportType);
ReportGeneration reportGeneration = new ReportGeneration();
reportGeneration.setRptMain(rptMain);
JasperPrint jasperPrint = null;
try {
LOG.info("rptMain.getRptScript : " + rptMain.getRptScript());
jasperPrint = reportGeneration.createPrint(fileName);
} catch (RPTDesignException e1) {
e1.printStackTrace();
outErrHtml(response, e1.getMessage());
return;
} catch (JRException e1) {
e1.printStackTrace();
outErrHtml(response, e1.getMessage());
return;
}
ServletOutputStream ouputStream = null;
try {
ouputStream = response.getOutputStream();
response.setContentType("application/" + exportType);
if (exportType.equalsIgnoreCase("pdf")) {
response.setHeader("Content-disposition",
"attachment; filename="
+ new String("report"
.getBytes("GBK"),
"ISO-8859-1") + ".pdf");
JRPdfExporter exporter = new JRPdfExporter();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exporter.setParameter(
JRExporterParameter.JASPER_PRINT,
jasperPrint);
exporter.setParameter(
JRExporterParameter.OUTPUT_STREAM, baos);
exporter.exportReport();
byte[] bytes = baos.toByteArray();
response.setContentLength(bytes.length);
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
} else if (exportType.equalsIgnoreCase("xls")) {
response.setHeader("Content-disposition",
"attachment; filename="
+ new String("report"
.getBytes("GBK"),
"ISO-8859-1") + ".xls");
JRXlsExporter exporter = new JRXlsExporter();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exporter.setParameter(
JRExporterParameter.JASPER_PRINT,
jasperPrint);
exporter.setParameter(
JRExporterParameter.OUTPUT_STREAM, baos);
exporter
.setParameter(
JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
Boolean.TRUE); // 删除记录最下面的空行
exporter
.setParameter(
JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.FALSE);// 删除多余的ColumnHeader
exporter
.setParameter(
JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
Boolean.FALSE);// 显示边框
exporter
.setParameter(
JRXlsExporterParameter.IS_FONT_SIZE_FIX_ENABLED,
Boolean.TRUE);
exporter.exportReport();
byte[] bytes = baos.toByteArray();
response.setContentLength(bytes.length);
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
}
} catch (JRException e) {
outErrHtml(response, e.getMessage() + exportType);
} catch (IOException e) {
outErrHtml(response, e.getMessage() + exportType);
} finally {
if (ouputStream != null) {
try {
ouputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
} catch (SQLException e) {
outErrHtml(response, e.getMessage());
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) {
doGet(request, response);
}
private void outHtmlStart(HttpServletResponse response) {
PrintWriter out;
try {
out = response.getWriter();
out.println("<html>");
out.println("<head>");
out
.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\" />");
out.println("<title>报表参数</title>");
out
.println("<link rel=\"stylesheet\" href=\""
+ ApplicationConfig.getContextPath()
+ "/styles/default/ioffice.css\" type=\"text/css\"></link>");
out.println("</head>");
} catch (IOException e) {
e.printStackTrace();
}
}
private void outHtmlEnd(HttpServletResponse response) {
PrintWriter out;
try {
out = response.getWriter();
out.println("</html>");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void outBodyStart(HttpServletResponse response, String reportId) {
PrintWriter out;
try {
out = response.getWriter();
out.println("<script language=\"javascript\" src=\"");
out.println(jbc.config.ApplicationConfig.getContextPath());
out.println("/jsp/include/calendar.js\"></script>");
out.println("<body>");
out.println("<form method=\"post\">");
out
.println("<input type =\"hidden\" name=\"reportId\" id=\"reportId\" value=\""
+ reportId + "\" />");
out
.println("<table width=\"400\" cellspacing=\"1\" border=\"0\" cellpadding=\"1\" bgcolor=\"#D6E7FF\">");
out.print("<tr bgcolor=\"#FFFFFF\"><td>");
out.print("<h5 align=center style=\"font:'宋体'; \">报表参数</h5>");
out.println("</td></tr >");
out.print("<tr bgcolor=\"#FFFFFF\">");
out.println("<td width=\"100%\" height=\"100%\" >");
out.println("<table width=\"100%\" align=\"center\" >");
} catch (IOException e) {
e.printStackTrace();
}
}
private void outBodyEnd(HttpServletResponse response) {
PrintWriter out;
try {
out = response.getWriter();
out.println("<tr bgcolor=\"#FFFFFF\">");
out.println("<td>导出文件类型</td>");
out.print("<td>");
out
.print("<select name=\"exportType\" id=\"exportType\" size=\"1\"> <option value=\"pdf\">pdf</option><option value=\"xls\">excel</option> </select>");
out.println("</td>");
out.println("</tr>");
out.println("<tr bgcolor=\"#FFFFFF\">");
out.println("<td colspan=2 align=center >");
out
.println("<input name=createReprot id=createReprot type=\"submit\" value=\"确定\"/>");
out.println("</td>");
out.println("</tr>");
out.println("</table>");
out.println("</td>");
out.println("</tr>");
out.println("</table>");
out.println("</form>");
out.println("</body>");
} catch (IOException e) {
e.printStackTrace();
}
}
private void createInputHtml(StringBuffer buffer, String inputValue) {
Date toDay = new Date();
if (inputValue.indexOf("jizu") == 0) {
buffer.append("<select ").append("name=\"").append(inputValue)
.append("\" id=\"").append(inputValue).append("\" >");
buffer
.append("<option value=\"1\">1</option> <option value=\"2\">2</option> <option value=\"-\">-</option>");
buffer.append("</select> ");
} else {
buffer.append("<input type=\"text\" ");
buffer.append("name=\"").append(inputValue).append("\" id=\"")
.append(inputValue).append("\"");
if (inputValue.indexOf("_num") == 0) {
buffer.append("value=0");
} else if (inputValue.indexOf("_date") == 0) {
SimpleDateFormat dtFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dttFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm");
if (inputValue.contains("_date_start")) {
Calendar calendar0 = new GregorianCalendar();
calendar0.set(Calendar.DATE, 1);
buffer.append("value=\"").append(
dtFormat.format(calendar0.getTime())).append("\" ");
} else if (inputValue.contains("_date_end")) {
Calendar calendar1 = new GregorianCalendar();
buffer.append(" value=\"").append(
dtFormat.format(calendar1.getTime())).append("\" ");
} else if (inputValue.contains("_datetime_start")) {
Calendar calendar0 = new GregorianCalendar();
calendar0.set(Calendar.DATE, 1);
buffer.append("value=\"").append(
dttFormat.format(calendar0.getTime()))
.append("\" ");
} else if (inputValue.contains("_datetime_end")) {
Calendar calendar1 = new GregorianCalendar();
buffer.append(" value=\"").append(
dttFormat.format(calendar1.getTime()))
.append("\" ");
} else if (inputValue.contains("_date")) {
Calendar calendar1 = new GregorianCalendar();
buffer.append(" value=\"").append(
dtFormat.format(calendar1.getTime())).append("\" ");
}
buffer.append("onclick=setday(this); ");
} else {
if (inputValue.indexOf("yearMonth") > -1) {
DateFormat format = new SimpleDateFormat("yyyyMM");
buffer.append("value=\"" + format.format(toDay)
+ "\" size=\"7\" maxlength=\"7\"");
} else if (inputValue.indexOf("year") > -1) {
DateFormat format = new SimpleDateFormat("yyyy");
buffer.append("value=\"" + format.format(toDay)
+ "\"size=\"4\" maxlength=\"4\"");
}
if (inputValue.indexOf("month") > -1) {
DateFormat format = new SimpleDateFormat("MM");
buffer.append("value=\"" + format.format(toDay)
+ "\"size=\"2\" maxlength=\"2\"");
}
}
buffer.append(" />");
}
}
private void outString(HttpServletResponse response, String value) {
PrintWriter out;
try {
out = response.getWriter();
if (value == null || value.equals("")
|| value.equalsIgnoreCase("null")) {
out.print(" ");
return;
}
out.println(value);
} catch (IOException e) {
e.printStackTrace();
}
}
private void outErrHtml(HttpServletResponse response, String errMessage) {
PrintWriter out;
try {
out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>");
out.println(errMessage);
out.println("</title>");
out.println("</head>");
out.println("<body bgcolor=\"#D6EBFC\">");
out
.println("<table border=\"1\" style='background:#D6EBFC; font-size:12px; font:\"宋体\";'>");
out.println("<td> <b>");
out.println("输出报表不成功");
out.println("</b></td>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
} catch (IOException e) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -