jxl 导入excel以及日期格式处理

时间:2023-03-10 01:17:40
jxl 导入excel以及日期格式处理

先建一个excel文件abc.xls.放到E盘根目录下。形如下:

name secondName
hot1 leave1
hot2 leave2

然后在数据库里建表。

CREATE TABLE `name` (
`name` varchar(30) default NULL,
`secondname` varchar(20) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

/*
* Excel2Mysql.java
*
* Created on 2006年9月25日, 下午6:48
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package excel;

/**
*
* @author hotleave
*/
import java.io.*;
import jxl.*;
import java.sql.*;

public class Excel2Mysql {
    
    public static Connection conn=null;
    public static Statement stmt=null;
    /** Creates a new instance of Excel2Mysql */
    public Excel2Mysql() {
        try{
            Class.forName("org.gjt.mm.mysql.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=123456&characterEncoding=UTF-8");
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    
    public static void main(String args[]){
        
        try{
            Class.forName("org.gjt.mm.mysql.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=user&password=pwd&characterEncoding=UTF-8");
            stmt=conn.createStatement();
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        
        try{
            InputStream   is   =   new   FileInputStream("e:/abc.xls");
            Workbook book=Workbook.getWorkbook(is);//获得一个工作表对象
            Sheet sheet=book.getSheet(0);//取得第一个工作表,也可用sheet名字获得。
            int len=sheet.getRows();//取得行数
            String sql="";
            System.out.println("len is:"+len);
            Cell[] cells=null;
            System.out.println("读出来的结果为:");
            for(int i=1;i<len;i++){//从1开始,避免插入标题
                //System.out.println(i);

Cell [] cell = sheet.getRow(i);
                if(cell.length == 0) { //判断这行是不是空行,是空行的话就跳过
                continue;
                }
                cells=null;
                cells=sheet.getRow(i);

sql="insert into name values ('"+cells[0].getContents()+"','" +
                     cells[1].getContents()+"')";
                    stmt.execute(sql);
                    System.out.println("ok");
                //System.out.println("cells len is:"+cells.length);
                for(int j=0;j<cells.length;j++){//打印每行信息
                    System.out.print(cells[j].getContents()+" ");
                }
                System.out.println("");
            }
            book.close(); 
        }catch(Exception e){
            System.out.println(e); 
        }
    }
}

缺点:jxl对office2007不支持

判断是否是日期类型,然后用Date获取并重新格式化 保存为字符串即可

Cell cell = sheet0.getCell(col,row);

String cellcon="";
if(cell.getType() == CellType.DATE){
DateCell dc = (DateCell)cell;
Date date = dc.getDate();
SimpleDateFormat ds = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
  cellcon = ds.format(date);
}