1. 程式人生 > >POI技術之生成Excel

POI技術之生成Excel

    /*
	 * 匯出excel,excel字型樣式修改方法
	 */
	@SuppressWarnings("deprecation")
	public static boolean exportExcel(String outputPath,String headName,List data,Map formStyle,Workbook wb,Sheet hs){
		FileOutputStream fs = null;
		try {
			File file = new File(outputPath);
			if(!file.exists()){
				file.getParentFile().mkdirs();		
			}
			fs = new FileOutputStream(file);	
		 //設定大標題合併單元格範圍(開始行,結束行,開始列,結束列)預設從0開始
			CellRangeAddress ar = new CellRangeAddress(0, 3,(short)0, (short)(data.get(0).length-1)); 
			hs.addMergedRegion(ar); //合併單元格
			String[] topForm = formStyle.get("top");
			createCellTitle(0,3,(short)0, (short)(data.get(0).length-1),hs,wb,headName,topForm);
		    
			/*
			 * 注意下邊這個格子樣式(styleContent)一定要放在下邊for迴圈之外設定,
			 * 是因為changeColor方法用到一個靜態變數a,如果放在裡邊,頻繁的去設定格子樣式,
			 * 會導致a不斷變化,造成背景顏色和字型顏色會亂
			 */
			String[] titleForm = formStyle.get("title");
			short titleHeight = Short.valueOf(titleForm[1]);
			CellStyle styleContent = designCellStyle(wb,titleForm[0],titleHeight,titleForm[2],titleForm[3],titleForm[4]);  
			//在大標題下第一行開始寫欄位名(小標題)並修改字型樣式
			Row hrTitle = hs.createRow(4); 	
			for(int i = 0;icolLength){
						hs.setColumnWidth(j, contentL); //自動設定列寬
					}
					celContent.setCellValue(contentV); //在格子中賦值
					
					celContent.setCellStyle(sty);  //將該格子應用此樣式
				}
				hrContent.setHeightInPoints(contentHeight+(short)6); //將內容資料行高設定為(內容字型高度+6)
			}
			wb.write(fs);	
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			if(fs !=null){
				try {
					fs.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/*
	 * 設定字型格式
	 * form為字型樣式    例如:楷體 
	 * formHeight 為字型大小
	 * color為字型顏色 
	 * bold 為字型加粗或常規
	 * 返回值為cellStyle,可以將該型別新增到一個cell裡,即對該cell可以調整字型格式
	 */
	public static CellStyle designCellStyle(Workbook wb,String form,short formHeight,String colorForm,String colorBack,String bold){
		
		 //建立一個字型 
	    Font font = wb.createFont();  
	    //設定字型高度
	    font.setFontHeightInPoints(formHeight);  
	    //設定字型樣式
	    font.setFontName(form);  
	    font.setColor(changeColor(colorForm,wb));
	    short boldValue = (short)0;
	    if("加粗".equals(bold)){
	    	boldValue = Font.BOLDWEIGHT_BOLD;
	    }else if("常規".equals(bold)){
	    	boldValue = Font.BOLDWEIGHT_NORMAL;
	    }
	    font.setBoldweight(boldValue);
//	    //設定是否使用斜體  
//	    font.setItalic(true);  
//	    //設定是否刪除線通過字型  
//	    font.setStrikeout(true);  
	    //將建立的Font設定給CellStyle,所以需要建立一個新的Font  
	    CellStyle style = wb.createCellStyle();  
	    style.setAlignment(CellStyle.ALIGN_CENTER_SELECTION); //設定字型水平居中
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //設定字型垂直居中
	    style.setFont(font); 
	    style.setFillPattern(CellStyle.SOLID_FOREGROUND); //設定背景色沒有這一步,下一步將沒有作用,純色使用前景顏色填充
	    style.setFillForegroundColor(changeColor(colorBack,wb)); //設定前景顏色(不能設定成背景顏色)
	    style.setBorderBottom((short)1);
	    style.setBorderLeft((short)1);
	    style.setBorderTop((short)1);
	    style.setBorderRight((short)1);
	    return style;
	}
	/**
	 * 自動調整字型顏色邏輯
	 * str傳入的是十六進位制格式調整顏色
	 * 
	 */
	static int  a = 0;
	public static short changeColor(String str,Workbook wb){
		a++;
		short value = (short)(HSSFColor.BLACK.index+a);
	    //處理把它轉換成十六進位制並放入一個數
	    int[] color=new int[3];
	    color[0]=Integer.parseInt(str.substring(1, 3), 16);
	    color[1]=Integer.parseInt(str.substring(3, 5), 16);
	    color[2]=Integer.parseInt(str.substring(5, 7), 16);
	  //自定義顏色
	    HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette();
	    palette.setColorAtIndex(value,(byte)color[0], (byte)color[1], (byte)color[2]);
	    //將自定義的顏色引入進來 
	    return value;
	}
	
	public static void createCellTitle(int rowFrom,int rowTo,short colFrom,short colTo,Sheet hs,Workbook wb,String headName,String[] topForm){
		for(int i = rowFrom;i<=rowTo;i++){
			Row rowTitle = hs.createRow(i);
			for(short j = colFrom;j<=colTo;j++){
				Cell cellTitle = rowTitle.createCell(j);
				CellStyle style = wb.createCellStyle();
				style.setBorderBottom((short)1);
				style.setBorderLeft((short)1);
				style.setBorderTop((short)1);
				style.setBorderRight((short)1);
				if(i == rowFrom && j== colFrom){
					cellTitle.setCellValue(headName);
					short topheight = Short.valueOf(topForm[1]);
					rowTitle.setHeightInPoints(topheight); //將大標題的行高設定為(字型高度+5)
					style = designCellStyle(wb,topForm[0],topheight,topForm[2],topForm[3],topForm[4]);  
					cellTitle.setCellStyle(style);  //將大標題單元格應用此樣式  
					continue;
				}
				cellTitle.setCellStyle(style);
			}
		}
	}