上传excel文件,解析excel文件

需要jxl.jar包
上传excel文件:

//导入包
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


//实现方法
StringBuilder sb = new StringBuilder();
String tempPath = request.getSession().getServletContext().getRealPath("/");
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 最大缓存
factory.setSizeThreshold(5 * 1024);
// 设置临时文件目录
factory.setRepository(new File(tempPath));
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
for (FileItem item : items) {
if (!item.isFormField()) {
Calendar CD = Calendar.getInstance();
StringBuffer path = new StringBuffer();
path.append(request.getSession().getServletContext().getRealPath("/f")).append("/");
path.append("upload/"+System.currentTimeMillis() + CD.get(Calendar.MILLISECOND)).append(".").append(getFileExt(item.getName()));
// 写入文件
File file = new File(path.toString());
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
item.write(file);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

List list = ExcelReaderUtil.readExcel(new File(path.toString()), 2);
Map map = ExcelReaderUtil.checkImport(list);
System.out.println(map);

}
}
} catch (Exception e) {
e.printStackTrace();
}



//获取文件后缀名
public static String getFileExt(String f) {
int i = f.lastIndexOf(".");
if (i != -1) {
return f.substring(i + 1).toLowerCase();
}
return "";
}



/**
* @param excelFile 读取文件对象
* @param rowNum 从第几行开始读,如果有一行表头则从第二行开始读
* @return
* @throws BiffException
* @throws IOException
*/
public static List readExcel(File excelFile,int rowNum) throws BiffException,
IOException {
// 创建一个list 用来存储读取的内容
List list = new ArrayList();
Workbook rwb = null;
Cell cell = null;
// 创建输入流
InputStream stream = new FileInputStream(excelFile);
// 获取Excel文件对象
rwb = Workbook.getWorkbook(stream);
// 获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
// 行数(表头的目录不需要,从1开始)
for (int i = rowNum-1; i < sheet.getRows(); i++) {
// 创建一个数组 用来存储每一列的值
String[] str = new String[sheet.getColumns()];
String temp = "";
// 列数
for (int j = 0; j < sheet.getColumns(); j++) {
// 获取第i行,第j列的值
cell = sheet.getCell(j, i);
str[j] =replaceBlank(cell.getContents());
temp = temp.concat(str[j]);
}
if(!StringUtils.isEmpty(temp)){
// 把刚获取的列存入list
list.add(str);
}

}
// 返回值集合
return list;
}

//功能描述: 去空格,回车符
public static String replaceBlank(String str) {
String dest = "";
if (str!=null&&!str.equals("")) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest.trim();
}

//制定导入实体类规则
public static Map checkImport(List list_s){
Map map = new HashMap();
CountryObj countryObj = null;
String msg = "";
List countryList = new ArrayList();
if(list_s!=null&&list_s.size()>0){
for (int i = 0; i < list_s.size(); i++) {
String[] arr = list_s.get(i);
String country = arr[1];
countryObj = new CountryObj();
countryObj.setName(country);
countryObj.setTitle(country);
countryObj.setCreateAt(new Date());
countryObj.setUpdateAt(countryObj.getCreateAt());
countryObj.setUpdateBy(countryObj.getCreateBy());
countryList.add(countryObj);
}
}else{
msg = "上传失败,您选择的EXCEL文件为空。";
}
map.put("countryList", countryList);
map.put("msg", msg);
return map;
}

你可能感兴趣的:(上传excel文件,解析excel文件)