求救,如果用poi方法把excel文件导入到数据库中!十分感谢!!!! http://expert.csdn.net/Expert/topic/2892/2892016.xml?temp=.6403009去那儿留下邮件地址吧! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的邮件地址是:[email protected]留个联系方式,请你吃饭 这是读数据库数据到execl,你大概看一下吧<%@page contentType="text/html;charset=gb2312" errorPage="error.jsp"%><%@page import="org.apache.poi.hssf.usermodel.*" import="org.apache.poi.hssf.usermodel.HSSFDataFormat" import="org.apache.poi.poifs.filesystem.POIFSFileSystem" import="java.io.*" import="com.jspsmart.upload.*" import="java.sql.*" import="java.util.*" import="java.sql.Date" import="java.text.DateFormat" import="edu.*"%><%request.setCharacterEncoding("gb2312");Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Connection conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://10.0.0.176:1433;DataBaseName=student","zdsystem","5938288_5800142");%><%! public static String getCellValue(HSSFCell cell) { HSSFCellStyle cellStyle = cell.getCellStyle(); String msg = null; switch(cell.getCellType()) { case (HSSFCell.CELL_TYPE_STRING): msg = cell.getStringCellValue(); break; case (HSSFCell.CELL_TYPE_NUMERIC): msg = String.valueOf(cell.getNumericCellValue()); break; case (HSSFCell.CELL_TYPE_FORMULA): msg = cell.getCellFormula(); break; case (HSSFCell.CELL_TYPE_BLANK): break; default: break; } return msg; }%><% StringBuffer message = new StringBuffer(); String fileName = null; String filetype=request.getParameter("type"); POIFSFileSystem fs = null; HSSFWorkbook wb = null; int inputCount = 0; SmartUpload mySmartUpload = new SmartUpload(); mySmartUpload.initialize(config,request,response); mySmartUpload.upload(); com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(0); // 测试中文文件名 fileName = file.getFileName(); // 在临时文件夹中保存为一个临时文件 fileName = java.io.File.createTempFile(fileName,null).getAbsolutePath(); file.saveAs(fileName); fs = new POIFSFileSystem(new FileInputStream(fileName)); wb = new HSSFWorkbook(fs); int sheetCount = wb.getNumberOfSheets(); //System.out.print("sheetCount"+sheetCount); for(int x=0;x<sheetCount;x++) { //System.out.print("sheetCount"+sheetCount); HSSFSheet sheet = wb.getSheetAt(x); int rowCount = sheet.getPhysicalNumberOfRows(); //System.out.print("rowCount"+rowCount); String sqlprofessional = null; String sqlvocation=null; HSSFRow row=null; String school=null; //----得到第一行,即表头的字符串 if(filetype.equals("1")){ row=sheet.getRow(1); short t=13; school=row.getCell(t).getStringCellValue(); } // sqlvocation ="insert vocation (studentID,name,sex,schoolname,birthday,address,examID,polity,chinese,math,english,physics,chymistry,history,sport,additament,total,re) values(?,?,?,'"+school+"',?,?,?,?,?,?,?,?,?,?,?,?,?,?)";// java.sql.PreparedStatement pstmt=conn.prepareStatement(sqlvocation); sqlprofessional="insert professional (city,SN,name,sex,birthday,member,folk,joinTime,joinTimes,occupation,level,finishSchool,workUnit,grade,cause,re) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";// java.sql.PreparedStatement pstmt1=conn.prepareStatement(sqlprofessional); for(int y=3;y<rowCount;y++) { row = sheet.getRow(y); if(filetype.equals("1")){ short a=0; //String short b=1; //String short c=2; //String short d=3; //String short e=4; //String short f=5; //String short g=6; //int short h=7; //int short i=8; //int short j=9; //int short k=10; //int short l=11; //int short m=12; //int short n=13; //int short o=14; //int short p=15; //int short q=16; //String; pstmt.setString(1, row.getCell(a).getStringCellValue()); pstmt.setString(2, row.getCell(b).getStringCellValue()); pstmt.setString(3, row.getCell(c).getStringCellValue()); pstmt.setString(4, row.getCell(d).getStringCellValue()); pstmt.setString(5, row.getCell(e).getStringCellValue()); pstmt.setString(6, row.getCell(f).getStringCellValue()); pstmt.setDouble(7, row.getCell(g).getNumericCellValue()); pstmt.setDouble(8, row.getCell(h).getNumericCellValue()); pstmt.setDouble(9, row.getCell(i).getNumericCellValue()); pstmt.setDouble(10, row.getCell(j).getNumericCellValue()); pstmt.setDouble(11, row.getCell(k).getNumericCellValue()); pstmt.setDouble(12, row.getCell(l).getNumericCellValue()); pstmt.setDouble(13, row.getCell(m).getNumericCellValue()); pstmt.setDouble(14, row.getCell(n).getNumericCellValue()); pstmt.setDouble(15, row.getCell(o).getNumericCellValue()); pstmt.setDouble(16, row.getCell(p).getNumericCellValue()); pstmt.setString(17, row.getCell(q).getStringCellValue()); pstmt.execute(); inputCount++; } if(filetype.equals("2")){ short a=0; //String short b=1; //String short c=2; //String// short d=3; //String short e=4; //String short f=5; //String short g=6; //String short h=7; //String short i=8; //String short j=9; //String short k=10; //String short l=11; //String short m=12; //String short n=13; //String short o=14; //String short p=15; //String pstmt1.setString(1, row.getCell(a).getStringCellValue()); pstmt1.setString(2, row.getCell(b).getStringCellValue()); pstmt1.setString(3, row.getCell(c).getStringCellValue()); pstmt1.setString(4, row.getCell(d).getStringCellValue()); pstmt1.setString(5, row.getCell(e).getStringCellValue()); pstmt1.setString(6, row.getCell(f).getStringCellValue()); pstmt1.setString(7, row.getCell(g).getStringCellValue()); pstmt1.setString(8, row.getCell(h).getStringCellValue()); pstmt1.setString(9, row.getCell(i).getStringCellValue()); pstmt1.setString(10, row.getCell(j).getStringCellValue()); pstmt1.setString(11, row.getCell(k).getStringCellValue()); pstmt1.setString(12, row.getCell(l).getStringCellValue()); pstmt1.setString(13, row.getCell(m).getStringCellValue()); pstmt1.setString(14, row.getCell(n).getStringCellValue()); pstmt1.setString(15, row.getCell(o).getStringCellValue()); pstmt1.setString(16, row.getCell(p).getStringCellValue()); pstmt1.execute(); inputCount++; } }//里层for循环 if(filetype.equals("1")){ pstmt.close(); } pstmt1.close(); }//最外层for循环 %><br/><script language="javascript"> alert("完毕:共导入 <%=inputCount%> 条数据。");</script><%conn.close();%> 我需要的是从EXCEL导入到数据库中!~你的方法我会考虑的,谢谢,有没有我需要的呢???? poi方法,我看了这些天也差不多了,就是在取单元格的类型,为空的时候,总是出错,可是我从excel中取数据时,空是不可避免的! 弄个ODBC的接口,就可以直接弄进去了。 Java开发文件服务器的一些想法,需要你的建议! web项目发布在tomcat后,localhost登陆和IP地址登陆的后,JavaScript有差异 mysql怎样将两个字符型时间用sum统计 这样的语句对吗? 这个想法,java不知能否实现 一个使用fop转换数据的问题 没钱也来求救!!!! session.putValue有什么作用啊? 小第写了个if语句,但总是通不过,谁帮着看看啊。 请问在一个servlet里取得一个用singleton模式实现的类实例,那么这个类实例的生命周期是怎样的? java中使用0x493e0L等是什么意思啊? 关于 文本的 读取和分析
留个联系方式,请你吃饭
<%@page import="org.apache.poi.hssf.usermodel.*"
import="org.apache.poi.hssf.usermodel.HSSFDataFormat"
import="org.apache.poi.poifs.filesystem.POIFSFileSystem"
import="java.io.*"
import="com.jspsmart.upload.*"
import="java.sql.*"
import="java.util.*"
import="java.sql.Date"
import="java.text.DateFormat"
import="edu.*"%>
<%
request.setCharacterEncoding("gb2312");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://10.0.0.176:1433;DataBaseName=student","zdsystem","5938288_5800142");
%>
<%!
public static String getCellValue(HSSFCell cell)
{
HSSFCellStyle cellStyle = cell.getCellStyle();
String msg = null;
switch(cell.getCellType())
{
case (HSSFCell.CELL_TYPE_STRING):
msg = cell.getStringCellValue();
break;
case (HSSFCell.CELL_TYPE_NUMERIC):
msg = String.valueOf(cell.getNumericCellValue());
break;
case (HSSFCell.CELL_TYPE_FORMULA):
msg = cell.getCellFormula();
break;
case (HSSFCell.CELL_TYPE_BLANK):
break;
default: break;
}
return msg;
}
%>
<%
StringBuffer message = new StringBuffer();
String fileName = null;
String filetype=request.getParameter("type");
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
int inputCount = 0; SmartUpload mySmartUpload = new SmartUpload();
mySmartUpload.initialize(config,request,response); mySmartUpload.upload(); com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(0);
// 测试中文文件名
fileName = file.getFileName();
// 在临时文件夹中保存为一个临时文件
fileName = java.io.File.createTempFile(fileName,null).getAbsolutePath();
file.saveAs(fileName);
fs = new POIFSFileSystem(new FileInputStream(fileName));
wb = new HSSFWorkbook(fs);
int sheetCount = wb.getNumberOfSheets();
//System.out.print("sheetCount"+sheetCount);
for(int x=0;x<sheetCount;x++)
{
//System.out.print("sheetCount"+sheetCount);
HSSFSheet sheet = wb.getSheetAt(x);
int rowCount = sheet.getPhysicalNumberOfRows();
//System.out.print("rowCount"+rowCount);
String sqlprofessional = null;
String sqlvocation=null;
HSSFRow row=null;
String school=null;
//----得到第一行,即表头的字符串
if(filetype.equals("1")){
row=sheet.getRow(1);
short t=13;
school=row.getCell(t).getStringCellValue();
}
// sqlvocation ="insert vocation (studentID,name,sex,schoolname,birthday,address,examID,polity,chinese,math,english,physics,chymistry,history,sport,additament,total,re) values(?,?,?,'"+school+"',?,?,?,?,?,?,?,?,?,?,?,?,?,?)";// java.sql.PreparedStatement pstmt=conn.prepareStatement(sqlvocation);
sqlprofessional="insert professional (city,SN,name,sex,birthday,member,folk,joinTime,joinTimes,occupation,level,finishSchool,workUnit,grade,cause,re) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";// java.sql.PreparedStatement pstmt1=conn.prepareStatement(sqlprofessional);
for(int y=3;y<rowCount;y++)
{
row = sheet.getRow(y);
if(filetype.equals("1")){
short a=0; //String
short b=1; //String
short c=2; //String
short d=3; //String
short e=4; //String
short f=5; //String
short g=6; //int
short h=7; //int
short i=8; //int
short j=9; //int
short k=10; //int
short l=11; //int
short m=12; //int
short n=13; //int
short o=14; //int
short p=15; //int
short q=16; //String;
pstmt.setString(1, row.getCell(a).getStringCellValue());
pstmt.setString(2, row.getCell(b).getStringCellValue());
pstmt.setString(3, row.getCell(c).getStringCellValue());
pstmt.setString(4, row.getCell(d).getStringCellValue());
pstmt.setString(5, row.getCell(e).getStringCellValue());
pstmt.setString(6, row.getCell(f).getStringCellValue());
pstmt.setDouble(7, row.getCell(g).getNumericCellValue());
pstmt.setDouble(8, row.getCell(h).getNumericCellValue());
pstmt.setDouble(9, row.getCell(i).getNumericCellValue());
pstmt.setDouble(10, row.getCell(j).getNumericCellValue());
pstmt.setDouble(11, row.getCell(k).getNumericCellValue());
pstmt.setDouble(12, row.getCell(l).getNumericCellValue());
pstmt.setDouble(13, row.getCell(m).getNumericCellValue());
pstmt.setDouble(14, row.getCell(n).getNumericCellValue());
pstmt.setDouble(15, row.getCell(o).getNumericCellValue());
pstmt.setDouble(16, row.getCell(p).getNumericCellValue());
pstmt.setString(17, row.getCell(q).getStringCellValue());
pstmt.execute();
inputCount++;
}
if(filetype.equals("2")){
short a=0; //String
short b=1; //String
short c=2; //String//
short d=3; //String
short e=4; //String
short f=5; //String
short g=6; //String
short h=7; //String
short i=8; //String
short j=9; //String
short k=10; //String
short l=11; //String
short m=12; //String
short n=13; //String
short o=14; //String
short p=15; //String
pstmt1.setString(1, row.getCell(a).getStringCellValue());
pstmt1.setString(2, row.getCell(b).getStringCellValue());
pstmt1.setString(3, row.getCell(c).getStringCellValue());
pstmt1.setString(4, row.getCell(d).getStringCellValue());
pstmt1.setString(5, row.getCell(e).getStringCellValue());
pstmt1.setString(6, row.getCell(f).getStringCellValue());
pstmt1.setString(7, row.getCell(g).getStringCellValue());
pstmt1.setString(8, row.getCell(h).getStringCellValue());
pstmt1.setString(9, row.getCell(i).getStringCellValue());
pstmt1.setString(10, row.getCell(j).getStringCellValue());
pstmt1.setString(11, row.getCell(k).getStringCellValue());
pstmt1.setString(12, row.getCell(l).getStringCellValue());
pstmt1.setString(13, row.getCell(m).getStringCellValue());
pstmt1.setString(14, row.getCell(n).getStringCellValue());
pstmt1.setString(15, row.getCell(o).getStringCellValue());
pstmt1.setString(16, row.getCell(p).getStringCellValue());
pstmt1.execute();
inputCount++;
}
}//里层for循环 if(filetype.equals("1")){
pstmt.close();
}
pstmt1.close();
}//最外层for循环
%>
<br/>
<script language="javascript"> alert("完毕:共导入 <%=inputCount%> 条数据。");</script>
<%
conn.close();
%>