首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

excel解决办法

2013-01-21 
excelpublic void downloadExcel(ListRepairCatalogSearchResultsVO rowDataList, String fileName) thr

excel

public void downloadExcel(List<RepairCatalogSearchResultsVO> rowDataList, String fileName) throws CROSException{
ServletOutputStream servletOutputStream = null;
FileOutputStream output = null;
FileInputStream input = null;
File exportFile = null;

try {
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet("results");
sheet.setDefaultColumnWidth((short)20);
HSSFFont font1 = workBook.createFont();
font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

/*HSSFCellStyle titleBold = workBook.createCellStyle();
titleBold.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleBold.setWrapText(true);
titleBold.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
titleBold.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = workBook.createFont();
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
titleBold.setFont(font);*/


HSSFRow row = sheet.createRow((short) 0);
row.createCell((short)0).setCellValue("ATA");
//row.getCell((short)0).setCellStyle(titleBold);

row.createCell((short)1).setCellValue("componentDesc");
//row.getCell((short)1).setCellStyle(titleBold);


Iterator<RepairCatalogSearchResultsVO> ite = rowDataList.iterator();
int index = 1;
while (ite.hasNext()) {
RepairCatalogSearchResultsVO vo = ite.next();
row = sheet.createRow(index);
row.createCell((short)0);
row.getCell((short)0).setCellValue(vo.getAtaNum());

row.createCell((short)1);
row.getCell((short)1).setCellValue(vo.getComponentDesc());

index++;
}

String filePath = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRealPath("/")
+ "WEB-INF\" + System.currentTimeMillis() + "_" + fileName;
output = new FileOutputStream(filePath);
workBook.write(output);
output.flush();
output.close();

//下面写不写
exportFile = new File(filePath);
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
servletOutputStream = httpServletResponse.getOutputStream();
httpServletResponse.setHeader("Content-disposition",
"attachment; filename=" + fileName);
httpServletResponse.setContentLength((int) exportFile.length());
httpServletResponse.setContentType("application/x-download");
httpServletResponse.setContentType("application/vnd.ms-excel");
byte[] buffer = new byte[1024];
int flag = 0;
input = new FileInputStream(exportFile);
while ((flag = input.read(buffer)) > 0) {
servletOutputStream.write(buffer, 0, flag);
}
servletOutputStream.flush();
} catch (FileNotFoundException notFoundException) {
} catch (IOException ioException) {
} catch (Exception exception) {
} finally {
try {
if (output != null) {
output.close();
}

if (input != null) {
input.close();
}

if (servletOutputStream != null) {


servletOutputStream.close();
}

if (exportFile != null) {
exportFile.delete();
}
FacesContext.getCurrentInstance().responseComplete();
} catch (IOException ioException) {
}
}
}


[解决办法]
OH....代码分享吗
[解决办法]
下面写不写?不用写吧
[解决办法]
http://user.qzone.qq.com/474436236/blog/1327824678

引用:
下面写不写?不用写吧

热点排行
Bad Request.