Jasperreports+jaspersoft studio 實現支援富文字格式的資料
阿新 • • 發佈:2019-01-01
昨天有位博友給我發郵件問我:Jasperreports+jaspersoft studio 如何實現支援富文字格式的資料!
其實,我也是第一次遇到這個問題;我就百度搜索,我記得之前:我解決過用百度編輯器,解析富文字格式的資料,是設定一個屬性;
廢話不多說了!看下圖:
這是普通的資料展示,沒問題的
修改資料表裡面的資料如下:
頁面顯示如下:
圖中的樣式:不是我想要的,我想讓<u></u> 變成下劃線
解決方法如下:
最後成功解決:
程式碼如下:
JasperServletDb.java
package com.accord.servlet; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRRuntimeException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperRunManager; public class JasperServletDb extends HttpServlet { private static final long serialVersionUID = 1L; public JasperServletDb() { super(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletConfig().getServletContext(); try { //JasperCompileManager.compileReportToFile("E:/report/DbReport.jrxml"); JasperCompileManager.compileReportToFile(context.getRealPath("/jasper/DemoReport03.jrxml")); } catch (JRException e1) { e1.printStackTrace(); }//編譯jrxml檔案,生成jasper檔案 File reportFile = new File(context.getRealPath("/jasper/DemoReport03.jasper")); if (!reportFile.exists()) throw new JRRuntimeException("FileWebappReport.jasper " + "not found. The report design must be compiledfirst."); String jasperPath = context.getRealPath("/jasper/DemoReport03.jasper"); PrintWriter out = null; try { String url = "jdbc:mysql://localhost:3306/db_bank"; Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, "root", "123456"); FileInputStream isRef = new FileInputStream(reportFile); ServletOutputStream sosRef = response.getOutputStream(); try { //JasperRunManager.runReportToPdfStream(isRef, sosRef, null,new JREmptyDataSource()); JasperRunManager.runReportToPdfStream(isRef, sosRef, null, conn); response.setContentType("application/pdf"); } catch (Exception e) { }finally{ sosRef.flush(); sosRef.close(); } /*//通過JasperFillManager工具進行填充報表,生成檔案 JasperPrint jasperPrint = JasperFillManager .fillReport(jasperPath, null,conn); //將html輸出到瀏覽器上 JRHtmlExporter exporter = new JRHtmlExporter(); response.setCharacterEncoding("UTF-8"); out = response.getWriter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out); exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE); exporter.exportReport();*/ } catch (Exception e) { e.printStackTrace(); }finally { //out.flush(); //out.close(); } } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>JasperReports</groupId> <artifactId>JasperReports02</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>JasperReports02 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>5.6.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> <!-- 解決中文亂碼 --> <dependency> <groupId>com.lowagie</groupId> <artifactId>itextasian</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies> <build> <finalName>JasperReports02</finalName> </build> </project>
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>JasperServlet</servlet-name> <display-name>JasperServlet</display-name> <description></description> <servlet-class>com.accord.servlet.JasperServlet</servlet-class> </servlet> <servlet> <servlet-name>JasperServletDb</servlet-name> <display-name>JasperServletDb</display-name> <description></description> <servlet-class>com.accord.servlet.JasperServletDb</servlet-class> </servlet> <servlet> <servlet-name>JasperServletJavaBean</servlet-name> <display-name>JasperServletJavaBean</display-name> <description></description> <servlet-class>com.accord.servlet.JasperServletJavaBean</servlet-class> </servlet> <servlet> <servlet-name>JasperServletJavaBeanGroup</servlet-name> <display-name>JasperServletJavaBeanGroup</display-name> <description></description> <servlet-class>com.accord.servlet.JasperServletJavaBeanGroup</servlet-class> </servlet> <servlet> <servlet-name>JasperServletTable</servlet-name> <display-name>JasperServletTable</display-name> <description></description> <servlet-class>com.accord.servlet.JasperServletTable</servlet-class> </servlet> <servlet> <servlet-name>JasperServletChart</servlet-name> <display-name>JasperServletChart</display-name> <description></description> <servlet-class>com.accord.servlet.JasperServletChart</servlet-class> </servlet> <servlet> <servlet-name>JasperServletChart02</servlet-name> <display-name>JasperServletChart02</display-name> <description></description> <servlet-class>com.accord.servlet.JasperServletChart02</servlet-class> </servlet> <servlet> <servlet-name>JasperServletCh</servlet-name> <display-name>JasperServletCh</display-name> <description></description> <servlet-class>com.accord.servlet.JasperServletCh</servlet-class> </servlet> <servlet-mapping> <servlet-name>JasperServlet</servlet-name> <url-pattern>/JasperServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>JasperServletDb</servlet-name> <url-pattern>/JasperServletDb</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>JasperServletJavaBean</servlet-name> <url-pattern>/JasperServletJavaBean</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>JasperServletJavaBeanGroup</servlet-name> <url-pattern>/JasperServletJavaBeanGroup</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>JasperServletTable</servlet-name> <url-pattern>/JasperServletTable</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>JasperServletChart</servlet-name> <url-pattern>/JasperServletChart</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>JasperServletChart02</servlet-name> <url-pattern>/JasperServletChart02</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>JasperServletCh</servlet-name> <url-pattern>/JasperServletCh</url-pattern> </servlet-mapping> </web-app>
DemoReport03.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="DemoReport03" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9070203f-ccf8-4d07-b92c-387acc1200a1">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="msql"/>
<queryString language="SQL">
<![CDATA[select * from company]]>
</queryString>
<field name="Id" class="java.lang.Integer">
<property name="com.jaspersoft.studio.field.label" value="Id"/>
<property name="com.jaspersoft.studio.field.tree.path" value="company"/>
</field>
<field name="name" class="java.lang.String">
<property name="com.jaspersoft.studio.field.label" value="name"/>
<property name="com.jaspersoft.studio.field.tree.path" value="company"/>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="230" y="24" width="100" height="30" uuid="33f7db34-0687-469c-b9fb-69023e73638d"/>
<textElement>
<font fontName="宋體" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[公司明細表]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="49" splitType="Stretch">
<staticText>
<reportElement x="208" y="10" width="139" height="30" uuid="746bdd39-9f31-4905-a700-91f57aea3cf7"/>
<textElement>
<font fontName="宋體" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[這是pdf的頁首]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="130" y="23" width="116" height="20" uuid="f0c3ef36-e052-476f-853c-e94249dac976"/>
<textElement>
<font fontName="宋體" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{Id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="247" y="23" width="233" height="20" uuid="b80cb7b0-ce36-4957-934b-fe2a3f8d792b"/>
<textElement markup="html">
<font fontName="宋體" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="54" splitType="Stretch">
<staticText>
<reportElement x="213" y="18" width="134" height="30" uuid="b824b8c5-c31d-4d3e-9b4b-c07a26b11061"/>
<textElement>
<font fontName="宋體" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[這是pdf的頁尾]]></text>
</staticText>
</band>
</pageFooter>
</jasperReport>
<textElement markup="html">
<font fontName="宋體" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
解決了支援富文字格式資料的問題!