2016-06-05 3 views
0

У меня есть некоторые отчеты, которые я их экспортирую, используя JasperReports библиотека. Все отлично, кроме файлов excel (xls). Например, когда я пытаюсь экспортировать точный отчет с 751 строками в качестве файла excel, проблем нет, и файл excel будет загружен правильно, но когда он будет превышать 800 строк, он покажет некоторые несвязанные символы. Размер строки, упомянутый выше, предназначен только для этого отчета, а для других отчетов предел находится на разных номерах. Эта проблема не существует, прежде чем я обновляю библиотеки с версии 2.3 до 6.1.0, и теперь я попробовал 6.2.2.jasperreports большой файл excel

Это, как я создаю мой первенствовать файл:

JasperPrint jasperPrint; 
    try { 
     jasperPrint = getReportPrint(request, baseForm, "excel"); 
    } catch (ReportNoDataException e) { 
     return handleNoData(mapping, request, e); 
    } 

    try { 
     JRXlsExporter jrXlsExporter = new JRXlsExporter(); 
     try { 
      jrXlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint)); 
      jrXlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream())); 

      SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration(); 
      configuration.setIgnoreCellBorder(true); 
      configuration.setWrapText(true); 
      configuration.setWhitePageBackground(false); 
      configuration.setRemoveEmptySpaceBetweenColumns(true); 
      configuration.setRemoveEmptySpaceBetweenRows(true); 
      configuration.setAutoFitPageHeight(true); 
      configuration.setDetectCellType(true); 
      configuration.setSheetDirection(RunDirectionEnum.RTL); 

      jrXlsExporter.setConfiguration(configuration); 
      jrXlsExporter.exportReport(); 
     } catch (IOException e) { 
      log.error("IOException occured" + e); 
      e.printStackTrace(); 
     } catch (JRException e) { 
      log.error("JRException occured while exporting for xls format", e); 
      e.printStackTrace(); 
     } 
    } catch (Exception e) { 
     throw e; 
    } 
    String filename="report"; 
    response.setHeader("Content-disposition","attachment; filename=\""+filename+".xls\""); 
    putSearchForm(request, baseForm); 
    return null; 

здесь код отчета:

<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="mtdTankhahEdaratMojoodiExcelReport" pageWidth="595" pageHeight="60" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" scriptletClass="report.webapp.tlmReportScriptlet" uuid="4354b1d9-63b2-4236-b190-ac5b2fb0f464"> 
<property name="com.jaspersoft.studio.unit." value="pixel"/> 
<queryString> 
    <![CDATA[]]> 
</queryString> 
<field name="no" class="java.lang.String"> 
    <fieldDescription><![CDATA[no]]></fieldDescription> 
</field> 
<field name="baseDate" class="java.lang.String"> 
    <fieldDescription><![CDATA[baseDate]]></fieldDescription> 
</field> 
<field name="pardakht" class="java.lang.Double"> 
    <fieldDescription><![CDATA[pardakht]]></fieldDescription> 
</field> 
<field name="daryaft" class="java.lang.Double"> 
    <fieldDescription><![CDATA[daryaft]]></fieldDescription> 
</field> 
<field name="zinaf" class="java.lang.String"> 
    <fieldDescription><![CDATA[zinaf]]></fieldDescription> 
</field> 
<field name="sanadType" class="java.lang.String"> 
    <fieldDescription><![CDATA[sanadType]]></fieldDescription> 
</field> 
<variable name="mande" class="java.lang.Double"> 
    <variableExpression><![CDATA[($F{pardakht}==null || $F{daryaft}==null) ? new Double(0) : 
(new Double(($F{daryaft}.doubleValue()-$F{pardakht}.doubleValue())+ 
$V{mande}.doubleValue()))]]></variableExpression> 
    <initialValueExpression><![CDATA[new Double("0")]]></initialValueExpression> 
</variable> 
<title> 
    <band height="30" splitType="Stretch"> 
     <staticText> 
      <reportElement key="staticText-4" x="0" y="0" width="30" height="30" uuid="36cd81f4-073b-4023-b5b8-716d43a4d377"> 
       <property name="com.jaspersoft.studio.unit.y" value="pixel"/> 
       <property name="com.jaspersoft.studio.unit.x" value="pixel"/> 
      </reportElement> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12" isBold="true"/> 
      </textElement> 
      <text><![CDATA[ردیف]]></text> 
     </staticText> 
     <staticText> 
      <reportElement key="staticText-8" x="275" y="0" width="80" height="30" uuid="0827df09-4353-424e-94fa-dd8cc7d5c6ac"> 
       <property name="com.jaspersoft.studio.unit.width" value="pixel"/> 
      </reportElement> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12" isBold="true"/> 
      </textElement> 
      <text><![CDATA[نوع سند]]></text> 
     </staticText> 
     <staticText> 
      <reportElement key="staticText-9" x="190" y="0" width="85" height="30" uuid="13490ca9-0ba0-4315-b5a0-745ce020f9e1"> 
       <property name="com.jaspersoft.studio.unit.width" value="pixel"/> 
      </reportElement> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12" isBold="true"/> 
      </textElement> 
      <text><![CDATA[تاریخ سند]]></text> 
     </staticText> 
     <staticText> 
      <reportElement key="staticText-12" x="110" y="0" width="80" height="30" uuid="544625a2-83d3-405d-a117-c0fa6820813a"> 
       <property name="com.jaspersoft.studio.unit.width" value="pixel"/> 
      </reportElement> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12" isBold="true"/> 
      </textElement> 
      <text><![CDATA[شماره سند]]></text> 
     </staticText> 
     <staticText> 
      <reportElement key="staticText-14" x="355" y="0" width="80" height="30" uuid="fc76869a-b6b1-4223-8ab3-26a870a23e08"> 
       <property name="com.jaspersoft.studio.unit.width" value="pixel"/> 
      </reportElement> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12" isBold="true"/> 
      </textElement> 
      <text><![CDATA[دریافت]]></text> 
     </staticText> 
     <staticText> 
      <reportElement key="staticText-14" x="515" y="0" width="80" height="30" uuid="f53ebc1b-eaaa-487b-bd23-ee62a1534caa"/> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12" isBold="true"/> 
      </textElement> 
      <text><![CDATA[مانده]]></text> 
     </staticText> 
     <staticText> 
      <reportElement key="staticText-14" x="435" y="0" width="80" height="30" uuid="aa3bda32-9b4c-4e53-927a-f6750256fb51"/> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12" isBold="true"/> 
      </textElement> 
      <text><![CDATA[پرداخت]]></text> 
     </staticText> 
     <staticText> 
      <reportElement key="staticText-12" x="30" y="0" width="80" height="30" uuid="b8c6b055-1b5e-4923-954b-a4eed5bab543"> 
       <property name="com.jaspersoft.studio.unit.width" value="pixel"/> 
      </reportElement> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12" isBold="true"/> 
      </textElement> 
      <text><![CDATA[ذینفع]]></text> 
     </staticText> 
    </band> 
</title> 
<detail> 
    <band height="30" splitType="Stretch"> 
     <property name="com.jaspersoft.studio.unit.height" value="pixel"/> 
     <textField isStretchWithOverflow="true" isBlankWhenNull="true"> 
      <reportElement key="textField-1" stretchType="RelativeToTallestObject" x="0" y="0" width="30" height="30" isPrintWhenDetailOverflows="true" uuid="263cfc8f-7812-4797-843c-42fe00045f98"> 
       <property name="com.jaspersoft.studio.unit.width" value="pixel"/> 
      </reportElement> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12"/> 
      </textElement> 
      <textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression> 
     </textField> 
     <textField isStretchWithOverflow="true" isBlankWhenNull="true"> 
      <reportElement key="textField-5" stretchType="RelativeToTallestObject" x="355" y="0" width="80" height="30" isPrintWhenDetailOverflows="true" uuid="baaa6584-8b9d-4523-a698-a68d065b8892"/> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12"/> 
      </textElement> 
      <textFieldExpression><![CDATA[($F{daryaft} == null)? new Double("0"): $F{daryaft}]]></textFieldExpression> 
     </textField> 
     <textField isStretchWithOverflow="true" isBlankWhenNull="true"> 
      <reportElement key="textField-6" stretchType="RelativeToTallestObject" x="190" y="0" width="85" height="30" isPrintWhenDetailOverflows="true" uuid="073a34ff-2e81-4265-a580-beb8fb293ae0"/> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12"/> 
      </textElement> 
      <textFieldExpression><![CDATA[($F{baseDate} == null)? "---" : $F{baseDate}]]></textFieldExpression> 
     </textField> 
     <textField isStretchWithOverflow="true" isBlankWhenNull="true"> 
      <reportElement key="textField-9" stretchType="RelativeToTallestObject" x="110" y="0" width="80" height="30" isPrintWhenDetailOverflows="true" uuid="e1c072ca-5d06-453c-a739-c797507a92b0"/> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12"/> 
      </textElement> 
      <textFieldExpression><![CDATA[($F{no} == null)? "---" : $F{no}]]></textFieldExpression> 
     </textField> 
     <textField isStretchWithOverflow="true" isBlankWhenNull="true"> 
      <reportElement key="textField-11" stretchType="RelativeToTallestObject" x="435" y="0" width="80" height="30" isPrintWhenDetailOverflows="true" uuid="a5778bf8-71da-4c84-82b8-219f27f04065"/> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12"/> 
      </textElement> 
      <textFieldExpression><![CDATA[($F{pardakht} == null)? new Double("0"): $F{pardakht}]]></textFieldExpression> 
     </textField> 
     <textField isStretchWithOverflow="true" isBlankWhenNull="true"> 
      <reportElement key="textField-11" stretchType="RelativeToTallestObject" x="515" y="0" width="80" height="30" isPrintWhenDetailOverflows="true" uuid="a762bdfa-4d3f-4975-b70d-d25875d15af8"/> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12"/> 
      </textElement> 
      <textFieldExpression><![CDATA[($V{mande} == null)? new Double("0"): $V{mande}]]></textFieldExpression> 
     </textField> 
     <textField isStretchWithOverflow="true" isBlankWhenNull="true"> 
      <reportElement key="textField-5" stretchType="RelativeToTallestObject" x="275" y="0" width="80" height="30" isPrintWhenDetailOverflows="true" uuid="d8c12462-e172-47eb-a301-905e356465b2"/> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12"/> 
      </textElement> 
      <textFieldExpression><![CDATA[($F{sanadType} == null)? "---": $F{sanadType}]]></textFieldExpression> 
     </textField> 
     <textField isStretchWithOverflow="true" isBlankWhenNull="true"> 
      <reportElement key="textField-9" stretchType="RelativeToTallestObject" x="30" y="0" width="80" height="30" isPrintWhenDetailOverflows="true" uuid="7eae6f3a-173e-479d-90d7-38b6c5a5b99f"/> 
      <box> 
       <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
       <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/> 
       <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/> 
      </box> 
      <textElement textAlignment="Center" verticalAlignment="Middle"> 
       <font fontName="bnazanin" size="12"/> 
      </textElement> 
      <textFieldExpression><![CDATA[($F{zinaf} == null)? "---" : $F{zinaf}]]></textFieldExpression> 
     </textField> 
    </band> 
</detail> 
</jasperReport> 

и вот скриншот из отчета: report result

Есть ли любая идея, как я могу исправить проблему?

ответ

3

Вывод, на который вы смотрите, - это вывод Excel. Это то, что файл Excel выглядит как если открыть его в текстовом редакторе (я взял случайный файл XLS и открыл его в Notepad ++):

enter image description here

Это означает, что ваш ответ не обеспечивает содержание как файл Excel, но как прямая загрузка, которая отображается непосредственно в браузере.

Попробуйте добавить дополнительные параметры вашего ответ (from here и настроено под Java), чтобы быть более явными:

response.setHeader("Content-Type: application/vnd.ms-excel; charset=utf-8"); 
response.setHeader("Expires: 0"); 
response.setHeader("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
response.setHeader("Cache-Control: private"); 

EDIT

Если это происходит с большими файлами, посмотри на this answer который описывает ту же проблему. Как правило, говорится, что вы должны отправить заголовки ответов как можно раньше, а затем создать файл отчета.

Короче говоря: сначала вы должны сначала написать все содержимое заголовка (я подозреваю, что у вас больше на response выше), а затем попытайтесь сгенерировать файл. Таким образом, браузер получает результат, видит, что приложен файл приложения и обеспечит загрузку. В противном случае, насколько я понимаю, заголовок response устанавливается после создания файла - это может быть слишком долго для браузера, ожидающего Content-Type.

+0

Я пробовал каждый contentType и заголовки, которые нашли в Интернете, и ничего не работает для меня. Код работает для небольших файлов excel и создает загружаемый файл, но для больших файлов он изменяется и показывает эту веб-страницу. Я думаю, что нет ограничений на размер файла, потому что я могу экспортировать большие PDF-файлы, и это происходит только для файлов excel. Есть ли другие настройки, которые я могу изменить? –

+0

Добавлен более конкретный ответ относительно вашего последующего вопроса. – tobi6

+0

вы герой, он работает сейчас :) –

Смежные вопросы