2010.03.25(3)——大数据导入excel 最终版
最后就是,限制一下文件的大小,禁止大于7M的文件
html
<form action="excel_up.do?className=st_river_r" enctype="multipart/form-data" method="post">
<select name="className">
<option value="Student">学生</option>
<option value="Teacher" selected="selected">老师</option>
</select>
<input type="file" name="f1"/>
<input type="submit" value="上载" />
</form>
servlet类
package servlet;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
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 util.Class_Table;
import util.JdbcUtil;
/**
* Excel文件最大不要超过7M
* @author
*
*/
public class ExcelServlet2 extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response){
String className = request.getParameter("className");
System.out.println(className);
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是7MB
fu.setSizeMax(7340032);
// 设置缓冲区大小,这里是5kb
fu.setSizeThreshold(5120);
// 设置临时目录:
fu.setRepositoryPath("C:\\upload\\tmp\\");
// 解决中文乱码
fu.setHeaderEncoding("utf-8");
//输出路径
String path = "c:/";
try {
List fileItems = fu.parseRequest(request);
System.out.println(fileItems.size());
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
System.out.println(item.getName());
System.out.println(item.getSize());
//以下为文件名处理,将上传的文件保存在项目所在目录下。
//获取文件名字符串的长度
int end = name.length();
//返回在此字符串中最右边出现的指定子字符串的索引。
int begin = name.lastIndexOf("\\");
// BufferedInputStream in = new BufferedInputStream(item.getInputStream());
// //文件存储在工程的upload目录下,这个目录也得存在
// BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(new File("c:/xx.txt")));
// Streams.copy(in, out, true);
path += name.substring(begin + 1, end);
item.write(new File(path));
}
}
//Class c = Class.forName(className);
//Field[] fields = c.getDeclaredFields();
System.out.println(path);
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(path));
int totalSheets = workbook.getNumberOfSheets();
int totalRows;
int totalCells;
//System.out.println(totalSheets);
HSSFSheet sheet = null;
HSSFRow row = null;
for(int i=0;i<totalSheets;i++){
sheet = workbook.getSheetAt(i);
totalRows = sheet.getLastRowNum();
if(totalRows==0) continue;
for(int j=0;j<=totalRows;j++){
row = sheet.getRow(j);
totalCells = row.getLastCellNum();
//System.out.println(totalCells);
List<Object> list = new ArrayList<Object>();
for(int k=0;k<totalCells;k++){
HSSFCell cell = row.getCell(k);
//System.out.println(cell.toString());
if(cell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
//System.out.println("number "+cell.getNumericCellValue());
list.add(new Double(cell.getNumericCellValue()).toString());
}else if(cell.getCellType()==HSSFCell.CELL_TYPE_STRING){
//System.out.println("String "+cell.getRichStringCellValue().getString());
list.add(cell.getRichStringCellValue().getString());
}
}
importToTable(className,list);
System.out.println("导入"+(j+1)+"条数据");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void importToTable(String className,List<Object> values) throws Exception{
Connection conn = JdbcUtil.getConnection();
Statement st = conn.createStatement();
String str = "";
int i = 1;
for(Object o:values){
if(o instanceof String){
if(o.toString().equals(" ")||o.toString().trim().equals("")){
str += null;
}else{
str += "'"+o+"'";
}
}else{
str += o;
}
if(i!=values.size()){
str += ",";
}
i++;
}
String sql = "insert into "+Class_Table.get(className)+" values("+str+")";
//System.out.println(sql);
st.executeUpdate(sql);
JdbcUtil.release(null, st, null);
}
}
jdbcUtil
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import java.io.*;
public class JdbcUtil {
private static Properties info=new Properties();
static{
try {
InputStream is=JdbcUtil.class.getClassLoader().getResourceAsStream("config.properties");
info.load(is);
is.close();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
private static final ThreadLocal<Connection> tl=new ThreadLocal<Connection>();
public static Connection getConnection() throws Exception{
Connection conn=tl.get();
if(conn==null){
Class.forName(info.getProperty("driver"));
conn=DriverManager.getConnection(info.getProperty("url"),
info.getProperty("username"),info.getProperty("password"));
tl.set(conn);
}
return conn;
}
public static void release(ResultSet rs,Statement stm,Connection conn){
if(rs!=null) try{ rs.close();} catch(Exception ee){}
if(stm!=null) try{ stm.close();} catch(Exception ee){}
if(conn!=null) try{ conn.close();} catch(Exception ee){}
}
public static void main(String[] args) throws Exception{
System.out.println(getConnection());
}
}
util.Class_Table
package util;
import java.io.InputStream;
import java.util.Properties;
public class Class_Table {
private static Properties info=new Properties();
static{
try {
InputStream is=JdbcUtil.class.getClassLoader().getResourceAsStream("class_table.properties");
info.load(is);
is.close();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static String get(String key){
return info.getProperty(key);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>excel_up</servlet-name>
<servlet-class>servlet.ExcelServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>excel_up</servlet-name>
<url-pattern>/excel_up.do</url-pattern>
</servlet-mapping>
</web-app>