您的位置:首页 > 编程语言 > Java开发

jxl导出excel的一些方法

2012-01-11 17:44 369 查看
在做struts应用开发过程中,有时遇到需要导入导出excel,下面是其中一些常用的方法:

(1)导入excel表时对表中内容验证

for(int i=2; i<rowNum; i++){
for(int j=0;j<colNum;j++){
Cell cell = sheet.getCell(j, i);
String contents = cell.getContents();
if ((j == 0) || (j == 2) || (j == 3)) {
if (contents.equals("")){
res = "第" + (i+1) +"行"+"第"+(j+1)+"列的数据不能为空!";
check = false;
}
}
if(((j == 1)||(j == 3))&& check){
if (!(cell.getType() == CellType.LABEL)){
res = "第" + (i+1) +"行"+"第"+(j+1)+"列的数据应为字符型!";
check = false;
}
}

if((j == 1)&&(cell.getContents().length() > 30)&& check){
res = "第" + (i+1) +"行"+"第"+(j+1)+"列的数据不能超过30字符!";
check = false;
}
if((j == 3)&&(cell.getContents().length() > 1200)&& check){
res = "第" + (i+1) +"行"+"第"+(j+1)+"列的数据不能超过1200字符!";
check = false;
}
if(((j == 0)||(j == 2))&& check){
if ((cell.getType() != CellType.NUMBER)&& (cell.getType() != CellType.NUMBER_FORMULA)){
res = "第" + (i+1) +"行"+"第"+(j+1)+"列的数据应为数字型!";
check = false;
}
}
}
}

(2)设置某一列的单元格下拉菜单选项

public WritableCellFeatures addColumnList(String[] contentArray){
List<String> contentList=new ArrayList<String>();
WritableCellFeatures wcf=new WritableCellFeatures();
for(int i=0;i<contentArray.length;i++){
contentList.add(contentArray[i]);
}
wcf.setDataValidationList(contentList);
contentList.clear();
contentList=null;
return wcf;
}
然后Label=new Label(列,行,内容);
label.setCellFeatures(上面那个方法);
contentArray是下拉列表的显示的内容


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  excel wcf struts string