今天,我想把“登陆管理"中的人员清单,写程序用Excel导出来,原本想,这很简单啊,不就是一个Jtabel中的内容吗!结果还是有些小问题让我困扰,后来,才发现是数据类型转换的问题造成的
"访客权限","管理员权限",这4个栏位都是用JCheckBox,在AbstractTableModel中有一个方法返回的是Boolean.class,在Jtabel表格中就是
true与false,true就是会打勾
public Class getColumnClass(int columnIndex) {
// 返回每一列的数据类型
return typeArray.get(columnIndex);
}
经过排查,是以下的程序导出Excel时有问题
for(int i =0;i<b ;i++){
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();