天天看點

EAS BOS Excel導出功能

1: EAS  自制導出功能

@Override
	public void actionExportASN_actionPerformed(ActionEvent e) throws Exception {
		int result = MsgBox.showConfirm2("===是否導出ASN===");
		if(MsgBox.OK==result){
			KDFileChooser fileChooser = new  KDFileChooser();     
			fileChooser.setDialogTitle("選擇導出路徑");
			fileChooser.setFileFilter(new ExcelFileFilter());
		    //fileChooser.showSaveDialog(this);
		    int customResult = fileChooser.showSaveDialog(this);
		    if(0==customResult){
		    	//MsgBox.showConfirm2(fileChooser.getSelectedFile()+"");
		    	String path = fileChooser.getSelectedFile()+"";
		    	exportASNExcel(path,editData.getId().toString());
		    }else{
		    	MsgBox.showConfirm2("======導出失敗,請聯系管理者======");
		    }
		    //int d=fileChooser.showDialog(this, "導出");
		    //MsgBox.showInfo(d+"");
		}
//		else{
//			MsgBox.showInfo("不是");
//		}
	}

	/**
	 * 導出Excel
	 * @param path
	 * @param string
	 * @throws Exception 
	 * @throws BOSException 
	 * @throws EASBizException 
	 */
	private void exportASNExcel(String path, String id) throws EASBizException, BOSException, Exception {
		HSSFWorkbook wb = new HSSFWorkbook();  
		HSSFSheet sheet = wb.createSheet("ASN");  
		HSSFRow row = sheet.createRow((int) 0);  
		HSSFCellStyle style = wb.createCellStyle();  
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個居中格式    
		initHeadCell(row,style);
		//寫入實體資料 實際應用中這些資料從資料庫得到,   
		List list = ((ICustomInformation) getBizInterface()).getASNResult(id); 
		for (int i = 0; i < list.size(); i++){  
			ASNInfo asnInfo = (ASNInfo)list.get(i);
			row = sheet.createRow((int) i + 1);  
			row.createCell(0).setCellValue(asnInfo.getAsnType());  
			row.createCell(1).setCellValue(asnInfo.getSupplierName());  
			row.createCell(2).setCellValue(asnInfo.getSupplierPartNo());  
			row.createCell(3).setCellValue(asnInfo.getCustomerName());  
			row.createCell(4).setCellValue(asnInfo.getCustomerPartNo());  
			row.createCell(5).setCellValue(asnInfo.getMfr());  
			row.createCell(6).setCellValue(asnInfo.getShippingQty().doubleValue());  
			row.createCell(7).setCellValue(asnInfo.getPrice().doubleValue());  
			row.createCell(8).setCellValue(asnInfo.getCurrency());  
			row.createCell(9).setCellValue(asnInfo.getCountryOfOriginal());  
			row.createCell(10).setCellValue(new SimpleDateFormat("yyyy/MM/dd").format(asnInfo.getEta()));  
			row.createCell(11).setCellValue(asnInfo.getInvoiceNo()); 
			if(asnInfo.getGrossWeight()==null){
				row.createCell(12).setCellValue("");
			}else{
				row.createCell(12).setCellValue(asnInfo.getGrossWeight().doubleValue());  
			}
			if(asnInfo.getCartonQty()==null){
				row.createCell(13).setCellValue(""); 
			}else{
				row.createCell(13).setCellValue(asnInfo.getCartonQty().doubleValue()); 
			}
			if(asnInfo.getPalletQty()==0){
				row.createCell(14).setCellValue("");
			}else{
				row.createCell(14).setCellValue(asnInfo.getPalletQty());  
			}

			row.createCell(15).setCellValue(asnInfo.getCustomerPO());  
			row.createCell(16).setCellValue(asnInfo.getCustomerPOItem());  
			row.createCell(17).setCellValue(asnInfo.getPartnerReferenceNo());  
			row.createCell(18).setCellValue(asnInfo.getForwarderName());  
			row.createCell(19).setCellValue(asnInfo.getSupplierPackName());  
			row.createCell(20).setCellValue(asnInfo.getXOut());  
			row.createCell(21).setCellValue(asnInfo.getOwnershipType());  
		}
		try  
		{  
			FileOutputStream fout = new FileOutputStream(path+".xls");  
			wb.write(fout);  
			fout.close();  
		}  
		catch (Exception e)  
		{  
			e.printStackTrace();  
			MsgBox.showInfo("======導出失敗======");
		} 
                MsgBox.showInfo("======導出成功======");
	}