jxl自己使用

 工具类的下载地址:  http://www.andykhan.com/jexcelapi/download.html

 

    工具类的使用:

   

public class JxlUtil {
 
        /**
         * 将一个二维数组的数据写到excel文件中
         * @param filepath
         * @param datas
         * @throws IOException
         * @throws RowsExceededException
         * @throws WriteException
         */
        public static void  writeExcel(String filepath,String[][] datas) throws IOException, RowsExceededException, WriteException{
                WritableWorkbook wwbook = Workbook.createWorkbook( new File(filepath)) ;
                WritableSheet ws = wwbook.createSheet("tablendata", 0);
                Label label;
                for(int row=0;row<datas.length;row++){
                        String[] column_data =datas[row]; 
                        for(int column=0;column<column_data.length;column++){
                           label = new Label(column,row,datas[row][column],getPlainCF());
                           ws.addCell( label );
                        }
                }
                /*
                 * 设置 某行的高     某列的宽
                 */
                ws.setColumnView( 0, 30);
                ws.setRowView(1, 400);
   
            wwbook.write();  
            wwbook.close();
            System.out.println( "结束!");
        }
        
   /**
    * 设置单元格的格式
    * @return
    * @throws WriteException
    */
        public static CellFormat getPlainCF() throws WriteException{
                
                /*
                 * 设置单元格的字体
                 */
                WritableFont wf = new WritableFont( WritableFont.TIMES ) ;
                wf.setColour( Colour.LIGHT_ORANGE);
   
                /*
                 * 设置单元格的格式
                 */
                WritableCellFormat wcf  = new  WritableCellFormat(wf) ;
                wcf.setAlignment(  Alignment.RIGHT );
                wcf.setBackground( Colour.WHITE);
                wcf.setBorder( Border.ALL, BorderLineStyle.THIN);
                wcf.setOrientation(Orientation.HORIZONTAL);
                wcf.setVerticalAlignment(VerticalAlignment.TOP);
                return wcf;
        }
 
        /**
         * 这种方法 每次循环到该行的最后一个  速度快一些
         * @param filepath
         * @return
         * @throws IOException
         * @throws BiffException
         */
        public static  List readExcel(String filepath) throws IOException, BiffException{
                List list = new ArrayList();
                List list_temp  ;
                
                Workbook wwbook = Workbook.getWorkbook( new File(filepath)) ;
                Sheet ws = wwbook.getSheet(0 ) ;
                
                
                int row_count = ws.getRows();//的都总行数
                
                for(int i=0;i<row_count;i++){
                        Cell[] cells = ws.getRow(i); //得到每行的单元格数组
                        list_temp = new ArrayList();
                        for(int j=0;j<cells.length;j++){
                                System.out.print(cells[j].getContents() ); 
                                list_temp.add( cells[j].getContents()); //得到每个cell中的数据
                        }
                                System.out.println();  
                        list.add(list_temp );
                }
                 
                return list;
        }

         /**
          * 每行都运行到该 表格的最大一行 读取慢
          * @param filename
          * @throws BiffException
          * @throws IOException
          */
         public static  void readExc(File filename) throws BiffException, IOException{   
                  
                    Workbook wb = Workbook.getWorkbook(filename);   
                    Sheet s = wb.getSheet(0);//第1个sheet   
                    Cell c = null;   
                    int row = s.getRows();//总行数   
                    int col = s.getColumns();//总列数   
                    for(int i=0;i<row;i++){   
                     for(int j=0;j<col;j++){   
                      c = s.getCell(j,i);   
                      System.out.print(c.getContents()+"  ");   
                     }   
                     System.out.println();   
                    }      
                 }  
        
        public static void main(String[] args) throws RowsExceededException, WriteException, IOException, BiffException {
//                String[][] datas= {{"1","","3"},{"1","2","3"},{"1","2","3"}};
//                writeExcel("d:/test.xls", datas);
                
//                File f = new File("d:/test.xls");
                List list = readExcel("d:/test1.xls");
         
//                readExc(new File("d:/test1.xls"));
                
//                for(int i=0;i<list.size();i++){
//                        List list2 = (List) list.get(i);
//                        for(int j=0;j<list2.size();j++){
//                                System.out.print( list2.get(j)+"   ");
//                        }
//                        System.out.println( );
//                }     
        } 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值