假設(shè)有物品編號P1,P2,及其價格10,20,
生成的excel表格第一行第一列是下拉列表,其中值為P1或P2,
選擇P1,第一行第二列自動填充價格10
選擇P2,第一行第二列自動填充價格20
用java代碼實現(xiàn)。
有沒有哪個大佬會的啊,百度巨久,就只能實現(xiàn)下拉,但是自動填充那真的是完全找不到。
//因為是盲打過來的,我驗證的數(shù)據(jù)并不是這個,但是為了符合題目還是寫了這個,所以參數(shù)有可能在表格中位置會
//有點不對,但是實驗一下還是可以很容易看出來的。
String[] p = {"p1","p2"};
String[] price = {"10","20"};
int rindex = 0;
Name name;
HSSFWorkbook workbook = new HSSFWorkbook();//excel文件對象
HSSFSheet sheet = workbook.createSheet("Info");//工作表對象
HSSFSheet hidesheet = workbook.createSheet("hideSheet");//隱藏一些信息
HSSFRow row = hidesheet.createRow(rindex++);
//設(shè)置物品編號
for(int i=0;i<p.size();i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(p[i]);
}
// 名稱管理
name = workbook.createName();
name.setNameName("物品編號");
name.setRefersToFormula("hidesheet!$A$"+rindex+":$"+judgePos(p.size())+"$"+rindex);
for(int i=0;i<p.size();i++){
HSSFRow row = hidesheet.createRow(rindex++);
HSSFCell cell = row.createCell(0);
cell.setCellValue(p[i]);
cell = row.createCell(1);
cell.setCellValue(price[i]);
}
String[] title = {"物品編號","物品價格"};
setTitle(sheet ,title,0);
HSSFRow allocationRow = sheet.createRow(rindex++);
**allocationRow.createCell(4).setCellFormula("LOOKUP(A2,hideSheet!A2:A3,hideSheet!B2:B3)");**
// 得到驗證對象
DataValidation validation = getDataValidationByFormula("物品編號",2,1);
// 工作表添加驗證數(shù)據(jù)
sheet.addValidationData(validation);
// 生成輸入文件
File file = new File(filePath);
FileOutputStream out = new FileOutputStream(file);
workbook.write(out);
out.close();
//以上大概就是個全過程,主要是設(shè)置LOOKUP函數(shù),我原先一直沒搞出來是因為函數(shù)參數(shù)寫錯了,搞半天沒搞出來,今天搞出來了。
//用到的函數(shù)我也貼一下,有些用別人的,有些自己寫的。
/**
* 返回所在列的字符
* @param size
* @return
*/
public String judgePos(int size) {
String[] args= {"A","B","C","D","E","F","G","H","I","J","K","L","M","N",
"O","P","Q","R","S","T","U","V","W","X","Y","Z"};
boolean tag=true;
StringBuffer reversepos=new StringBuffer();
while(tag) {
int pos=size%26;
if(pos==0) {
pos=25;
size--;
}else {
pos-=1;
}
int result = size/26;
if(result==0) {
reversepos.append(args[pos]);
tag=false;
}else {
reversepos.append(args[pos]);
size/=26;
}
}
return reversepos.reverse().toString();
}
/**
* 創(chuàng)建表頭
* @param sheet
* @param title
*/
public void setTitle(HSSFSheet sheet,String[] title,int index) {
HSSFRow row = sheet.createRow(index);
for(int i=0;i<title.length;i++) {
HSSFCell userNameLableCell = row.createCell(i);
userNameLableCell.setCellValue(title[i]);
}
}
/**
* 使用已定義的數(shù)據(jù)源方式設(shè)置一個數(shù)據(jù)驗證
*
* @param formulaString
* @param startRow
* @param startCol
* @return
*/
public static DataValidation getDataValidationByFormula(String formulaString,int startRow,int startCol) {
// 加載下拉列表內(nèi)容
DVConstraint constraint = DVConstraint.createFormulaListConstraint(formulaString);
// 設(shè)置數(shù)據(jù)有效性加載在哪個單元格上。
// 四個參數(shù)分別是:起始行、終止行、起始列、終止列
int firstRow = startRow-1;
int lastRow = startRow-1;
int firstCol = startCol - 1;
int lastCol = startCol - 1;
CellRangeAddressList regions = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
// 數(shù)據(jù)有效性對象
DataValidation validation = new HSSFDataValidation(regions, constraint);
return validation;
}
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團,成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達內(nèi)教育集團成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。