public ActionForward saveFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelApplyForm modelApplyForm = (ModelApplyForm) form;// TODO Auto-generated method stub
String id=modelApplyForm.getItems().getModelApplyId();
ModelApply modelApply= this.modelApplyBiz.getModelApplyById(id);
List list=this.modelapplyInfoBiz.getModelApplyInfoByModelApplyId(id);
 String richvote_id ="模型開發申請信息";  
 String  path=request.getSession().getServletContext().getRealPath("/");
//图片存储路径
 path=this.getServlet().getServletContext().getRealPath("\\")+"images\\ModelImages\\";
 try{
 response.setContentType("application/vnd.ms-excel");
 response.addHeader("Content-Disposition","attachment;  filename=\""   + new String(richvote_id.getBytes("GBK"),"ISO8859_1")+".xls"  +   "\"");
 OutputStream os = response.getOutputStream();
WritableWorkbook wwb = Workbook.createWorkbook(os);
//新建一张表
     WritableSheet wsheet = wwb.createSheet(richvote_id,0);
     WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, 
     WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, 
     jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色 
     WritableCellFormat wcf = new WritableCellFormat(wf); // 单元格定义 
   //wcf.setBackground(jxl.format.Colour.BLACK); // 设置单元格的背景颜色 
   wcf.setAlignment(jxl.format.Alignment.RIGHT); // 设置对齐方式 
   WritableCellFormat wcfl = new WritableCellFormat(wf); // 单元格定义 
   //wcf.setBackground(jxl.format.Colour.BLACK); // 设置单元格的背景颜色 
   wcfl.setAlignment(jxl.format.Alignment.LEFT); // 设置对齐方式 
   WritableCellFormat twcf = new WritableCellFormat(wf);
   twcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
   Label label = new Label(0,0,"勢創實業股份有限公司",twcf);
   wsheet.mergeCells(0, 0, 5, 0);
   wsheet.addCell(label);
   label=new Label(0,1,"產品編號",wcf);
   wsheet.addCell(label);
   label=new Label(1,1,modelApply.getModelReview().getProductPlan().getProductId(),wcf);
   wsheet.addCell(label);
   label=new Label(0,2,"產品名稱",wcf);
   wsheet.addCell(label);
   label=new Label(1,2,modelApply.getModelReview().getProductPlan().getProductName(),wcf);
   wsheet.addCell(label);
   label=new Label(0,3,"產品英文名稱",wcf);
   wsheet.addCell(label);
   label=new Label(1,3,modelApply.getModelReview().getProductPlan().getProductEngName(),wcf);
   wsheet.addCell(label);
   label=new Label(0,4,"備註",wcf);
   wsheet.addCell(label);
   wsheet.mergeCells(1, 4, 6, 4);
   label=new Label(1,4,modelApply.getModelApplyDesc(),wcf);
   wsheet.addCell(label);
   label=new Label(2,1,"客戶",wcf);
   wsheet.addCell(label);
   label=new Label(3,1,modelApply.getModelReview().getProductPlan().getCustomer().getCname(),wcf);
   wsheet.addCell(label);
   label=new Label(2,2,"客戶接洽人",wcf);
   wsheet.addCell(label);
   label=new Label(3,2,modelApply.getHandleUser(),wcf);
   wsheet.addCell(label);
   label=new Label(2,3,"向客戶收費",wcf);
   wsheet.addCell(label);
   label=new Label(3,3,modelApply.getModelReview().getProductPlan().getCutomerPay()==0?"是":"否",wcf);
   wsheet.addCell(label);
   label=new Label(4,1,"申請單號",wcf);
   wsheet.addCell(label);
   label=new Label(5,1,modelApply.getModelApplyId(),wcf);
   wsheet.addCell(label);
   label=new Label(4,2,"日期",wcf);
   wsheet.addCell(label);
   SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");
   label=new Label(5,2,dateformat.format(modelApply.getCreatedate()),wcf);
   wsheet.addCell(label);
   label=new Label(4,3,"經辦人",wcf);
   wsheet.addCell(label);
   label=new Label(5,3,modelApply.getCreateUser(),wcf);
   wsheet.addCell(label);
   label=new Label(4,4,"頁數",wcf);
   insertSheet( label, wsheet, list,wcf,path,wcfl);
   wwb.write();
   wwb.close();
   os.close();
 }catch (Exception e) {
// TODO: handle exception
}
return null;
}public  void insertSheet(Label label,WritableSheet sheet,List list,WritableCellFormat wcf,String path,WritableCellFormat wcfl){
 try{
 for(int i=0;i<list.size();i++)
 {
 Integer no=i+1;
 ModelApplyInfo info=(ModelApplyInfo) list.get(i);
 label=new Label(0,i*7+5,"項次",wcf);
 sheet.addCell(label);
 label=new Label(1,i*7+5,no.toString(),wcfl);
 sheet.addCell(label);
 //合併單元格
 sheet.mergeCells(1, i*7+5, 4, i*7+5);
 label=new Label(0,i*7+6,"配件圖檔編號",wcf);
 sheet.addCell(label);
 label=new Label(1,i*7+6,info.getModelReviewInfo().getApplyDrawId(),wcfl);
 sheet.addCell(label);
 //合併單元格
 sheet.mergeCells(1, i*7+6, 4, i*7+6);
 label=new Label(0,i*7+7,"配件名稱",wcf);
 sheet.addCell(label);
 label=new Label(1,i*7+7,info.getModelReviewInfo().getFittingName(),wcfl);
 sheet.addCell(label);
 sheet.mergeCells(1, i*7+7, 4, i*7+7);
 label=new Label(0,i*7+8,"模型材質",wcf);
 sheet.addCell(label);
 label=new Label(1,i*7+8,info.getModelMaterial().getMaterialName(),wcfl);
 sheet.addCell(label);
 sheet.mergeCells(1, i*7+8, 2, i*7+8);
 label=new Label(0,i*7+9,"後處理",wcf);
 sheet.addCell(label);
 label=new Label(1,i*7+9,info.getColorHandel().getHandelName(),wcfl);
 sheet.addCell(label);
 sheet.mergeCells(1, i*7+9, 2, i*7+9);
 label=new Label(0,i*7+10,"Pantone號",wcf);
 sheet.addCell(label);
 label=new Label(1,i*7+10,info.getPantoneNo(),wcf);
 sheet.addCell(label);
 sheet.mergeCells(1, i*7+10, 2, i*7+10);
 label=new Label(0,i*7+11,"說明圖檔號",wcf);
 sheet.addCell(label);
 label=new Label(1,i*7+11,info.getExplainDrawId(),wcfl);
 sheet.addCell(label);
 sheet.mergeCells(1, i*7+11, 3, i*7+11);
 label=new Label(3,i*7+8,"數量",wcf);
 sheet.addCell(label);
 label=new Label(4,i*7+8,info.getModelNum().toString(),wcfl);
 sheet.addCell(label);
 label=new Label(3,i*8+8,"顔色",wcf);
 sheet.addCell(label);
 label=new Label(4,i*8+8,info.getColor().getCname(),wcfl);
 sheet.addCell(label);
 label=new Label(3,i*7+10,"其他處理",wcf);
 sheet.addCell(label);
 label=new Label(4,i*7+10,info.getOtherHandelId(),wcfl);
 sheet.addCell(label);
 label=new Label(4,i*7+11,"備註",wcf);
 sheet.addCell(label);
 label=new Label(5,i*7+11,info.getModelDesc(),wcfl);
 sheet.addCell(label);
 sheet.mergeCells(5,i*7+11,  6, i*7+11);
 sheet.mergeCells(5,i*7+5,  6, i*7+10);
                                      //导出图片
 String imgName=info.getModelReviewInfo().getModelImg();
 File inputFile = new File(path+imgName); 
 BufferedImage input = ImageIO.read(inputFile);
 File outputFile = new File(path+imgName.substring(0,imgName.indexOf("."))+".png");  
 ImageIO.write(input, "PNG", outputFile);
 WritableImage ri=new WritableImage(5,i*7+5,6,i*7+10,new File("E:/271568.png"));   
 sheet.addImage(ri);  
 }
 }catch (Exception e) {
// TODO: handle exception
 System.out.println(e.getStackTrace());
}为什么加上图片导出哪一段代码后。导出的ecxl里面什么都没有了
如果不加就正常(没图片)

解决方案 »

  1.   

    关于excel导出图片时要设置相应的图片格式
      

  2.   

    这个问题我也想知道 求答复 excel导出图片
      

  3.   

    把后缀名都改为png格式的就可以导出了 jxl只支持这个格式
      

  4.   


    String realpath = request.getRealPath("/");
    System.out.println("[realpath]:"+realpath+"voephoto"+File.separator+list.get(15).toString());
    File imgFile = new File(realpath+"voephoto"+File.separator+list.get(15).toString()); 
    WritableImage image = null;
    if(imgFile != null && imgFile.exists()){
      image = new WritableImage(5, 9, 2, 4, imgFile);
      sheet.addImage(image);
    }