java 导入excel数据demo(二)

2014-11-24 09:19:35 · 作者: · 浏览: 1
flag);
dispatcher.forward(req,resp);
}

/** 转化EXCEL文件并存放到retList中
*根据excel数据和数据格式的不同,以下代码的实现方式也会略有不同
* @param request
* @return retList
* @throws FileUploadException
* @throws FileUploadException
* @throws IOException
* @throws IOException
* @throws BiffException
* @throws BiffException
*/
private List parseData(HttpServletRequest req,HttpServletResponse resp) throws FileUploadException, IOException, BiffException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
List resultList = new ArrayList();
//默认路径
String uploadTo = "D:\\";
// 支持的文件类型
String[] errorType = { ".xls" };
//格式化日期
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//缓冲区域
// File tempPathFile;
if (FileUpload.isMultipartContent(req)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置缓冲区大小,这里是4kb
factory.setSizeThreshold(4096);
// 设置缓冲区目录
// factory.setRepository(tempPathFile);
//设置最大文件尺寸,这里是4MB
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(4*1024*1024);
// 开始读取上传信息
//开始读取上传信息
List fileItems = new ArrayList();
try {
fileItems = upload.parseRequest(req);
} catch (FileUploadException e1) {
// e1.printStackTrace();
logger.error(e1.getMessage());
}
//依次处理每个上传的文件
Iterator iter = fileItems.iterator();
//System.out.println("fileItems的大小是" + fileItems.size());
logger.info("fileItems的大小是" + fileItems.size());
//正则匹配,过滤路径取文件名
String regExp = ".+\\\\(.+)$";
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()){
FileItem item = (FileItem) iter.next();

//忽略其他不是文件域的所有表单信息
//System.out.println("正在处理" + item.getFieldName());
logger.info("正在处理" + item.getFieldName());
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if ((name == null || name.equals("")) && size == 0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result) {
boolean flag = false;
for (int temp = 0; temp < errorType.length; temp++) {
if(m.group(1).endsWith(errorType[temp])) {
flag = true;
}
}
if(!flag) {
//System.out.println("上传了不支持的文件类型");
logger.error("上传了不支持的文件类型");
throw new IOException(name + ": wrong type");
}
try {
String fileName = uploadTo + format.format(new Date()) + m.group(1).substring(m.group(1).indexOf("."));
item.write(new File(fileName));
//打开文件
Workbook book = Workbook.getWorkbook(new File(fileName)) ;
//取得第一个sheet
Sheet sheet = book.getSheet(0);
//取得行数
int rows = sheet.getRows();
Map param=new HashMap();
param.put("aliveFlag",Constants.REC_STATUS_USE);
//查出数据库中所有的中文电码表数据
List listdata=GROUPAgentFactory.getTelegraphcodeManager().queryList(param);
Map mapdata=new HashMap();
if(listdata!=null&&listdata.size()>0){
for (Telegraphcode telegraphcode : listdata) {
mapdata.put(telegraphcode.getTelegraphcode(),telegraphcode.getTelegraph());
}
}

for(int i = 1; i < rows; i++) {
Cell [] cell = sheet.getRow(i);
for(int j=0; j Map dataMap=new HashMap();
String str=sheet.getCell(j, i).getContents();
//getCell(列,行)
//System.out.println("******Cell data=="+sheet.getCell(j, i).getContents());
logger.info("******Cell data=="+sheet.getCell(j, i).getContents());
if(str!=null&&!str.equalsIgnoreCase("")){
str=str.trim();
String telegraph=str.substring(0,1);
String telegraphc