我正在使用 JasperReports API 来制作报告。我制作了一个子报告,其中我放了一个矩形,用黑色填充那个矩形并设置为背景。当我通过 pdf 下载此报告时,一切正常,但当我以 doc 下载此报告时,矩形背景颜色和textField内容未显示。
截图:
全文:
DOC:
编辑:
这是我的模板:
<?xml version="1.0" encoding="UTF-8"?>
<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="header" language="groovy" pageWidth="515" pageHeight="842" columnWidth="515" leftMargin="0" rightMargin="0" topMargin="10" bottomMargin="0" uuid="57cd769e-896d-4b65-9ade-a81ddccb4b4d">
<parameter name="fullName" class="java.lang.String"/>
<parameter name="phone1" class="java.lang.String"/>
<parameter name="phone2" class="java.lang.String"/>
<parameter name="email" class="java.lang.String"/>
<parameter name="address" class="java.lang.String"/>
<parameter name="userImagePathInSubReport" class="java.lang.String"/>
<queryString>
<![CDATA[SELECT
address.`address1` AS address_address1,
address.`address2` AS address_address2,
address.`city` AS address_city,
address.`country_id` AS address_country_id,
address.`county` AS address_county,
address.`house_number` AS address_house_number,
address.`state` AS address_state,
address.`street` AS address_street,
address.`zip` AS address_zip,
personal_detail.`email` AS personal_detail_email,
personal_detail.`first_name` AS personal_detail_first_name,
personal_detail.`last_name` AS personal_detail_last_name,
personal_detail.`phone1_id` AS personal_detail_phone1_id,
personal_detail.`phone2_id` AS personal_detail_phone2_id,
personal_detail.`status` AS personal_detail_status,
personal_detail.`title` AS personal_detail_title,
resume.`website` AS resume_website,
resume.`career_profile` AS resume_career_profile
FROM
`address` address,
`personal_detail` personal_detail,
`resume` resume]]>
</queryString>
<field name="careerProfile" class="java.lang.String"/>
<field name="website" class="java.lang.String"/>
<title>
<band height="142" splitType="Stretch">
<rectangle>
<reportElement uuid="b844ceda-3ba1-40e0-9fe3-bc353c8f593a" x="0" y="0" width="503" height="137" backcolor="#000000"/>
</rectangle>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement uuid="672155ad-36d0-41a2-9495-dd362dc90382" positionType="Float" x="147" y="47" width="121" height="20" forecolor="#FFFFFF"/>
<textElement/>
<textFieldExpression><![CDATA[$P{fullName}.toUpperCase()]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement uuid="7b0d10ef-7253-48fb-aa31-507cf75a1145" positionType="Float" x="378" y="37" width="110" height="15" forecolor="#FFFFFF" backcolor="#000000"/>
<textElement>
<font fontName="SansSerif" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{phone1}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement uuid="0ab66ca4-9aa2-416c-b431-27591d5cba4f" positionType="Float" x="268" y="27" width="94" height="20" forecolor="#FFFFFF"/>
<textElement>
<font fontName="SansSerif" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{address}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement uuid="b1e9d52c-c272-40fd-bf55-42cb023255da" positionType="Float" mode="Transparent" x="378" y="52" width="110" height="15" forecolor="#FFFFFF" backcolor="#FFFFFF"/>
<textElement>
<font fontName="SansSerif" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{email}]]></textFieldExpression>
</textField>
<image vAlign="Middle" isUsingCache="true">
<reportElement uuid="1fedbec8-731c-456b-99ef-7477ca2cd933" x="23" y="12" width="99" height="113"/>
<imageExpression><![CDATA[$P{userImagePathInSubReport}]]></imageExpression>
</image>
<textField isBlankWhenNull="true">
<reportElement uuid="8a931b28-74c3-4693-ae2b-530d85469a2a" x="378" y="67" width="110" height="15" forecolor="#FFFFFF"/>
<textElement/>
<textFieldExpression><![CDATA[$F{website}]]></textFieldExpression>
</textField>
</band>
</title>
编辑:这是我用于以 doc 格式生成报告的代码(在 Grails 中)。我的应用程序使用 JasperReports插件1.6.1
def testSampleForDownloaddoc{
def result = [:]
result.data = []
result.data << [data1:"data1",data2:"data2"]
params._format = "DOCX"
params._file = "sample" //Set your file name this will call to sample.jrxml
JasperReportDef report = jasperService.buildReportDefinition(params, request.getLocale(), result)
generateReport(report)
}
def generateReport = { reportDef ->
if (!reportDef.fileFormat.inline && !reportDef.parameters._inline) {
response.setHeader("Content-Disposition", "inline; filename=\"${reportDef.parameters._name ?: reportDef.name}.${reportDef.fileFormat.extension}\"");
response.setHeader("Content-Type", "${reportDef.fileFormat.mimeTyp}");
response.contentType = reportDef.fileFormat.mimeTyp
response.characterEncoding = "UTF-8"
response.outputStream << reportDef.contentStream.toByteArray()
} else {
render(text: reportDef.contentStream, contentType: reportDef.fileFormat.mimeTyp, encoding: reportDef.parameters.encoding ? reportDef.parameters.encoding : 'UTF-8');
}
}
编辑:这是我正在使用的 Java 代码:
public JasperDesign template(List<SectionOrder> SectionOrderList, List<ExtraSection> extraSectionList, Integer selectedTemplate, String applicationPath, Boolean isUserImageAvailable) throws Exception {
//Parameters
JRDesignParameter SUBREPORT_DIR = new JRDesignParameter();
SUBREPORT_DIR.setName("imagePathInTemplate5");
SUBREPORT_DIR.setValueClass(java.lang.String.class);
expression = new JRDesignExpression();
expression.setValueClass(java.lang.String.class);
expression.setText("\"" + applicationPath + "/images/templateImages/template5\"");
SUBREPORT_DIR.setDefaultValueExpression(expression);
jasperDesign.addParameter(SUBREPORT_DIR);
JRDesignField field;
field = createField("personalDetail.fullName", java.lang.String.class);
jasperDesign.addField(field);
field = createField("personalDetail.phone1.number", java.lang.String.class);
jasperDesign.addField(field);
field = createField("personalDetail.phone2.number", java.lang.String.class);
jasperDesign.addField(field);
field = createField("personalDetail.email", java.lang.String.class);
jasperDesign.addField(field);
field = createField("userImagePath", String.class);
jasperDesign.addField(field);
field = createField("headerImage", String.class);
jasperDesign.addField(field);
field = createField("phoneImage", String.class);
jasperDesign.addField(field);
field = createField("messageImage", String.class);
jasperDesign.addField(field);
for (ExtraSection extraSection : extraSectionList) {
String name = replaceDollarSign(extraSection.getTitle()).replace(" ", "_");
System.out.println("name-------------" + name);
field = createField(name, List.class);
jasperDesign.addField(field);
}
JRDesignBand band = new JRDesignBand();
band.setHeight(20);
jasperDesign.setTitle(band);
//Detail
band = new JRDesignBand();
int totalNumberOfSubReport = resumeSectionOrderList.size();
int heightOfOneSubReport = 200 / totalNumberOfSubReport;
band.setHeight(300);
band.setSplitType(SplitTypeEnum.IMMEDIATE);
//Header Data
JRDesignSubreport headerInfoSubReport;
if (isUserImageAvailable) {
headerInfoSubReport = createSubReport(jasperDesign, 0, 0, heightOfOneSubReport, jasperDesign.getColumnWidth(), "resumeList", net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.class, applicationPath + "/report/template5/headerWithUserImage.jasper");
}
//User image
JRDesignSubreportParameter userImagePathParameter = new JRDesignSubreportParameter();
userImagePathParameter.setName("userImagePathInSubReport");
expression = new JRDesignExpression();
expression.setText("$F{userImagePath}");
userImagePathParameter.setExpression(expression);
headerInfoSubReport.addParameter(userImagePathParameter);
//End user image
//Full name
JRDesignSubreportParameter fullName = new JRDesignSubreportParameter();
fullName.setName("fullName");
expression = new JRDesignExpression();
expression.setValueClass(String.class);
expression.setText("$F{personalDetail.fullName}");
fullName.setExpression(expression);
headerInfoSubReport.addParameter(fullName);
band.addElement(headerInfoSubReport);
//End of full name
//Phone1
JRDesignSubreportParameter phone1 = new JRDesignSubreportParameter();
phone1.setName("phone1");
expression = new JRDesignExpression();
expression.setValueClass(String.class);
expression.setText("$F{personalDetail.phone1.number}");
phone1.setExpression(expression);
headerInfoSubReport.addParameter(phone1);
band.addElement(headerInfoSubReport);
//End of phone1
//Phone2
JRDesignSubreportParameter phone2 = new JRDesignSubreportParameter();
phone2.setName("phone2");
expression = new JRDesignExpression();
expression.setValueClass(String.class);
expression.setText("$F{personalDetail.phone2.number}");
phone2.setExpression(expression);
headerInfoSubReport.addParameter(phone2);
band.addElement(headerInfoSubReport);
//End of phone2
//Email
JRDesignSubreportParameter email = new JRDesignSubreportParameter();
email.setName("email");
expression = new JRDesignExpression();
expression.setValueClass(String.class);
expression.setText("$F{personalDetail.email}");
email.setExpression(expression);
headerInfoSubReport.addParameter(email);
band.addElement(headerInfoSubReport);
//End of email
//Address
JRDesignSubreportParameter address = new JRDesignSubreportParameter();
address.setName("address");
expression = new JRDesignExpression();
expression.setValueClass(String.class);
expression.setText("$F{newLineAddress}");
address.setExpression(expression);
headerInfoSubReport.addParameter(address);
band.addElement(headerInfoSubReport);
//End of address
Map<String, String> pathMap = new HashMap<String, String>();
Map<String, String> valueMap = new HashMap<String, String>();
for (ResumeSectionOrder resumeSectionOrder : resumeSectionOrderList) {
if (resumeSectionOrder.getSectionName().equals("Career Objective")) {
pathMap.put("Career Objective", applicationPath + "/report/template5 /careerObjective.jasper");
valueMap.put("Career Objective", "resumeList");
}
if (resumeSectionOrder.getSectionName().equals("Personal Profile")) {
pathMap.put("Personal Profile", applicationPath + "/report/template5 /personalStatement.jasper");
valueMap.put("Personal Profile", "someList");
}
}
((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
//Column footer
band = new JRDesignBand();
jasperDesign.setColumnFooter(band);
//Page footer
band = new JRDesignBand();
jasperDesign.setPageFooter(band);
//Summary
return jasperDesign;
}
答案 0 :(得分:1)
结果是:没有有效。容器的背景颜色缺失。
我得到 Word 文档中表的结果,没有背景包含样本中的文本和图像。
我认为它可能是一个实现错误,或者它可能是基于 Apache POI 库的实现的“功能”。
我发现了“丑陋”的解决方案,但它确实有效。
您可以构建 html 页面(您想要的任何设计)并使用 Html 元素来显示此页面。
jrxml 的样本:
<?xml version="1.0" encoding="UTF-8"?>
<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="background_word" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5c50ffd4-6429-4c13-83d1-86221e9aa4bc">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="fullName" class="java.lang.String">
<defaultValueExpression><![CDATA["Bill"]]></defaultValueExpression>
</parameter>
<parameter name="phone1" class="java.lang.String">
<defaultValueExpression><![CDATA["234-56-78"]]></defaultValueExpression>
</parameter>
<parameter name="phone2" class="java.lang.String">
<defaultValueExpression><![CDATA["234-56-77"]]></defaultValueExpression>
</parameter>
<parameter name="email" class="java.lang.String">
<defaultValueExpression><![CDATA["vk@google.com"]]></defaultValueExpression>
</parameter>
<parameter name="address" class="java.lang.String">
<defaultValueExpression><![CDATA["City"]]></defaultValueExpression>
</parameter>
<parameter name="userImagePathInSubReport" class="java.lang.String">
<defaultValueExpression><![CDATA["image.jpg"]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="careerProfile" class="java.lang.String"/>
<field name="website" class="java.lang.String"/>
<title>
<band height="241" splitType="Stretch">
<componentElement>
<reportElement uuid="cbd45ce6-f8bf-411c-a453-24a12abe76d8" x="48" y="21" width="327" height="200"/>
<hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="FillFrame" horizontalAlign="Left" verticalAlign="Middle">
<hc:htmlContentExpression><![CDATA["<table\n" +
"style=\"text-align: left; height: 94px; background-color: black; width: 99%;\"\n" +
"border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n" +
"<tbody>\n" +
"<tr>\n" +
"<td colspan=\"1\" rowspan=\"3\"\n" +
"style=\"vertical-align: top; background-color: black; width: 25%;\"><img\n" +
"style=\"width: 25px; height: 31px;\" alt=\"\" src=\"" + $P{userImagePathInSubReport} + "><br>\n" +
"</td>\n" +
"<td style=\"vertical-align: top; background-color: black;\"><span\n" +
"style=\"background-color: white;\">" + $P{fullName} + "</span><br>\n" +
"</td>\n" +
"<td style=\"vertical-align: top;\"><br>\n" +
"</td>\n" +
"<td style=\"vertical-align: top;\"><span\n" +
"style=\"background-color: white;\">" + $P{phone1} + "</span></td>\n" +
"</tr>\n" +
"<tr>\n" +
"<td style=\"vertical-align: top;\"><br>\n" +
"</td>\n" +
"<td style=\"vertical-align: top;\"><span\n" +
"style=\"background-color: white;\">" + $P{address} + "</span></td>\n" +
"<td style=\"vertical-align: top;\"><span\n" +
"style=\"background-color: white;\">" + $P{email} + "</span></td>\n" +
"</tr>\n" +
"<tr>\n" +
"<td style=\"vertical-align: top;\"><br>\n" +
"</td>\n" +
"<td style=\"vertical-align: top;\"><br>\n" +
"</td>\n" +
"<td style=\"vertical-align: top;\"><span\n" +
"style=\"background-color: white;\">" + $F{website} + "</span></td>\n" +
"</tr>\n" +
"</tbody>\n" +
"</table>"]]></hc:htmlContentExpression>
</hc:html>
</componentElement>
</band>
</title>
</jasperReport>
结果将是(在 MS Word 中):
注意:
我用过 iReport 5.2.0
您可以在 JasperReports库分发包的demo\samples\htmlcomponent\
文件夹中找到使用 Html 组件的示例。
我看到您已在http://community.jaspersoft.com上发布此问题。如果你得到帮助,请告诉我们。