上传模板
uploadify+java 可参考:https://blog.csdn.net/zz_cl/article/details/52742721
------------------------------------------------------------------------------------------------------
controller
//文件上传方法
@RequestMapping("/uploadfile")
public String upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,ModelMap model) throws Exception {
long a=System.currentTimeMillis();
String path = request.getSession().getServletContext().getRealPath("upload");//文件路径
System.out.println("文件路径:"+path);
String originalFilename = file.getOriginalFilename();
String type = file.getContentType();
File targetFile = new File(path,originalFilename );
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}else if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 获得上传文件的文件扩展名
String subname = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
System.out.println("文件的扩展名:"+subname);
try {
file.transferTo(targetFile);
String rootpath = path + File.separator + originalFilename;
List<List<String[]>> excellist = UploadFileUtil.readExcel(rootpath);
}
工具类
public class UploadFileUtil{
public static List<List<String[]>> readExcel(String path){
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
List<List<String[]>> list = null;
try {
//同时支持Excel 2003、2007
File excelFile = new File(path); //创建文件对象
FileInputStream is = new FileInputStream(excelFile); //文件流
Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的
int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量
//存储数据容器
list = new ArrayList<List<String[]>>();
//默认从第二个sheet开始遍历
for (int s=1;s<sheetCount;s++) {
Sheet sheet = workbook.getSheetAt(s);
List<String[]> stringList=new ArrayList<String[]>() ;//用来存储每个sheet数据的容器
if (sheet.getLastRowNum() <= 0) continue;//判断如果sheet为空则进行下一个循环
//遍历行
for(int r=1;r<22;r+=2){
//用来存储每行数据的容器
Row row = sheet.getRow(r);
int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
String[] model = new String[cellCount];//用来存储每行数据的容器
int cellType=0;//用来判断数据类型
String cellValue = ""; //用来存储数据
Cell cell=null;
//遍历列
for(int c=0;c<cellCount;c++){
cell = row.getCell(c);
if (cell!=null) {
cellType = cell.getCellType();
switch(cellType) {
case Cell.CELL_TYPE_STRING: //文本
cellValue = cell.getStringCellValue();
//model[c-1] = cellValue;
break;
case Cell.CELL_TYPE_NUMERIC: //数字、日期
if(DateUtil.isCellDateFormatted(cell)) {
cellValue = fmt.format(cell.getDateCellValue()); //日期型
//model[c-1] = cellValue;
}
else {
cellValue = String.valueOf(cell.getNumericCellValue()); //数字
//model[c-1] = cellValue;
}
break;
case Cell.CELL_TYPE_BOOLEAN: //布尔型
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_BLANK: //空白
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_ERROR: //错误
cellValue = "错误";
break;
case Cell.CELL_TYPE_FORMULA: //公式
cellValue = "错误";
break;
default:
cellValue = "错误";
}
}
model[c] = cellValue;
}
//model放入list容器中
stringList.add(model);
}
list.add(stringList);
}
is.close();
}catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
可能用到
1、单元格如果是number类型,得到的数据会是小数点一位的double数据,如果想改变,可用DecimalFormat
DecimalFormat df = new DecimalFormat("0.00");
df.format(cell.getNumericCellValue())