JExcel - 学习总结(1)

时间:2024-03-11 15:39:25

1、什么是JExcel

   JExcel是Java对Excel进行操作的包,可以实现创建一个Excel并写入或读取Excel的数据等操作;

   JExcel的主要类为:

     (1)Workbook:工作簿

     (2)WritableWorkbook:可写工作簿

     (3)Sheet:表单;

     (4)WritableSheet:可写表单;

     (5)Label:单元格;

Maven依赖引入:

<dependency>
      <groupId>net.sourceforge.jexcelapi</groupId>
      <artifactId>jxl</artifactId>
      <version>2.6.12</version>
</dependency>

项目结构:

2、Java输出Excel

public static String createSheet() throws IOException, RowsExceededException, WriteException {
        
        String str[][] = {{"姓名","编号"},{"终结者","54321"}};  
        File f = new File("test.xls");  
        WritableWorkbook workbook = Workbook.createWorkbook(f);  
        WritableSheet sheet = workbook.createSheet("sheet1", 0);  
        Label lab = null;  
        for(int i=0;i<str.length;i++){  
            for(int j=0;j<str[i].length;j++){  
                lab = new Label(j,i,str[i][j]); //Label(col,row,str);  
                sheet.addCell(lab);  
            }  
        }  
        workbook.write();  
        workbook.close();
        return "1";
    }

3、Java读取Excel

public static String readSheet() throws BiffException, IOException {
        
        Workbook workbook = Workbook.getWorkbook(new File("test.xls"));  
        Sheet sheet[] = workbook.getSheets();  
        String lab = null;  
        for(int a=0;a<sheet.length;a++){  
            for(int i=0;i<sheet[a].getRows();i++){  
                for(int j=0;j<sheet[a].getColumns();j++){  
                    lab = sheet[a].getCell(j,i).getContents();  
                    System.out.print(lab+"、");  
                }  
                System.out.println();  
            }  
        }  
        return "1";
    }

4、运行测试

AppTest.java

package com.lfy.cn.JexcelapiTest;

import java.io.IOException;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import jxl.read.biff.BiffException;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        String res="0";
//        try {
//            res=App.createSheet();
//        } catch (RowsExceededException e) {
//            e.printStackTrace();
//        } catch (WriteException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        
        try {
            res=App.readSheet();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        assertEquals("1",res);
    }
}