文件上传返回存贮路径
public static String fileUpload(HttpServletRequest request, HttpServletResponse response) throws FileUploadException, IOException{
String filepath = null;
String filename = null;
response.setContentType("text/html;charset=utf-8");
DiskFileItemFactory factory=new DiskFileItemFactory();
File f=new File("E:\\TempFolder");
if (!f.exists()){
f.mkdirs();
}
factory.setRepository(f);
ServletFileUpload fileUpload=new ServletFileUpload(factory);
fileUpload.setHeaderEncoding("utf-8");
List<FileItem> fileItems = fileUpload.parseRequest(request);
for (FileItem fileItem:fileItems) {
if (fileItem.isFormField()){
} else {
filename=fileItem.getName();
if(filename!=null&&filename!=""){
filename= UUID.randomUUID().toString()+"_"+filename;
String webpath="/upload/";
filepath = "E:/TempFolder"+webpath+filename;
File file=new File(filepath);
file.getParentFile().mkdirs();
file.createNewFile();
InputStream in=fileItem.getInputStream();
FileOutputStream out=new FileOutputStream(file);
byte[] bytes=new byte[1024];
int len;
while ((len=in.read(bytes))>0) {
out.write(bytes, 0, len);
}
in.close();
out.close();
fileItem.delete();
}
}
}
return filepath;
}
解析表头不固定Excel 返回List集合
因业务需求 每次上传表头顺序无法确定 ,没想到更好的方法
采用表头名称固定 遍历后 添加到集合中 和数据库字段对应 得到固定的顺序
public static List<Map<String,String>> analysisExcel(String filepath) throws IOException{
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
File excel = new File(filepath);
if (excel.isFile() && excel.exists()) {
String[] split = excel.getName().split("\\.");
Workbook wb=null;
if ( "xls".equals(split[1])){
FileInputStream fis = new FileInputStream(excel);
wb = new HSSFWorkbook(fis);
}else if ("xlsx".equals(split[1])){
FileInputStream fis = new FileInputStream(excel);
wb = new XSSFWorkbook(fis);
}else {
return null;
}
Sheet sheet = wb.getSheetAt(0);
List<String> listBt = new ArrayList<String>();
Row bt = sheet.getRow(0);
if (bt != null) {
int firstIndex = bt.getFirstCellNum();
int lastIndex = bt.getLastCellNum();
for (int cellIndex = firstIndex; cellIndex < lastIndex; cellIndex++) {
String cell = bt.getCell(cellIndex).toString();
if(cell.equals("序号")){ listBt.add("ID");}
}
}
int firstRowIndex = sheet.getFirstRowNum()+1;
int lastRowIndex = sheet.getLastRowNum();
for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
Row row = sheet.getRow(rIndex);
if (row != null) {
int firstCellIndex = row.getFirstCellNum();
int lastCellIndex = row.getLastCellNum();
Map<String,String> map = new HashMap<String, String>();
for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {
Cell cell = row.getCell(cIndex);
String key = listBt.get(cIndex);
map.put(key, cell.toString());
}
list.add(map);
}
}
} else {
}
return list;
}
多sheet页解析
上传excel 用sheet页名称做数据库某一关键字段 ,将sheet页名称拼如集合中返回:
public static List<Map<String,String>> analysisExcel(String filepath,String[] field) throws IOException{
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
File excel = new File(filepath);
if (excel.isFile() && excel.exists()) {
String[] split = excel.getName().split("\\.");
Workbook wb=null;
if ( "xls".equals(split[1])){
FileInputStream fis = new FileInputStream(excel);
wb = new HSSFWorkbook(fis);
}else if ("xlsx".equals(split[1])){
FileInputStream fis = new FileInputStream(excel);
wb = new XSSFWorkbook(fis);
}else {
return null;
}
int sheetCount = wb.getNumberOfSheets();
for(int i = 0;i<sheetCount;i++){
Sheet sheet = wb.getSheetAt(i);
String sheetName = sheet.getSheetName();
int firstRowIndex = sheet.getFirstRowNum()+1;
int lastRowIndex = sheet.getLastRowNum();
for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
Row row = sheet.getRow(rIndex);
if (row != null) {
int firstCellIndex = row.getFirstCellNum();
int lastCellIndex = row.getLastCellNum();
Map<String,String> map = new HashMap<String, String>();
for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {
Cell cell = row.getCell(cIndex);
map.put(field[cIndex], cell.toString());
}
map.put(field[lastCellIndex], sheetName);
list.add(map);
}
}
}
} else {
}
return list;
}