1. 程式人生 > >Excel2003和Excel2007對下拉選擇和下拉級聯選擇的操作以及java程式的呼叫

Excel2003和Excel2007對下拉選擇和下拉級聯選擇的操作以及java程式的呼叫

Excel2007和Excel2003的部分功能選單有所調整
比如2003的“插入-名稱”,在2007中更為到“公式-定義的名稱”
比如2003的“插入-名稱-指定-首行”,在2007中更為到“公式-定義的名稱-根據所選內容建立-首行”

Excel功能點應用:
相對位置和絕對位置,特別在某個行列的資料是參考另外某個行列的資料而變動的,相對位置的表示方法:A8、B9等等,絕對位置的表示方法:$A$8、$B$9(就是使用美元符號$)

隱藏頁的資料引用。
2003中,假設sheet1是隱藏頁,並先定義好資料(公式-定義的名稱→定義;在sheet1中定義資料來源名稱:省份;引用位置:=Sheet1!$A$1:$A$5),然後在“新增”資料,設定資料的有效性選項(資料-資料有效性-序列;來源填寫“=省份”)
2007中,假設sheet1是隱藏頁,並先定義好資料(插入→名稱→定義;在sheet1中定義資料來源名稱:省份;引用位置:=Sheet1!$A$1:$A$5),然後在“新增”資料,設定資料的有效性選項(資料-資料有效性-序列;來源填寫“=省份”)

package com.fruitking.caipiao;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.util.CellRangeAddressList;

public class TestExcelSelect {

	
	 public static void main(String [] args) throws IOException {  
         HSSFWorkbook workbook = new HSSFWorkbook();//excel檔案物件  
         HSSFSheet userinfosheet1 = workbook.createSheet("使用者資訊表-1");//工作表物件
         HSSFSheet userinfosheet2 = workbook.createSheet("使用者資訊表-2");//工作表物件
         //建立一個隱藏頁和隱藏資料集
         TestExcelSelect.creatHideSheet(workbook, "hideselectinfosheet");
         //設定名稱資料集
         TestExcelSelect.creatExcelNameList(workbook);
         //建立一行資料
         TestExcelSelect.creatAppRow(userinfosheet1, "許果",1);
         TestExcelSelect.creatAppRow(userinfosheet1, "劉德華",2);
         TestExcelSelect.creatAppRow(userinfosheet1, "劉若英",3);
         TestExcelSelect.creatAppRow(userinfosheet2, "張學友",1);
         TestExcelSelect.creatAppRow(userinfosheet2, "林志玲",2);
         TestExcelSelect.creatAppRow(userinfosheet2, "林熙蕾",3);
         
         //生成輸入檔案
         FileOutputStream out=new FileOutputStream("success.xls");  
         workbook.write(out);  
         out.close();
     }
	 
	 /**
	  * 名稱管理
	  * @param workbook
	  */
	 public static void creatExcelNameList(HSSFWorkbook workbook){
		//名稱管理
         Name name;
         name = workbook.createName();
         name.setNameName("provinceInfo");
         name.setRefersToFormula("hideselectinfosheet!$A$1:$E$1");
         name = workbook.createName();
         name.setNameName("浙江");
         name.setRefersToFormula("hideselectinfosheet!$B$2:$K$2");
         name = workbook.createName();
         name.setNameName("山東");
         name.setRefersToFormula("hideselectinfosheet!$B$3:$I$3");
         name = workbook.createName();
         name.setNameName("江西");
         name.setRefersToFormula("hideselectinfosheet!$B$4:$E$4");
         name = workbook.createName();
         name.setNameName("江蘇");
         name.setRefersToFormula("hideselectinfosheet!$B$5:$I$5");
         name = workbook.createName();
         name.setNameName("四川");
         name.setRefersToFormula("hideselectinfosheet!$B$6:$K$6");
	 }
	 
	 
	 /**
	  * 建立隱藏頁和資料域
	  * @param workbook
	  * @param hideSheetName
	  */
	 public static void creatHideSheet(HSSFWorkbook workbook,String hideSheetName){
		 HSSFSheet hideselectinfosheet = workbook.createSheet(hideSheetName);//隱藏一些資訊
         //設定下拉列表的內容  
         String[] provinceList = {"浙江","山東","江西","江蘇","四川"};
         String[] zjProvinceList = {"浙江","杭州","寧波","溫州","台州","紹興","金華","湖州","麗水","衢州","舟山"};
         String[] sdProvinceList = {"山東","濟南","青島","煙臺","東營","菏澤","淄博","濟寧","威海"};
         String[] jxProvinceList = {"江西","南昌","新餘","鷹潭","撫州"};
         String[] jsProvinceList = {"江蘇","南京","蘇州","無錫","常州","南通","泰州","連雲港","徐州"};
         String[] scProvinceList = {"四川","成都","綿陽","自貢","瀘州","宜賓","攀枝花","廣安","達州","廣元","遂寧"};
         //在隱藏頁設定選擇資訊
         HSSFRow provinceRow = hideselectinfosheet.createRow(0);
         TestExcelSelect.creatRow(provinceRow, provinceList);
         HSSFRow zjProvinceRow = hideselectinfosheet.createRow(1);
         TestExcelSelect.creatRow(zjProvinceRow, zjProvinceList);
         HSSFRow sdProvinceRow = hideselectinfosheet.createRow(2);
         TestExcelSelect.creatRow(sdProvinceRow, sdProvinceList);
         HSSFRow jxProvinceRow = hideselectinfosheet.createRow(3);
         TestExcelSelect.creatRow(jxProvinceRow, jxProvinceList);
         HSSFRow jsProvinceRow = hideselectinfosheet.createRow(4);
         TestExcelSelect.creatRow(jsProvinceRow, jsProvinceList);
         HSSFRow scProvinceRow = hideselectinfosheet.createRow(5);
         TestExcelSelect.creatRow(scProvinceRow, scProvinceList);
         //設定隱藏頁標誌
         workbook.setSheetHidden(workbook.getSheetIndex(hideSheetName), true);
	 }
	 
	 /**
	  * 建立一列應用資料
	  * @param userinfosheet1
	  * @param userName
	  */
	 public static void creatAppRow(HSSFSheet userinfosheet1,String userName,int naturalRowIndex){
		//構造一個資訊輸入表單,使用者姓名,出生省份,出生城市
         //要求省份是可以下拉選擇的,出生城市根據所選擇的省份級聯下拉選擇
         //在第一行第一個單元格,插入下拉框
         HSSFRow row = userinfosheet1.createRow(naturalRowIndex-1);
         HSSFCell userNameLableCell = row.createCell(0);
         userNameLableCell.setCellValue("使用者姓名:");
         HSSFCell userNameCell = row.createCell(1);
         userNameCell.setCellValue(userName);
         HSSFCell provinceLableCell = row.createCell(2);
         provinceLableCell.setCellValue("出生省份:");
         HSSFCell provinceCell = row.createCell(3);
         provinceCell.setCellValue("請選擇");
         HSSFCell cityLableCell = row.createCell(4);
         cityLableCell.setCellValue("出生城市:");
         HSSFCell cityCell = row.createCell(5);
         cityCell.setCellValue("請選擇");
         
         //得到驗證物件  
         DataValidation data_validation_list = TestExcelSelect.getDataValidationByFormula("provinceInfo",naturalRowIndex,4);
         //工作表新增驗證資料  
         userinfosheet1.addValidationData(data_validation_list);
         DataValidation data_validation_list2 = TestExcelSelect.getDataValidationByFormula("INDIRECT($D"+naturalRowIndex+")",naturalRowIndex,6);
         //工作表新增驗證資料  
         userinfosheet1.addValidationData(data_validation_list2);
	 }
	 
	 /**
	  * 建立一列資料
	  * @param currentRow
	  * @param textList
	  */
	 public static void creatRow(HSSFRow currentRow,String[] textList){
		 if(textList!=null&&textList.length>0){
			 int i = 0;
			 for(String cellValue : textList){
				 HSSFCell userNameLableCell = currentRow.createCell(i++);
		         userNameLableCell.setCellValue(cellValue);
			 }
		 }
	 }
     
	 /**
	  * 對Excel自然行列設定一個數據驗證(並出現下拉列表選擇格式)
	  * @param selectTextList
	  * @param naturalRowIndex
	  * @param naturalColumnIndex
	  * @return
	  */
     public static DataValidation getDataValidationList(String[] selectTextList,int naturalRowIndex,int naturalColumnIndex){
         //載入下拉列表內容  
    	 DVConstraint constraint = DVConstraint.createExplicitListConstraint(selectTextList);
         //設定資料有效性載入在哪個單元格上。  
         //四個引數分別是:起始行、終止行、起始列、終止列  
         int firstRow = naturalRowIndex-1;
         int lastRow = naturalRowIndex-1;
         int firstCol = naturalColumnIndex-1;
         int lastCol = naturalColumnIndex-1;
         CellRangeAddressList regions=new CellRangeAddressList(firstRow,lastRow,firstCol,lastCol);  
         //資料有效性物件
         DataValidation data_validation_list = new HSSFDataValidation(regions,constraint);  
         return data_validation_list;  
     }
     
     /**
      * 使用已定義的資料來源方式設定一個數據驗證
      * @param formulaString
      * @param naturalRowIndex
      * @param naturalColumnIndex
      * @return
      */
     public static DataValidation getDataValidationByFormula(String formulaString,int naturalRowIndex,int naturalColumnIndex){
         //載入下拉列表內容  
    	 DVConstraint constraint = DVConstraint.createFormulaListConstraint(formulaString); 
         //設定資料有效性載入在哪個單元格上。  
         //四個引數分別是:起始行、終止行、起始列、終止列  
         int firstRow = naturalRowIndex-1;
         int lastRow = naturalRowIndex-1;
         int firstCol = naturalColumnIndex-1;
         int lastCol = naturalColumnIndex-1;
         CellRangeAddressList regions=new CellRangeAddressList(firstRow,lastRow,firstCol,lastCol);  
         //資料有效性物件 
         DataValidation data_validation_list = new HSSFDataValidation(regions,constraint);
         return data_validation_list;  
     }
}


原文來自:http://fruitking.iteye.com/blog/811931

其他關於POI的好文章:

http://blog.csdn.net/lk_blog/article/details/7234798

http://blog.csdn.net/lk_blog/article/details/7234736