前台代码:
function fileChange(){ $('#file_form').submit(); }后台代码:
@Controller
@RequestMapping("/whiteList")
public class WhiteListControll {
@RequestMapping("/uploadFile")
@ResponseBody
public String uploadFile(HttpServletRequest request) {
request.getParameter("fileHiden");//没拿到
// TODO
return ""
}
}
问题描述:
我在后台怎么样才能拿到前台过来的FILE?
并且遍历Excel的第一列,Insert到数据库中
String fields = "traceCode";
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
Iterator fileNames = multipartRequest.getFileNames();
if(fileNames.hasNext()) {
String name = (String) fileNames.next();
CommonsMultipartFile multipartFile = (CommonsMultipartFile) multipartRequest.getFile(name);
InputStream in = null;
String fileName=multipartFile.getFileItem().getName();
String [] s=fileName.split("\\.");
try {
List<Map> detailMaps = new ArrayList<Map>();
String[] field={"traceCode"};
if(s[1].equals("xls")){
HSSFWorkbook hwb = new HSSFWorkbook(in);
detailMaps=ExcelUtils.readDataFromExcelFor(hwb,field,1,0);
}else{
XSSFWorkbook xwb = new XSSFWorkbook(in);
detailMaps=ExcelUtils.readDataFromExcelFor(xwb,field,1,0);
}
}catch(Exception e){
logger.info("*********************");
}
public static List readDataFromExcelFor(HSSFWorkbook workbook,String[] fields,int startRow,int startCol){
ArrayList list = new ArrayList();
HSSFSheet sheet = workbook.getSheetAt(0);
int rowCount = sheet.getPhysicalNumberOfRows();
HSSFFormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator(workbook);
// 设置单元格内容
HSSFRow _row;
for(int i=startRow;i<=rowCount-1;i++){
_row = sheet.getRow(i);
if(_row!=null){
HashMap map = new HashMap();
int col = fields.length;
for(int j=startCol;j<col;j++){
Object cellValue = null;
HSSFCell cell = _row.getCell(j);
if(cell != null){
try {
//测试强制日期。如果能转
if(HSSFDateUtil.isCellDateFormatted(cell)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String str = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();
cell.setCellValue(str);
}
}catch (Exception e){
logger.info("-------------"+e.getMessage());
}
HSSFCellStyle s = cell.getCellStyle();
short dataFormat = s.getDataFormat();
// 设置单元格的值
if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING){
cellValue = cell.getStringCellValue();
}else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
cellValue = (cell.getNumericCellValue());//处理下小数位,todo:这么写法会丢失小数精度
if(dataFormat!=0){
if(HSSFDateUtil.isCellDateFormatted(cell)){
//日期格式化
cellValue = cell.getDateCellValue();
}else{
//数字格式化
if(dataFormat>0){{
DecimalFormat decimalFormat=new DecimalFormat(s.getDataFormatString().replace("\\","").replace("\"",""));
cellValue = decimalFormat.format(cell.getNumericCellValue());
}}
}
}
}else if (cell.getCellType()== HSSFCell.CELL_TYPE_FORMULA){
String ret = "";
int retType = formulaEvaluator.evaluateFormulaCell(cell);
try {
switch (retType) {
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
ret = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
ret = "";
break;
case HSSFCell.CELL_TYPE_NUMERIC:
ret = String.valueOf(cell.getNumericCellValue());
if(HSSFDateUtil.isCellDateFormatted(cell)){
//日期格式化
Date dateCellValue = cell.getDateCellValue();
String dataFormatString = s.getDataFormatString();
dataFormatString = dataFormatString.replaceAll("y+", "yyyy").replaceAll("d+","dd").replace("m","M");
SimpleDateFormat simpleDateFormat=new SimpleDateFormat(dataFormatString);
ret = simpleDateFormat.format(dateCellValue);
}else{
//数字格式化
if(dataFormat>0){
DecimalFormat decimalFormat=new DecimalFormat(s.getDataFormatString().replace("\\","").replace("\"",""));
ret = decimalFormat.format(cell.getNumericCellValue());
}
}
break;
case HSSFCell.CELL_TYPE_STRING:
ret = cell.getRichStringCellValue().getString();
break;
default:
break;
}
} catch (Exception e) {
logger.error("Excel模板公式:"+e.getMessage());
}
cellValue = ret;
}
}
map.put(fields[j-startCol],cellValue);
}
list.add(map);
}
}
return list;
}
改改就能用了