JExcel - 学习总结(1)

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);
    }
}

 

转载于:https://www.cnblogs.com/ZeroMZ/p/11468340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值