Java 操作Excel数据方法

4 篇文章 0 订阅
2 篇文章 0 订阅

        JAVA 通常有两种方法来操作Excel,分别是POI和JExcelAPI,而且都是开源的。POI是Apace公司开发的,对中文的支持比较弱一些;JExcelAPI是韩国公司开发的,对中文的支持好,而且由于是纯JAVA编写的,所以可以跨平台操作。本文介绍的也是JExcelAPI的使用方法。

1、环境配置

   到 http://www.andykhan.com/jexcelapi/download.html 下载对应的功能包

   然后解压得到:

     jxl.jar           

     源代码            

     文档

如果使用集成开发环境,则只要引入jxl.jar功能包就可以。

 本人读取Excel文件的代码如下:

 

package com.shinewaysoft.supperlotto.util;

import com.shinewaysoft.supperlotto.database.SupperLottoTable;
import com.shinewaysoft.supperlotto.database.SupperLottoTableJpaController;
import com.shinewaysoft.supperlotto.database.exceptions.NonexistentEntityException;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

/**
 * 
 * @describe 使用了jxl功能包,用于读取要导入的Excel文件
 * @author <guowenbin>
 * @date (2012-1-13)
 * 
 */
public class ReadInXLS {
//    public static void main(String[] args) throws IOException{
//        ReadInXLS.getExcelFile("C:\\Documents and Settings\\Administrator\\桌面\\新建 Microsoft Excel 工作表.xls");
//    }
    public ReadInXLS(){}
    /*
     获得excel文件,并读取第一个sheet对应的内容
     */
    public static boolean getExcelFile(String filePath) throws IOException, NonexistentEntityException, Exception{
        try {
            String oldChar = "\\";
            String newChar = "\\\\";
            filePath = filePath.replace(oldChar, newChar);
            File file = new File(filePath);
            if(file.exists()){
                Workbook book = Workbook.getWorkbook(file);
                //  获得第一个工作表对象
                Sheet sheet  =  book.getSheet(0);
                //  得到第一列第一行的单元格 
                int row = sheet.getRows();
                
                EntityManagerFactory emf = Persistence.createEntityManagerFactory("SupperLotto1.0PU");                
                SupperLottoTable supperLottoTable = new SupperLottoTable();
                SupperLottoTableJpaController controller = new SupperLottoTableJpaController(emf);
                
                Cell[] cell = null;
                for(int i=1; i<row ; i++){
                    cell = sheet.getRow(i);
                    supperLottoTable.setIssue(Integer.parseInt(cell[0].getContents()));
                    supperLottoTable.setBefNumber1(Integer.parseInt(cell[1].getContents()));
                    supperLottoTable.setBefNumber2(Integer.parseInt(cell[2].getContents()));
                    supperLottoTable.setBefNumber3(Integer.parseInt(cell[3].getContents()));
                    supperLottoTable.setBefNumber4(Integer.parseInt(cell[4].getContents()));
                    supperLottoTable.setBefNumber5(Integer.parseInt(cell[5].getContents()));
                    supperLottoTable.setAftNumber1(Integer.parseInt(cell[6].getContents()));
                    supperLottoTable.setAftNumber2(Integer.parseInt(cell[7].getContents()));
                    controller.edit(supperLottoTable);
                }  
                book.close();
            } else {
                System.out.println("没有找到文件");
                return false;
            }
        } catch (BiffException ex) {
            Logger.getLogger(ReadInXLS.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return true;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

收获de季节

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值