本文实例讲述了JSP上传excel及excel插入至数据库的方法。分享给大家供大家参考。具体如下:
此导入excel是与pojo绑定的,(缺点)excle表头必须是pojo的字段值
1. html页面:
2. java代码:
@RequestMapping("/excleImport")
public void excleImport(HttpServletRequest request) throws IOException, Exception {
request.setCharacterEncoding("utf-8"); //设置编码
//获得磁盘文件条目工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//获取文件需要上传到的路径
String path = request.getRealPath("/upload/kaku");
File uploadDir = new File(path);
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
factory.setRepository(uploadDir);
//设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室
factory.setSizeThreshold(1024*1024) ;
//高水平的API文件上传处理
ServletFileUpload upload = new ServletFileUpload(factory);
//可以上传多个文件
List list = (List)upload.parseRequest(request);
for(FileItem item : list)
{
//获取表单的属性名字
String name = item.getFieldName();
//如果获取的 表单信息是普通的 文本 信息
if(item.isFormField())
{
//获取用户具体输入的字符串 ,名字起得挺好,因为表单提交过来的是 字符串类型的
String value = item.getString() ;
request.setAttribute(name, value);
}
//对传入的非 简单的字符串进行处理 ,比如说二进制的 图片,电影这些
else
{
/**
* 以下三步,主要获取 上传文件的名字
*/
//获取路径名
String value = item.getName() ;
//索引到最后一个反斜杠
int start = value.lastIndexOf("\\");
//截取 上传文件的 字符串名字,加1是 去掉反斜杠,
String filename = value.substring(start+1);
//文件后缀名
String prefix = filename.substring(filename.lastIndexOf(".") + 1);
CardCenter cardCenter = new CardCenter();
request.setAttribute(name, filename);
//真正写到磁盘上
//它抛出的异常 用exception 捕捉
//item.write( new File(path,filename) );//第三方提供的
//手动写的
//OutputStream out = new FileOutputStream(new File(path,filename));
InputStream in = item.getInputStream() ;
List listFromExcel = (List)ExelUtil.exportListFromExcel(in, prefix, cardCenter);
this.cardCenterService.excleImport(listFromExcel);
/*int length = 0 ;
byte [] buf = new byte[1024] ;
System.out.println("获取上传文件的总共的容量:"+item.getSize());
// in.read(buf) 每次读到的数据存放在 buf 数组中
while( (length = in.read(buf) ) != -1)
{
//在 buf 数组中 取出数据 写到 (输出流)磁盘上
out.write(buf, 0, length);
} */
in.close();
//out.close();
}
}
}
3. java代码:
public class ExelUtil {
//第一列开始
private static int start = 0;
//最后一列序号
private static int end =0;
public static String getSubString(String str){
return str.substring(0,str.lastIndexOf("."));
}
/**
* 方法描述:由Excel文件的Sheet导出至List
* @param file
* @param sheetNum
* @return
* @throws IOException
* @author
* @date 2013-3-25 下午10:44:26
* @comment
*/
public static List> exportListFromExcel(File file, String fileFormat,Object dtoobj)
throws IOException {
return exportListFromExcel(new FileInputStream(file), fileFormat,dtoobj);
}
/**
* 方法描述:由Excel流的Sheet导出至List
* @param is
* @param extensionName
* @param sheetNum
* @return
* @throws IOException
* @author
* @date 2013-3-25 下午10:44:03
* @comment
*/
public static List> exportListFromExcel(InputStream is,String fileFormat,Object dtoobj) throws IOException {
Workbook workbook = null;
if (fileFormat.equals(BizConstant.XLS)) {
workbook = new HSSFWorkbook(is);
} else if (fileFormat.equals(BizConstant.XLSX)) {
workbook = new XSSFWorkbook(is);
}
return exportListFromExcel(workbook,dtoobj);
}
/**
* 方法描述:由指定的Sheet导出至List
* @param workbook
* @param sheetNum
* @return
* @author
* @date 2013-3-25 下午10:43:46
* @comment
*/
private static List
希望本文所述对大家的JSP程序设计有所帮助。