开发者

Why am I getting corrupt excel files when generating them in my java seam application with apache poi api

I'm trying to create an excel spreadsheat in my seam java application. This is my setup:

web.xml:

    <servlet>
        <servlet-name>Document Store Servlet</servlet-name>
        <servlet-class>org.jboss.seam.document.DocumentStoreServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Document Store Servlet</servlet-name>
        <url-pattern>/seam/docstore/*</url-pattern>
    </servlet-mapping>

export.xhtml:

<s:resource xmlns="http://www.w3.org/1999/xhtml"
            xmlns:s="http://jboss.com/products/seam/taglib"
            data="#{showSoldArticlesBean.excelData()}"
            contentType="application/vnd.ms-excel"
            fileName="#{showSoldArticlesBean.excelFileName()}"/>

someother.xhtml:

<s:download src="/export/export.xhtml">
                    <h:outputText value="Download excel"/>
                </s:download>

ShowSoldArticlesBean

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.*;

import java.io.*;
import java.util.List;       

@Name("showSoldArticlesBean")
@AutoCreate
@MeasureCalls
@Scope(ScopeType.CONVERSATION)
public class ShowSoldArticlesBean implements Serializable {

    @In
    private ArticleAdminBean articleAdminBean;

    @In(required = false)
    @Out(required = false)
    private String fromDate;

    @In(required = false)
    @Out(required = false)
    private String toDate;

    @Out(required = false)
    List<Article> currentArticleList;

    @Begin
    public void initiatePage() {
        if (StringUtils.isBlank(fromDate) && StringUtils.isBlank(toDate)) {
            fromDate = aWeekAgo();
            toDate = dateToString(now());
            this.showSoldArticles();
        }
    }

    @End
    public void showSoldArticles() {
        currentArticleList = articleAdminBean.getArticles(fromDate, toDate);
    }

    public byte[] excelData() {

        OutputStream fos = null;

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();

        HSSFCellStyle style = workbook.createCellStyle();
        style.setBorderTop((short) 6); // double lines border
        style.setBorderBottom((short) 1); // single line border
        style.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);

        HSSFFont font = workbook.createFont();
        font.setFontName(HSSFFont.FONT_ARIAL);
        font.setFontHeightInPoints((short) 20);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setColor(HSSFColor.BLUE.index);
        style.setFont(font);

        HSSFRow row = sheet.createRow(1);
        HSSFCell cell = row.createCell(1);
        cell.setCellValue(new HSSFRichTextString("Doing some excel crazy stuff!"));
        cell.setCellStyle(style);
        sheet.autoSizeColumn((short) 1);

        fos = null;
        try {
            fos = new FileOutputStream(new File(t开发者_如何学Gohis.excelFileName()));
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return workbook.getBytes();
    }

    public String excelFileName() {
        return "Somecrap.xls";
    }

}

pom.xml:

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8-beta4</version>
        </dependency>

This code generates an excel file that excel complains about and states that the file is corrupt. If I choose to repair the file I get the file with the correct data.

Could you please help me and explain where I'm wrong.


I guess that sometimes drinking on the job doesn't help you perform better as a programmer =))

I have no idea what was going on in my head when I chose to return the bytes instead of the file. Thank you Gagravarr for pointing that out.

Correct code:

public File excelData() {

        File excel = new File("Somecrap.xls");

        OutputStream fos = null;

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();

        HSSFCellStyle style = workbook.createCellStyle();
        style.setBorderTop((short) 6); // double lines border
        style.setBorderBottom((short) 1); // single line border
        style.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);

        HSSFFont font = workbook.createFont();
        font.setFontName(HSSFFont.FONT_ARIAL);
        font.setFontHeightInPoints((short) 20);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setColor(HSSFColor.BLUE.index);
        //style.setFont(font);

        HSSFRow row = sheet.createRow(1);
        HSSFCell cell = row.createCell(1);
        cell.setCellValue(new HSSFRichTextString("Doing some excel crazy stuff!"));
        //cell.setCellStyle(style);
        sheet.autoSizeColumn((short) 1);

        fos = null;
        try {
            fos = new FileOutputStream(excel);
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return test;
    }

    public String excelFileName() {
        return "Somecrap.xls";
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜