在 JasperReports 中添加表格边框可以通过设置表格单元格(JRDesignCell
)和表格元素(JRDesignElement
)的边框属性来实现。以下是一个详细的步骤指南,帮助你在 JasperReports 中为表格添加边框。
如果你使用的是 iReport 或 Jaspersoft Studio 设计报表,可以通过图形界面轻松设置表格边框。
.jrxml
)。如果你更喜欢手动编辑 JRXML 文件,可以直接在 XML 中设置边框属性。
以下是一个示例 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="SampleReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="12345678-1234-1234-1234-123456789012">
<detail>
<band height="50">
<componentElement>
<reportElement x="0" y="0" width="555" height="50"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<jr:column>
<jr:columnHeader>
<jr:box>
<jr:pen lineWidth="1.0" lineColor="#000000"/>
</jr:box>
<jr:textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement>
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Header 1"]]></textFieldExpression>
</jr:textField>
</jr:columnHeader>
<jr:detailCell>
<jr:box>
<jr:pen lineWidth="1.0" lineColor="#000000"/>
</jr:box>
<jr:textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textFieldExpression><![CDATA[$F{field1}]]></textFieldExpression>
</jr:textField>
</jr:detailCell>
</jr:column>
<!-- 其他列定义 -->
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
<jr:box>
元素:用于定义单元格的边框。<jr:pen>
元素:用于设置边框的宽度和颜色。lineWidth
属性:设置边框的宽度。lineColor
属性:设置边框的颜色。<jr:columnHeader>
和 <jr:detailCell>
元素:分别定义表头单元格和数据单元格的边框。如果你是通过 Java 代码动态生成报表,可以使用 JasperReports API 设置单元格的边框。
以下是一个示例 Java 代码,展示了如何动态生成带有边框的表格。
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.*;
import net.sf.jasperreports.engine.type.*;
public class JasperReportExample {
public static void main(String[] args) throws JRException {
// 创建 JasperDesign 对象
JasperDesign jasperDesign = new JasperDesign();
jasperDesign.setName("SampleReport");
jasperDesign.setPageWidth(595);
jasperDesign.setPageHeight(842);
jasperDesign.setColumnWidth(555);
jasperDesign.setLeftMargin(20);
jasperDesign.setRightMargin(20);
jasperDesign.setTopMargin(20);
jasperDesign.setBottomMargin(20);
// 创建 Detail Band
JRDesignBand detailBand = new JRDesignBand();
detailBand.setHeight(50);
jasperDesign.setDetail(detailBand);
// 创建 Table
JRDesignComponentElement tableElement = new JRDesignComponentElement();
tableElement.setX(0);
tableElement.setY(0);
tableElement.setWidth(555);
tableElement.setHeight(50);
JRDesignTable table = new JRDesignTable();
tableElement.setComponent(table);
// 创建列
JRDesignColumn column = new JRDesignColumn();
column.setWidth(100);
// 创建列头单元格
JRDesignCell headerCell = new JRDesignCell();
headerCell.setWidth(100);
headerCell.setHeight(20);
JRDesignBox headerBox = new JRDesignBox();
JRDesignPen headerPen = new JRDesignPen();
headerPen.setLineWidth(1f);
headerPen.setLineColor(Color.BLACK);
headerBox.setPen(headerPen);
headerCell.setBox(headerBox);
JRDesignTextField headerTextField = new JRDesignTextField();
headerTextField.setX(0);
headerTextField.setY(0);
headerTextField.setWidth(100);
headerTextField.setHeight(20);
headerTextField.setExpression(new JRDesignExpression("\"Header 1\""));
headerCell.addElement(headerTextField);
column.setColumnHeader(headerCell);
// 创建数据单元格
JRDesignCell detailCell = new JRDesignCell();
detailCell.setWidth(100);
detailCell.setHeight(20);
JRDesignBox detailBox = new JRDesignBox();
JRDesignPen detailPen = new JRDesignPen();
detailPen.setLineWidth(1f);
detailPen.setLineColor(Color.BLACK);
detailBox.setPen(detailPen);
detailCell.setBox(detailBox);
JRDesignTextField detailTextField = new JRDesignTextField();
detailTextField.setX(0);
detailTextField.setY(0);
detailTextField.setWidth(100);
detailTextField.setHeight(20);
detailTextField.setExpression(new JRDesignExpression("$F{field1}"));
detailCell.addElement(detailTextField);
column.setDetailCell(detailCell);
table.addColumn(column);
detailBand.addElement(tableElement);
// 编译报表
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
// 填充报表
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<>(), new JREmptyDataSource());
// 导出报表
JasperExportManager.exportReportToPdfFile(jasperPrint, "SampleReport.pdf");
}
}
JRDesignBox
和 JRDesignPen
设置单元格的边框属性。领取专属 10元无门槛券
手把手带您无忧上云