package com.duia.web.controller.bang;
import com.duia.common.bean.MyPageModel;
import com.duia.common.bean.ParamUtil;
import com.duia.common.bean.ResponseJsonModel;
import com.duia.common.bean.annotation.PermissionAnn;
import com.duia.common.constant.HttpConstant;
import com.duia.model.bang.duiba.DuibaStar;
import com.duia.model.mars.app.StarConfig;
import com.duia.model.mars.system.SysDict;
import com.duia.service.bang.duiba.spi.IDuibaStarService;
import com.duia.service.mars.app.spi.IStarConfigService;
import com.duia.service.mars.system.spi.ISysDictService;
import com.duia.web.controller.BaseController;
import com.duia.web.controller.learn.SpokenController;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* Created by xiaochao on 2016/12/21.
*/
@Controller
@RequestMapping("/duibaStar")
public class DuibaStarController extends BaseController{
private static Logger logger = Logger.getLogger(SpokenController.class);
public List<String> uploadExcel(HttpServletRequest request){
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
List<String> list = new ArrayList<String>();
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
MultipartFile excel = entity.getValue();
try {
String type = excel.getOriginalFilename().substring(excel.getOriginalFilename().lastIndexOf("."), excel.getOriginalFilename().length());
Workbook wb = null;
if (type.equals(".xls")) {//2003
wb = new HSSFWorkbook(excel.getInputStream());
}
if (type.equals(".xlsx")) {//2007
wb = new XSSFWorkbook(excel.getInputStream());
}
return read(wb);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 需要多列的值则添加 case 1 ; case 2 ...
* @param wb
* @return
* @throws Exception
*/
private List<String> read(Workbook wb) throws Exception {
List<String> emailList = new LinkedList<String>();
try {
Sheet sheet = wb.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
for (int r = 0; r < rows; r++) {
// 定义 row
Row row = sheet.getRow(r);
if (row != null) {
int cells = row.getPhysicalNumberOfCells();
for (short c = 0; c < cells; c++) {
Cell cell = row.getCell(c);
cell.setCellType(cell.CELL_TYPE_STRING);
switch (c){
case 0:
emailList.add(cell.getStringCellValue());
break;
default:
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return emailList;
}
}
import com.duia.common.bean.MyPageModel;
import com.duia.common.bean.ParamUtil;
import com.duia.common.bean.ResponseJsonModel;
import com.duia.common.bean.annotation.PermissionAnn;
import com.duia.common.constant.HttpConstant;
import com.duia.model.bang.duiba.DuibaStar;
import com.duia.model.mars.app.StarConfig;
import com.duia.model.mars.system.SysDict;
import com.duia.service.bang.duiba.spi.IDuibaStarService;
import com.duia.service.mars.app.spi.IStarConfigService;
import com.duia.service.mars.system.spi.ISysDictService;
import com.duia.web.controller.BaseController;
import com.duia.web.controller.learn.SpokenController;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* Created by xiaochao on 2016/12/21.
*/
@Controller
@RequestMapping("/duibaStar")
public class DuibaStarController extends BaseController{
private static Logger logger = Logger.getLogger(SpokenController.class);
public List<String> uploadExcel(HttpServletRequest request){
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
List<String> list = new ArrayList<String>();
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
MultipartFile excel = entity.getValue();
try {
String type = excel.getOriginalFilename().substring(excel.getOriginalFilename().lastIndexOf("."), excel.getOriginalFilename().length());
Workbook wb = null;
if (type.equals(".xls")) {//2003
wb = new HSSFWorkbook(excel.getInputStream());
}
if (type.equals(".xlsx")) {//2007
wb = new XSSFWorkbook(excel.getInputStream());
}
return read(wb);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 需要多列的值则添加 case 1 ; case 2 ...
* @param wb
* @return
* @throws Exception
*/
private List<String> read(Workbook wb) throws Exception {
List<String> emailList = new LinkedList<String>();
try {
Sheet sheet = wb.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
for (int r = 0; r < rows; r++) {
// 定义 row
Row row = sheet.getRow(r);
if (row != null) {
int cells = row.getPhysicalNumberOfCells();
for (short c = 0; c < cells; c++) {
Cell cell = row.getCell(c);
cell.setCellType(cell.CELL_TYPE_STRING);
switch (c){
case 0:
emailList.add(cell.getStringCellValue());
break;
default:
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return emailList;
}
}