JTabel中有JCheckBox如何導出Excel

今天,我想把「登錄管理"中的人員清單,寫程序用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();

相關文章
相關標籤/搜索