package com.hans.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.hans.pojo.Orderinfo;
@Controller
@RequestMapping("/xcel")
public class Excel_xls {
private final static String excel2003L =".xls"; //2003- 版本的excel
private final static String excel2007U =".xlsx"; //2007+ 版本的excel
@RequestMapping("/findexcel")
public String excel(HttpServletRequest request) throws Exception
{
/*File excelFile = new File("C:\fakepath\123.xlsx"); //替换你文档地址
XSSFWorkbook wb = null;
try {
wb = new XSSFWorkbook(new FileInputStream(excelFile));
} catch (IOException e) {
e.printStackTrace();
}
int numberOfSheets = wb.getNumberOfSheets();
String str = "";*/
MultipartHttpServletRequest mult=(MultipartHttpServletRequest) request;
MultipartFile file=mult.getFile("pic");
if(file.isEmpty()){
System.out.println("文件为空");
}else{
System.out.println("文件不为空");
}
InputStream in = null;
in = file.getInputStream();
List list= getBankListByExcelTitle(in, file.getOriginalFilename(), 14);
for( int i=0;i
List one=(List) list.get(i);
/*for(int j=0;j
}*/
Orderinfo orderinfo=new Orderinfo();
String first=(String) one.get(0);
orderinfo.setContractName(first);
}
return null;
}
/**
* 描述:获取IO流中的数据,组装成List>对象
* @param in,fileName
* @return
* @throws IOException
*/
public static List> getBankListByExcelTitle(InputStream in,String fileName,Integer lastCellNum) throws Exception{
List> list = null;
//创建Excel工作薄 技术部
Workbook work = getWorkbook(in,fileName);
if(null == work){
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
list = new LinkedList>();
//遍历Excel中所有的sheet
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if(sheet==null){continue;}
//遍历当前sheet中的所有行
for (int j = sheet.getFirstRowNum(); j < sheet.getPhysicalNumberOfRows(); j++) {
row = sheet.getRow(j);
/*if(row==null||row.getFirstCellNum()==j){continue;}*/
//遍历所有的列
List
/**
* 描述:对表格中数值进行格式化
* @param cell
* @return
*/
public static Object getCellValue(Cell cell){
Object value = null;
DecimalFormat df = new DecimalFormat("0"); //格式化number String字符
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd"); //日期格式化
DecimalFormat df2 = new DecimalFormat("0.00"); //格式化数字
if(cell != null){
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString().trim();
break;
case Cell.CELL_TYPE_NUMERIC:
if("General".equals(cell.getCellStyle().getDataFormatString())){
String str=cell.getNumericCellValue()+"";
String[] strs=str.split("\\.");
double str1=Double.parseDouble(strs[1]);
if(str1>0){
value = df2.format(cell.getNumericCellValue()).trim();
}else{
value = df.format(cell.getNumericCellValue()).trim();
}
}else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
value = sdf.format(cell.getDateCellValue()).trim();
}else{
value = df2.format(cell.getNumericCellValue()).trim();
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
value = "";
break;
default:
break;
}
}
return value;
}
}
html页面
springMVC记得配置文件上传