我正在使用JasperReports API来制作报告。我制作了一个子报告,在其中放置了一个矩形并用黑色填充该矩形并设置为背景。当我通过pdf下载此报告时,一切正常,但是当我将此报告下载为doc矩形背景颜色和 textField 内容时未显示。
截图:
文件:
文档:
编辑:
这是我的模板:
<?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>
编辑:这是我用来生成文档格式报告的代码(在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;
}