Jxl将Excel中的数据写入数据库--->现成code

来源:互联网 发布:raphael js api 编辑:程序博客网 时间:2024/05/20 02:28

Jxl将Excel中的数据写入数据库

 

今天做了一个将Excel中的数据读出,再写入到数据库指定的表中去,在网上查看了一下资料,通过Jxl完成了此项功能,但是程序可能还有问题,只能对Excel与数据表的结构相同的情况才能写入,否则就会报异常

 

PS:方法也是随便写的,并不能保证一定所有的特殊情况下都能运行,比如当Excel在第一行是个表格头的时候,读取Excel文件的时间就没去过滤这一行,当要写入数据库的时候就可能会出现数据类型不匹配而报错,如果有时间的朋友可以帮着修改一下,最好能将代码也发我一份,我的邮箱是:kyo153@163.com

 

1.读入Excel

Java代码    收藏代码
  1. package com.yw.core;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.LinkedHashMap;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import jxl.BooleanCell;  
  11. import jxl.Cell;  
  12. import jxl.CellType;  
  13. import jxl.DateCell;  
  14. import jxl.LabelCell;  
  15. import jxl.NumberCell;  
  16. import jxl.Sheet;  
  17. import jxl.Workbook;  
  18. import jxl.read.biff.BiffException;  
  19.   
  20. public class ExcelRead {  
  21.     public Map<Integer,List<Object>> readExcel(String pathName,int sheetId) throws BiffException, IOException{  
  22.         Workbook book = Workbook.getWorkbook(new File(pathName));  
  23.         Sheet sheet = book.getSheet(sheetId);  
  24.         Map<Integer,List<Object>> map = new LinkedHashMap<Integer, List<Object>>();  
  25.         int rows = sheet.getRows();  
  26.         int columns = sheet.getColumns();  
  27.           
  28.         for (int i = ; i < rows; i++) {  
  29.             List<Object> list = new ArrayList<Object>();  
  30.             for (int j = ; j < columns; j++) {  
  31.                 Cell cell = sheet.getCell(j, i);                  
  32.                 list.add(getExcelDate(cell));  
  33.             }  
  34.             map.put(i, list);  
  35.         }  
  36.         System.out.println(map);  
  37.         for (Object object : map.values()) {  
  38.             System.out.println(object);  
  39.         }  
  40.         book.close();  
  41.         return map;  
  42.     }  
  43.       
  44.     public Object getExcelDate(Cell cell){  
  45.         if(cell.getType()==CellType.NUMBER){  
  46.             NumberCell number = (NumberCell) cell;  
  47.             return number.getValue();  
  48.         }else if (cell.getType() == CellType.LABEL){  
  49.             LabelCell label = (LabelCell) cell;  
  50.             return label.getString();  
  51.         }else if (cell.getType() == CellType.BOOLEAN){  
  52.             BooleanCell bool = (BooleanCell) cell;  
  53.             return bool.getValue();  
  54.         }else if (cell.getType() == CellType.DATE){  
  55.             DateCell d = (DateCell) cell;  
  56.             return d.getDate();  
  57.         }else{  
  58.             return cell.getContents();  
  59.         }  
  60.     }  
  61.       
  62.     public static void main(String[] args) throws BiffException, IOException {  
  63.         ExcelRead read = new ExcelRead();  
  64.         read.readExcel("book1.xls");  
  65.           
  66.     }  
  67. }  

 

2.写入数据库,这里只有二个方法,一个是Insert与Query,本身这里也只是做了个通用的方法,没有想过特别的情况

如果有数据不对,或是为空是否出现问题也没有去进行Check,以后有时间再修改吧

 

Java代码    收藏代码
  1. package com.yw.core;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.sql.Connection;  
  6. import java.sql.DriverManager;  
  7. import java.sql.PreparedStatement;  
  8. import java.sql.ResultSet;  
  9. import java.sql.ResultSetMetaData;  
  10. import java.sql.SQLException;  
  11. import java.util.ArrayList;  
  12. import java.util.LinkedHashMap;  
  13. import java.util.List;  
  14. import java.util.Map;  
  15.   
  16. public class DBManager {  
  17.     private Connection conn = null;  
  18.     private Config config = new Config();  
  19.   
  20.     public void getConnection(File file) throws ClassNotFoundException, IOException,  
  21.             SQLException {  
  22.         config.reloadConfig(file);  
  23.         System.out.println(config.toString());  
  24.         Class.forName(config.getClassname());  
  25.         if (conn == null || conn.isClosed()) {  
  26.             conn = DriverManager.getConnection(config.getUrl(), config  
  27.                     .getUsername(), config.getPassword());  
  28.         }  
  29.     }  
  30.   
  31.     public void close() throws SQLException {  
  32.         conn.close();  
  33.     }  
  34.   
  35.     public boolean insert(String tableName, Object... obj) throws SQLException {  
  36.         int length = obj.length;  
  37.         PreparedStatement ps = null;  
  38.         StringBuilder builder = new StringBuilder();  
  39.         for (int i = ; i < length; i++) {  
  40.             builder.append("?,");  
  41.         }  
  42.         builder.delete(builder.length()-1, builder.length());  
  43.         String sql = "insert into " + tableName + " values("+builder.toString()+")";  
  44.         System.out.println(sql);  
  45.           
  46.         try {  
  47.             ps = conn.prepareStatement(sql);  
  48.             for (int i = ; i < obj.length; i++) {  
  49.                 ps.setObject(i+1, obj[i]);  
  50.             }  
  51.             if(ps.executeUpdate()>){  
  52.                 return true;  
  53.             }  
  54.         } catch (Exception e) {  
  55.             e.printStackTrace();  
  56.         }finally{  
  57.             ps.close();  
  58.         }  
  59.         return false;  
  60.     }  
  61.       
  62.     public Map<Integer,List<Object>> query(String tableName) throws SQLException{  
  63.           
  64.         Map<Integer, List<Object>> map = null;  
  65.         PreparedStatement ps = null;  
  66.         ResultSet rs = null;  
  67.         ResultSetMetaData metaData = null;  
  68.         try {  
  69.             map = new LinkedHashMap<Integer, List<Object>>();  
  70.             String sql = "select * from " + tableName;  
  71.             ps = conn.prepareStatement(sql);  
  72.             rs = ps.executeQuery();  
  73.             metaData = rs.getMetaData();  
  74.             int columnCount = metaData.getColumnCount();  
  75.             int count = ;  
  76.             while(rs.next()){  
  77.                 List<Object> list = new ArrayList<Object>();  
  78.                 for (int j = 1; j <= columnCount; j++) {  
  79.                     list.add(rs.getObject(j));  
  80.                 }  
  81.                 map.put(count, list);  
  82.                 count++;  
  83.             }  
  84.         } catch (Exception e) {  
  85.             e.printStackTrace();  
  86.         }finally{  
  87.             rs.close();  
  88.             ps.close();  
  89.         }  
  90.         return map;  
  91.     }  
  92.       
  93. }  
 

3.写了一个Config的方法,读取Properties文件的类,用来实时的加载DB的配置信息

 

Java代码    收藏代码
  1. package com.yw.core;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.util.Properties;  
  7.   
  8. public class Config {  
  9.     private String username;  
  10.     private String password;  
  11.     private String classname;  
  12.     private String url;  
  13.   
  14.     public String getUsername() {  
  15.         return username;  
  16.     }  
  17.   
  18.     public String getPassword() {  
  19.         return password;  
  20.     }  
  21.   
  22.     public String getClassname() {  
  23.         return classname;  
  24.     }  
  25.   
  26.     public String getUrl() {  
  27.         return url;  
  28.     }  
  29.   
  30.     public void reloadConfig(File file) throws IOException{  
  31.         FileInputStream in = null;  
  32.         if(file == null){  
  33.             in = new FileInputStream(new File("config.properties"));  
  34.         }else{  
  35.             in = new FileInputStream(file);  
  36.         }  
  37.           
  38.         Properties prop = new Properties();  
  39.         prop.load(in);  
  40.         username = prop.getProperty("username");  
  41.         password = prop.getProperty("password");  
  42.         classname = prop.getProperty("classname");  
  43.         url = prop.getProperty("url");  
  44.                 in.close();  
  45.     }  
  46.       
  47.     public String toString(){  
  48.         return username + "\n" + password + "\n" + classname + "\n" + url;  
  49.     }  




PS:

使用jxl读取excel,首先需要把jxl.jar导入工程中:

1:首先需要构建workbook

2:然后读取sheet

3:最后读取单元个cell或者读取row

代码如下:

1Workbook wb = Workbook.getWorkbook(new File("E:/接口.xls"));
2Sheet sheet = wb.getSheet(0);
3for (int i = 0; i < sheet.getRows(); i++) {
4   Cell[] cell = sheet.getRow(i);
5   for (int j = 0; j < cell.length; j++) {
6    System.out.println("cell["+j+"]:" + cell[j].getContents());
7   }
8}
就是这么简单,但是有一点需要注意的是,目前还不支持2007版的excel,只支持2003的版本。

需要你可以直接把07的另存为03就可以读取了!


原创粉丝点击