如何用javajava 将csv导入数据库excel数据到数据库

java实现Excel导入数据库 -
- ITeye技术网站
博客分类:
这是一个比较简单的例子,做的操作是选择角色,把用户的ID导入用户角色表(如何获取角色信息,就没在贴出来了)。用到了Struts2、jdbc。
需要jlx.jar 等几个必须的包。
&%@ page language="java" pageEncoding="UTF-8"%&
&%@ taglib prefix="s" uri="/struts-tags" %&
&%@ taglib uri="http://www.ecside.org" prefix="ec"%&
&!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&
&title&excel&/title&
&%@include file="../inc/perm.inc"%&
&script type="text/javascript"&
function myFormCheck(theform)
if(theform.roleid.value==""){
alert("请选择对应的角色")
return (false);
if(theform.theFile.value=="")
alert("请点击浏览按钮,选择您要上传的文件!")
theform.theFile.
return (false);
str= theform.theFile.
strs=str.toLowerCase();
lens=strs.
extname=strs.substring(lens-4,lens);
if(extname!=".xls")
alert("请选择excel文件!")
return (false);
&div style="position: left: 30%; top: 20%"&
&div style="position: left: 30%; top: 30%"&
&form name="uploadform" action="&%=contextPath%&/features/excel.action" enctype="multipart/form-data" method=post onsubmit="return myFormCheck(this)"&
&SELECT name="roleid" id="select"&
&OPTION value="" &----请选择----&/OPTION&
&s:iterator value="allRole"&
&OPTION value="${roleId}"&${roleId}.${desc}&/OPTION&
&/s:iterator&
&input type="file" name="theFile"&
&input type="submit" value="导入"&
&action name="excel" class="com.skyarm.action.FeaturesAction" method="uplode"&
&result&/success.jsp&/result&
public class FeaturesAction extends ActionSupport {
private static final long serialVersionUID = -34341L;
// private static final int BUFFER_SIZE = 16 * 1024;
private File theF
public String uplode(){
int i = UplodeService.uplode(roleid,theFile);
return SUCCESS;
public String getRoleid() {
public void setRoleid(String roleid) {
this.roleid =
public File getTheFile() {
return theF
public void setTheFile(File theFile) {
this.theFile = theF
public class UplodeService {
public static void uplode(String id,File file){
int rows = 0; //表单行数
Sheet sh = //表单对象
InputStream inputStream =
Workbook book =
Connection conn =
PreparedStatement stmt =
inputStream = new FileInputStream(file);
book = Workbook.getWorkbook(inputStream);
sh = book.getSheet(0);
rows = sh.getRows();
conn = DataModule.getConnection();
String sql = "INSERT INTO auth_user_role (userid,roleid) VALUES (?,?)";
for(int i=0;i&i++){ // 程序只取第一列
userid = sh.getCell(0, i).getContents();
//判断userid的合法性
if(5&userid.length() || 8&userid.length())
stmt = conn.prepareStatement(sql);
stmt.setString(1, userid);
stmt.setString(2, id);
stmt.executeUpdate();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
if(inputStream!=null)
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(book!=null)
book.close();
DataModule.close(stmt);
DataModule.close(conn);
浏览: 17346 次
来自: 广州
可以的,只用mail.jarJavaForExcel 用 实现将 导入到数据库中 Jsp/Servlet 242万源代码下载-
&文件名称: JavaForExcel& & [
& & & & &&]
&&所属分类:
&&开发工具: Java
&&文件大小: 1327 KB
&&上传时间:
&&下载次数: 0
&&提 供 者:
&详细说明:用JAVA实现将excel导入到数据库中-Using JAVA to excel into the
文件列表(点击判断是否您需要的文件,如果是垃圾请在下面评价投诉):
&&JavaForExcel\.classpath&&............\.project&&............\.settings\org.eclipse.jdt.core.prefs&&............\.........\org.eclipse.jdt.ui.prefs&&............\bin\com\javen\db\DBhepler.class&&............\...\...\.....\entity\StuEntity.class&&............\...\...\.....\.xcel\TestDbToExcel.class&&............\...\...\.....\.....\TestExcel.class&&............\...\...\.....\.....\TestExcelToDb.class&&............\...\...\.....\service\StuService.class&&............\javenforexcel.sql&&............\lib\jxl.jar&&............\...\mysql-connector-java-5[1].1.7-bin.jar&&............\src\com\javen\db\DBhepler.java&&............\...\...\.....\entity\StuEntity.java&&............\...\...\.....\.xcel\TestDbToExcel.java&&............\...\...\.....\.....\TestExcel.java&&............\...\...\.....\.....\TestExcelToDb.java&&............\...\...\.....\service\StuService.java&&............\bin\com\javen\db&&............\...\...\.....\entity&&............\...\...\.....\excel&&............\...\...\.....\service&&............\src\com\javen\db&&............\...\...\.....\entity&&............\...\...\.....\excel&&............\...\...\.....\service&&............\bin\com\javen&&............\src\com\javen&&............\bin\com&&............\src\com&&............\.settings&&............\bin&&............\lib&&............\src&&JavaForExcel
&输入关键字,在本站242万海量源码库中尽情搜索:

我要回帖

更多关于 map导入数据库 java 的文章

 

随机推荐