我正在尝试在我的seam java应用程序中创建一个excel spreadheat。这是我的设置:
的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抱怨并声明该文件已损坏。如果我选择修复文件,我会得到包含正确数据的文件。
请你帮我解释一下我错在哪里。
答案 0 :(得分:3)
我想有时在工作中喝酒并不能帮助你做得更好,因为程序员=))
当我选择返回字节而不是文件时,我不知道脑子里发生了什么。 谢谢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";
}