---
title: "EXCEL導出"
author: "cdhychen"
date: "2011-08-09T15:34:14Z"
tags: []
categories: ["Web前端"]
slug: "excel"
draft: false
lastmod: "2011-08-09T15:34:14Z"
id: 1143560
url: "https://cdhychen.iteye.com/blog/1143560"
---

public void getStockTaskInfoExcel(String moldNo, String formNo)  
   throws Exception {  
    
  //開發始寫Excel  
  File sourceFile = new File(StockConstants.stockUrl + StockConstants.stockUrlFileNameTarget);  
  File targetFile = new File(StockConstants.stockUrl + StockConstants.stockUrlFileName);  
       
     //創建Excel  
     jxl.Workbook rw = jxl.Workbook.getWorkbook(sourceFile);  
     jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(targetFile, rw);

     //設置格式  
     jxl.write.WritableFont wf = new jxl.write.WritableFont(  
      WritableFont.COURIER, 18, WritableFont.BOLD, false);  
     jxl.write.WritableCellFormat wcf = new jxl.write.WritableCellFormat();  
     wcf.setBorder(jxl.format.Border.RIGHT, jxl.format.BorderLineStyle.MEDIUM);  
     wcf.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.MEDIUM);  
     wcf.setAlignment(jxl.format.Alignment.CENTRE);  
     jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);  
     wcfF.setAlignment(jxl.format.Alignment.CENTRE);  
     wcfF.setBorder(jxl.format.Border.RIGHT, jxl.format.BorderLineStyle.MEDIUM);  
     wcfF.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.MEDIUM);  
       
     jxl.write.WritableFont wfb = new jxl.write.WritableFont(  
     WritableFont.COURIER, 12, WritableFont.BOLD, false);  
       
     jxl.write.WritableCellFormat wcfa = new jxl.write.WritableCellFormat(wfb);  
     wcfa.setAlignment(jxl.format.Alignment.CENTRE);  
       
     jxl.write.WritableCellFormat wcfb = new jxl.write.WritableCellFormat();  
     wcfb.setBorder(jxl.format.Border.LEFT, jxl.format.BorderLineStyle.MEDIUM);  
     wcfb.setBorder(jxl.format.Border.RIGHT, jxl.format.BorderLineStyle.MEDIUM);  
     wcfb.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.MEDIUM);  
     wcfb.setAlignment(jxl.format.Alignment.CENTRE);

     jxl.write.WritableSheet ws = wwb.getSheet(0);  
     jxl.write.Label label1 = null;  
     //ws.addCell(label1);  
       
     //查找並寫EXCEL  
     List&lt;UnStandardBean&gt; stockInfo = this.getStockTaskInfo();  
       
     int row = 7;  
     String factoryChineseName = "";  
     String factoryEnglishName = "";

      factoryChineseName = StockConstants.factoryChineseNameLh;  
      factoryEnglishName = StockConstants.factoryEnglishNameLh;

     label1 = new jxl.write.Label(5, 2, factoryChineseName, wcfa);  
     ws.addCell(label1);  
     label1 = new jxl.write.Label(5, 3, factoryEnglishName, wcfa);  
     ws.addCell(label1);  
  if(stockInfo!=null && stockInfo.size() &gt; 0){  
   for (UnStandardBean bean : stockInfo) {

    label1 = new jxl.write.Label(1, row, String.valueOf(row-6), wcfb);  
       ws.addCell(label1);  
       label1 = new jxl.write.Label(2, row, bean.getMoldNo(), wcf);  
       ws.addCell(label1);  
       label1 = new jxl.write.Label(3, row, bean.getPartNo(), wcf);  
       ws.addCell(label1);  
       label1 = new jxl.write.Label(4, row, bean.getPartName(), wcf);  
       ws.addCell(label1);  
       label1 = new jxl.write.Label(5, row, String.valueOf(bean.getAcceptNum()), wcf);  
       ws.addCell(label1);  
       String nowStyle = "";  
       if(bean.getStyle() != null && bean.getStyle().length()&lt;=0){  
        nowStyle = bean.getOrdNumber();  
       }else{  
        nowStyle = bean.getStyle();  
       }  
       label1 = new jxl.write.Label(6, row, nowStyle, wcf);  
       ws.addCell(label1);  
       label1 = new jxl.write.Label(7, row, bean.getNote(), wcf);  
       ws.addCell(label1);  
       row++;  
   }  
     
  }  
     //增加內部版權標識  
     String  copyRight= StockConstants.copyRight;  
     label1 = new jxl.write.Label(5,row+2, copyRight, wcfa);  
     ws.addCell(label1);  
      
     wwb.write();  
     wwb.close();  
    
 }