上传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<FileItem> 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<String[]> 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<String[]> readExcel(File excelFile,int rowNum) throws BiffException,
IOException {
// 创建一个list 用来存储读取的内容
List<String[]> list = new ArrayList<String[]>();
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<String[]> list_s){
Map map = new HashMap();
CountryObj countryObj = null;
String msg = "";
List<CountryObj> countryList = new ArrayList<CountryObj>();
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值