今天,我想把“登录管理"中的人员清单,写程序用Excel导出来,本来想,这很简单啊,不就是一个Jtabel中的内容吗!结果仍是有些小问题让我困扰,后来,才发现是数据类型转换的问题形成的spa
"访客权限","管理员权限",这4个栏位都是用JCheckBox,在AbstractTableModel中有一个方法返回的是Boolean.class,在Jtabel表格中就是
true与false,true就是会打勾
get
public Class getColumnClass(int columnIndex) { string
// 返回每一列的数据类型 it
return typeArray.get(columnIndex);io
}table
通过排查,是如下的程序导出Excel时有问题
class
for(int i =0;i<b ;i++){object
for(int j=1;j<=a;j++){权限
String str = null; 数据类型
str = (String) table.getValueAt(j-1, i);
jxl.write.Label labelN = new jxl.write.Label(i, j, str);
try {
ws.addCell(labelN);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
//写入工做表
wwb.write();
修改以后:
for(int j=1;j<=a;j++){
String str = null;
if (i== 5 || i== 6 || i== 7 || i== 8){
//由于是Boolean类型,因此我先转为object,再转为string
//这样,程序就能够正常的导出Excel了
Object stry =table.getValueAt(j-1, i);
//强转为string
str = stry.toString();
if (str.equals("false")){
str = "";
}
}else{
str = (String) table.getValueAt(j-1, i);
}
jxl.write.Label labelN = new jxl.write.Label(i, j, str);
try {
ws.addCell(labelN);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
//写入工做表
wwb.write();