为什么我得到腐败的excel文件时,生成他们在我的java接缝应用程序与apache poi api



我试图在我的接缝java应用程序中创建一个excel电子表格。这是我的设置:

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(this.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>

这段代码生成一个excel文件,excel报错并指出该文件已损坏。如果我选择修复文件,我将得到具有正确数据的文件。

你能帮我解释一下我错在哪里吗

我想有时候工作时喝酒并不能帮助你更好地作为一个程序员=))

当我选择返回字节而不是文件时,我不知道我脑子里发生了什么。谢谢Gagravarr指出这一点。

正确代码:

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";
    }

相关内容

最新更新