Java 将报告生成为 txt 文件 - 如何设置列标题和详细信息带中文本的确切位置和大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19602804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating report as txt file - How to set the exact positions and sizes for texts in Column Header and Detail bands
提问by Suraj M
We have a requirement where text reports are to be generated using JasperReports.
The precision has to be accurate, sample format is as follows
我们需要使用JasperReports生成文本报告。
精度要准确,样本格式如下
- Header user name has to start at column 0.
- Header last accessed must start at column 23.
- Header is active must start at column 34.
- Line separator must have 8 characters (---) and start at column 22
- Name filed must be left aligned and start at column 1
- Points field must be right aligned and end in column 45, with precision to 1 place.
- 标题用户名必须从第 0 列开始。
- 上次访问的标题必须从第 23 列开始。
- 标题处于活动状态必须从第 34 列开始。
- 行分隔符必须有 8 个字符 (---) 并从第 22 列开始
- 名称字段必须左对齐并从第 1 列开始
- 点字段必须右对齐并在第 45 列结束,精度为 1 位。
The JasperReports, exportToTextmethod does not satisfy the above criteria and repeated adjustment of the .jrxmlis not serving the purpose.
We adjusted the character width and character height as well, but still there is incorrect spacing between rows and columns.
所述的JasperReports,exportToText方法不满足上述标准和反复调整的.jrxml未投放的目的。
我们也调整了字符宽度和字符高度,但行和列之间的间距仍然不正确。
Does JasperReports APIsupport such precision in text formats?
Is there any other API which can help us in achieving this format?
JasperReports API是否支持文本格式的这种精度?
有没有其他 API 可以帮助我们实现这种格式?
采纳答案by Alex K
Yes, you can achieve your design with JasperReports.
是的,您可以使用JasperReports实现您的设计。
Steps in solving task.
解决任务的步骤。
The first step. We should the numbers of characters in row and the number of rows on page.
We can use pair of net.sf.jasperreports.export.text.page.widthand net.sf.jasperreports.export.text.page.heightreport's properties for this purpose. You can also use net.sf.jasperreports.export.text.character.widthand net.sf.jasperreports.export.text.character.heightpair of report's properties instead of the first one.The second step. We known the page format size (for example in pixels. For A4 without margins it is: 555x802) and we set the number of characters in row and rows at page. We can calculate and set the exact values of staticTextand textFieldelements positions (x, ycoordinates and the widthand heightproperties of element),
第一步。我们应该知道行中的字符数和页面上的行数。为此,
我们可以使用成对的net.sf.jasperreports.export.text.page.width和net.sf.jasperreports.export.text.page.height报告的属性。您还可以使用net.sf.jasperreports.export.text.character.width和net.sf.jasperreports.export.text.character.height对报告的属性而不是第一个。第二步。我们知道页面格式大小(例如以像素为单位。对于没有边距的 A4,它是:555x802)并且我们设置了行中的字符数和页面中的行数。我们可以计算和设置staticText和textField元素位置的确切值(x,y坐标以及元素的宽度和高度属性),
The sample
样品
Sample datasouce
示例数据源
I've used the csvdatasource (points.csvfile):
我使用了csv数据源(points.csv文件):
USER_NAME,LAST_ACCESSED,IS_ACTIVE,POINTS
John Doe,10/26/2013,Y,87.9
Sarah Connor,10/23/2013,Y,80.5
Viktor Navorski,10/14/2013,Y,95.5
Forrest Gump,10/25/2013,Y,97.0
Raymond Babbitt,10/24/2013,Y,88.5
Thomas Crown,10/24/2013,Y,88.0
Danny Ocean,09/30/2013,Y,90.5
The report design
报告设计
I've remove the margin and set the A4report's format (555x802 pixels).
我已经删除了边距并设置了A4报告的格式(555x802 像素)。
The jrxml file
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="text_output" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="9a0b1db2-1ed0-44e1-960b-4626cea0097d">
<property name="net.sf.jasperreports.export.text.page.width" value="80"/>
<property name="net.sf.jasperreports.export.text.page.height" value="40"/>
<field name="USER_NAME" class="java.lang.String"/>
<field name="LAST_ACCESSED" class="java.lang.String"/>
<field name="IS_ACTIVE" class="java.lang.String"/>
<field name="POINTS" class="java.lang.String"/>
<columnHeader>
<band height="43" splitType="Stretch">
<staticText>
<reportElement uuid="d065f8ed-0c05-47d0-a679-a392c9cfabe5" x="0" y="0" width="152" height="20"/>
<textElement/>
<text><![CDATA[USER_NAME]]></text>
</staticText>
<staticText>
<reportElement uuid="573550a9-5fc9-41ee-ae4f-7b550868c75d" x="152" y="0" width="143" height="20"/>
<textElement/>
<text><![CDATA[LAST_ACCESSED]]></text>
</staticText>
<staticText>
<reportElement uuid="4b1f5b5c-4c61-4159-9357-041fd8e3e775" x="295" y="0" width="138" height="20"/>
<textElement/>
<text><![CDATA[IS_ACTIVE]]></text>
</staticText>
<staticText>
<reportElement uuid="1c851f50-9ace-4e30-be1e-7d5454127db8" x="433" y="0" width="103" height="20"/>
<textElement/>
<text><![CDATA[POINTS]]></text>
</staticText>
<staticText>
<reportElement uuid="7e358a83-b348-40ca-b6f4-009d6bed4b24" x="0" y="20" width="100" height="23"/>
<textElement/>
<text><![CDATA[----------------------]]></text>
</staticText>
<staticText>
<reportElement uuid="523a7abd-0fbd-4b63-b8d5-2ed78ee795cc" x="295" y="20" width="100" height="23"/>
<textElement/>
<text><![CDATA[---------]]></text>
</staticText>
<staticText>
<reportElement uuid="7ebe2ec7-9450-4fdf-b99a-48af5fe3ce49" x="152" y="20" width="100" height="23"/>
<textElement/>
<text><![CDATA[-------------]]></text>
</staticText>
<staticText>
<reportElement uuid="155d3972-9d99-4d3b-bb41-05f08dcdd5fa" x="433" y="20" width="100" height="23"/>
<textElement/>
<text><![CDATA[--------]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement uuid="652256e2-0f1e-4848-b1d0-d064caaa5020" x="0" y="0" width="152" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{USER_NAME}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="380af714-4dbd-4e0f-ae23-bfada0449a2f" x="152" y="0" width="143" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{LAST_ACCESSED}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="752c0f18-2dea-4209-bd0d-d787d8c462a8" x="295" y="0" width="63" height="20"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{IS_ACTIVE}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="40807ad0-58a4-42b7-9636-04f01c0c3bf5" x="433" y="0" width="54" height="20"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{POINTS}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
The result - generated txt file via JRTextExporter
结果 - 通过 JRTextExporter 生成的 txt 文件
I've used iReportfor generating output file. The contents of this txtfile is:
我使用iReport生成输出文件。这个txt文件的内容是:
Using Java code
使用 Java 代码
In case using Javacode we should set for JRTextExporterthis parameters: JRTextExporterParameter.PAGE_WIDTHand JRTextExporterParameter.PAGE_HEIGHT
如果使用Java代码,我们应该为JRTextExporter设置以下参数:JRTextExporterParameter.PAGE_WIDTH和JRTextExporterParameter.PAGE_HEIGHT
The sample code:
示例代码:
public static void generateReport() throws JRException {
JRCsvDataSource dataSource = new JRCsvDataSource(JRLoader.getLocationInputStream(csvFileName));
dataSource.setRecordDelimiter("\r\n");
dataSource.setUseFirstRowAsHeader(true);
dataSource.setColumnNames(new String[]{ "USER_NAME", "LAST_ACCESSED", "IS_ACTIVE", "POINTS"});
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);
JRTextExporter exporter = new JRTextExporter();
exporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, 80);
exporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, 40);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputFileName);
exporter.exportReport();
}
In this sample I've used the same csvfile as datasource.
在此示例中,我使用了与数据源相同的csv文件。
How we can calculate the x coordinate
我们如何计算 x 坐标
In your sample you want to start the second column with 23 symbols. Ok, let see my sample. What we have:
在您的示例中,您希望以 23 个符号开始第二列。好的,让我们看看我的样本。我们有什么:
- The page width is 555pixels
- The number of characters in row is 80
- 页面宽度为555像素
- 行中的字符数为80
The width of one character in this case is 555/80=6,9375.
We have 22 symbols before the first symbol in the 2nd column. It is 22*6,9375=152,625pixels. In my sample I've set the 152for the xproperty of 2nd column header. In my sample the first symbol in the header of the 2nd column has position 23.
在这种情况下,一个字符的宽度是555/80=6,9375。
我们在第 2 列中的第一个符号之前有 22 个符号。它是22*6,9375=152,625像素。在我的示例中,我为第二列标题的x属性设置了152。在我的示例中,第 2 列标题中的第一个符号的位置为23。
For the third column the calculated value is 43*6,9375=298,3125and the real xin my sample is 295(the 3rd column stars with 44symbol).
对于第三列,计算值是43*6,9375=298,3125,我的样本中的实际x是295(第三列带有44 个符号的星号)。
As you can see the calculation is too accurate.
如您所见,计算过于准确。
Notes:
笔记:
You can find information about exporting to the text file here: Text Export Sample
您可以在此处找到有关导出到文本文件的信息:文本导出示例