当前位置: 代码迷 >> Java Web开发 >> 怎么在页面中实现excel文件导入到数据库中
  详细解决方案

怎么在页面中实现excel文件导入到数据库中

热度:2190   发布时间:2013-02-25 21:22:13.0
如何在页面中实现excel文件导入到数据库中
我有一个jsp页面如下,当我选中要导入的excel文件时,点击导入按钮要实现将这个excel文件中的数据信息导入到后台对应的一个数据表中去,怎么去实现呢?好像jxl可以实现,不知道怎么去搞,各位帮忙了,上面催着完成哦 
HTML code
<html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'TestExcel.jsp' starting page</title>        <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>    <body>    <input type="file" name="inportFile" >    <input type="button" value="导入">  </body></html>




------解决方案--------------------------------------------------------
www.java2000.net 去搜索 jxl

你要做的分2部分
1 上传
2 用jxl解析,然后把数据保存到数据库
------解决方案--------------------------------------------------------
http://download.csdn.net/source/1532774
这上面有完整的例子
------解决方案--------------------------------------------------------
上传以后用 jxl 去解析,可以直接读取一列,非常方便的,去搜一下吧


------解决方案--------------------------------------------------------
java poi 我用过 jxl 和poi 好像都行吧
------解决方案--------------------------------------------------------
你也可以使用poi实现对excel的操作,给你一个,我之前做的代码:
package com.excel;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.Iterator;

import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor.WHITE;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.DateUtil;

import com.sun.org.apache.xerces.internal.impl.xs.identity.ValueStore;

public class ExcelToDB {
private static Connection conn = null;
private static Statement stmt = null;

private static boolean getConnectionToMysql() {
try {
String url = "jdbc:mysql://localhost:3306/teacher?user=root&password=wafdqq00";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
} catch (ClassNotFoundException e) {
// TODO: handle exception
System.out.println("驱动加载失败!");
e.printStackTrace();
return false;
} catch (SQLException e) {
// TODO: handle exception
System.out.println("数据库连接失败!");
e.printStackTrace();
System.exit(1);
return false;
}
return true;
}

private static String getCellValue(HSSFCell cell) {
String value = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().toString() + "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue() + "";
  相关解决方案