jxl读取excel导入到数据库,报错java.lang.NoClassDefFoundError: jxl/read/biff/BiffException

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jxl读取excel导入到数据库,报错java.lang.NoClassDefFoundError: jxl/read/biff/BiffException相关的知识,希望对你有一定的参考价值。

jxl包已经加载了

我给你一个我的例子吧:
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.sql.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import jxl.*;
public class SimUpdate
private String fileName;
public SimUpdate(String fileName)
this.fileName = fileName;

static Map tNames;
static
tNames = new HashMap();

//下面是主要代码

private void updateDb()
try
Connection conn = DbPool.connectDB();
if(conn != null)
Statement stmt = conn.createStatement();
/**********************************************/
jxl.Workbook rwb = null;
try
//构建Workbook对象 只读Workbook对象
//直接从本地文件创建Workbook
//从输入流创建Workbook
InputStream is = new FileInputStream(fileName);
rwb = Workbook.getWorkbook(is);
//Sheet(术语:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
//Sheet的下标是从0开始的
//获取第一张Sheet表
Sheet rs = rwb.getSheet(0);
//获取Sheet表中所包含的总列数
int rsColumns = rs.getColumns();
//获取Sheet表中所包含的总行数
int rsRows = rs.getRows();
//获取指这下单元格的对象引用

String simNumber = "",termSeqId = "";
//指定SIM卡号及序列号
for(int i=0;i<rsRows;i++)
for(int j=0;j<rsColumns;j++)
Cell cell = rs.getCell(j,i);
if(j==0)
simNumber = cell.getContents();//这里是猎取你要的参数,和下面一样

if(j == 1)
termSeqId = cell.getContents();


String sql = "update ....";//SQL语句
int isOk = stmt.executeUpdate(sql);
if(isOk == 0)

String insertSql = "insert....";//SQL语句
int isAdd = stmt.executeUpdate(insertSql);
if(isAdd > 0)
System.out.println("成功插入第"+i+"条数据");






//以下代码为写入新的EXCEL,这里不使用,所以注释
/*

//利用已经创建的Excel工作薄创建新的可写入的Excel工作薄
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File("D://Book2.xls"),rwb);
//读取第一张工作表
jxl.write.WritableSheet ws = wwb.getSheet(0);

//获取第一个单元格对象
jxl.write.WritableCell wc = ws.getWritableCell(0, 0);
//决断单元格的类型,做出相应的转化
if (wc.getType() == CellType.LABEL)
Label l = (Label) wc;
l.setString("The value has been modified.");

//写入Excel对象
wwb.write();
wwb.close();
*/
catch(Exception e)
e.printStackTrace();

finally
//操作完成时,关闭对象,翻译占用的内存空间
rwb.close();


/*********************************************/


catch(Exception e)
e.printStackTrace();



追问

我代码没有报错啊,是运行的时候报错的。我的excel文件是wps转化的格式,不知道是不是这个的问题,然后报错那里还说我的servlet有问题,然后我重建了一下发现好像没问题。网上其他的说这个问题就是jxl包没加载,可是我加载了,不知道是什么问题。

参考技术A 你的问题解决了吗?我也遇到了一样的问题现在也没办法搞懂

springMVC jxl数据库导入Excel文件

1.新建一个jsp表单 用到multipart

  <form id="form2" method="post" enctype="multipart/form-data"     action="<%=basePath%>uploadftp.shtml">
        <input type="file" name="file">
        <input type="submit" value="ftp提交">
    </form>

2.Controller代码 ftpserver判断 然后是username判断

如果没有ftpserver添加 添加mapper中设置主键添加

useGeneratedKeys="true"和主键id对应 Conroller下面的红色代码获得主键
<insert id="addFtpServer" useGeneratedKeys="true" parameterType="ftpServerBean" keyProperty="Id">

 

@Controller
@RequestMapping("/")
public class UploadFtpController {
    @Autowired
    private FtpService ftpService;
    
    @Autowired
    private FtpServerService ftpServerService;
    
    @Autowired
    private CustomerService customerService;
    
    @RequestMapping("uploadftppage")
    public String uploadcustomerpage()
    {
        return "help/uploadcustomer";
    }
    /**
     * @param request
     * @param file
     */
    /**
     * @param request
     * @param file
     */
    @RequestMapping("uploadftp")
    public void uploadcustomer(HttpServletRequest request,MultipartFile file)
    {
        System.out.println("-----------"+file);
        UserBean userBean = (UserBean) request.getAttribute("userBean");
        int i;
        Sheet sheet;
        Workbook book;
        Cell cell1,cell2;
        List<CustomerBean> agents = new ArrayList<CustomerBean>();
        try {
            CommonsMultipartFile cf = (CommonsMultipartFile) file;
            DiskFileItem fi = (DiskFileItem) cf.getFileItem();
            
            File f = fi.getStoreLocation();
            // t.xls为要读取的excel文件名
            book = Workbook.getWorkbook(f);

            // 获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
            sheet = book.getSheet(0);

            i = 1;
            FtpBean ftpBean =null;
            FtpServerBean ftpServerBean = null;
            CustomerBean customerBean = null;
            while (true) {
                // 获取每一行的单元格
                ftpServerBean = new FtpServerBean();
                customerBean = new CustomerBean();
                ftpBean = new FtpBean();
                cell1 = sheet.getCell(0, i);// (列,行)
                if ("".equals(cell1.getContents()) == true)
                    break;    // 如果读取的数据为空
                ftpServerBean.setServerName(cell1.getContents());
                cell2 = sheet.getCell(2,i);
                ftpServerBean.setServerIp(cell2.getContents());
                //serverid  begin    
                ftpServerBean = ftpServerService.getFtpServerByServerIP(ftpServerBean);
                if(ftpServerBean!=null && ftpServerBean.getId()>0)
                {
                    ftpBean.setServerId(ftpServerBean.getId());//存在
                }
                else
                {    
                    ftpServerBean = new FtpServerBean();
                    ftpServerBean.setServerName(PinYin.addPinYinHeadChar(cell1.getContents()));
                    ftpServerBean.setServerIp(cell2.getContents());
                    ftpServerBean.setCreateUser(userBean.getId());
                    int count =  ftpServerService.addFtpServer(ftpServerBean);
                    ftpBean.setServerId(ftpServerBean.getId());
                }
                //serverid  end    
                
                //customer
                    cell1 = sheet.getCell(1, i);//客户名
                    customerBean.setName(PinYin.addPinYinHeadChar(cell1.getContents()));
                    customerBean = customerService.selectCustomerByName(customerBean);
                    if (customerBean!=null && customerBean.getId()>0) {
                        ftpBean.setCustomerName(customerBean.getName());
                        ftpBean.setCustomerId(customerBean.getId());
                        ftpBean.setIpAddr(ftpServerBean.getServerIp());
                        ftpBean.setCreateUser(ftpServerBean.getCreateUser());
                    }
                    else {
                        customerBean = new CustomerBean();
                        customerBean.setName(PinYin.addPinYinHeadChar(cell1.getContents()));
                        customerBean.setAddUser(userBean.getId());
                        customerBean.setContacts("**");
                        customerBean.setContactTel("13156878391");
                        customerBean.setSigningTime(new Date());
                        customerBean.setCompletetime(new Date());
                        int count = customerService.insertCustomer(customerBean);
                        ftpBean.setCustomerId(customerBean.getId());
                        ftpBean.setCustomerName(PinYin.addPinYinHeadChar(cell1.getContents()));
                        ftpBean.setIpAddr(ftpServerBean.getServerIp());
                        ftpBean.setCreateUser(ftpServerBean.getCreateUser());
                    }
                    //customer
                        cell1 = sheet.getCell(3, i);
                        ftpBean.setUserName(cell1.getContents());//用户名
                        cell1 = sheet.getCell(4, i);
                        ftpBean.setPassword(cell1.getContents());//密码
                        cell1 = sheet.getCell(5, i);
                        ftpBean.setDomainName(cell1.getContents());//绑定域名
                        cell1 = sheet.getCell(6, i);
                        ftpBean.setDeveLanguage(cell1.getContents());//开发语言
                        cell1 = sheet.getCell(7, i);
                        ftpBean.setSpaceSize(cell1.getContents());//空间大小
                        try {
                            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                            cell1 = sheet.getCell(8, i);
                            Date date = dateFormat.parse(cell1.getContents().replace("/", "-"));
                            ftpBean.setBeginTime(date);//开始时间
                            
                        } catch (Exception e) {
                            System.out.println(e);
                            ftpBean.setBeginTime(new Date());//开始时间
                        }
                        try {
                            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                            cell1 = sheet.getCell(9, i);
                            Date date = dateFormat.parse(cell1.getContents().replace("/", "-"));
                            ftpBean.setEndTime(date);//到期时间
                            
                        } catch (Exception e) {
                            System.out.println(e);                            
                            ftpBean.setBeginTime(new Date(new Date().getTime()+365*24*3600));//开始时间
                        }
                        
                        cell1 = sheet.getCell(10, i);
                        ftpBean.setSqlType(cell1.getContents());//数据库
                        cell1 = sheet.getCell(11, i);
                        ftpBean.setSqlName(cell1.getContents());//数据库名
                        cell1 = sheet.getCell(12, i);
                        ftpBean.setSqlUserName(cell1.getContents());//用户名
                        cell1 = sheet.getCell(13, i);
                        ftpBean.setSqlPassword(cell1.getContents());//数据库密码
                        cell1 = sheet.getCell(14, i);
                        ftpBean.setInfo(cell1.getContents());//备注
                        ftpService.addFtp(ftpBean);
                        
                        i++;
                    }
            
            book.close();
        } catch (Exception e) {

            e.printStackTrace();
        }
    }
    
}

 

以上是关于jxl读取excel导入到数据库,报错java.lang.NoClassDefFoundError: jxl/read/biff/BiffException的主要内容,如果未能解决你的问题,请参考以下文章

java poi技术读取到数据库

JXL读写Excel表格数据

Java-jxl插件Excel文件读写报错jxl.read.biff.BiffException: Unable to recognize OLE stream

Java-jxl插件Excel文件读写报错jxl.read.biff.BiffException: Unable to recognize OLE stream

记录-java(jxl) Excel导入数据库

java用jxl读取网页上下载的excel文件,怎么识别?求解答