/**
* 上传文件
*
* @param action
* @param request
* @param response
* @return
*/
public ActionForward doUploadFiles(Action action,
HttpServletRequest request, HttpServletResponse response) {
DoradoContext context = DoradoContext.getContext();
context.setAttribute(DoradoContext.SESSION, "fileNames", null);
String responseMessage = "文件上传成功!";
String fileName = "";
AttachmentService service = (AttachmentService)ServiceLocator.getService("attachmentService");
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(factory);
fileUpload.setFileSizeMax(5 * 1024 * 1024);
fileUpload.setHeaderEncoding("utf-8");
List<?> fileList = fileUpload.parseRequest(request);
for (Object obj : fileList) {
FileItem fileItem = (FileItem) obj;
if (!fileItem.isFormField()) {
fileName = fileItem.getName();
System.out.println("-------解析文件--------file------" + fileName);
if (StringUtils.isEmpty(fileName)
|| fileItem.getSize() <= 0) {
continue;
}
// File.separator分隔符
int k = fileName.lastIndexOf(File.separator);
if (k < 0) {
k = fileName.lastIndexOf(':');
}
fileName = fileName.substring(k + 1);
// 上传
final int BUFFER_SIZE = 4096;
try {
InputStream in = fileItem.getInputStream();
FileOutputStream out = new FileOutputStream(fileName);
try {
int numRead;
byte[] buffer = new byte[BUFFER_SIZE];
while ((numRead = in.read(buffer, 0, BUFFER_SIZE)) > 0) {
out.write(buffer, 0, numRead);
}
// 解析Excel
doReadXlsx(fileName);
} finally {
out.close();
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (FileUploadException fe) {
// TODO Auto-generated catch block
responseMessage = "文件上传失败: " + fe.getMessage();
fe.printStackTrace();
}
handleSuccess(response, responseMessage);
return null;
}
/**
* 解析Excel
*
* @param fileName
* @throws ParseException
*/
public Boolean doReadXlsx(String fileName){
//AttachmentService service = (AttachmentService)ServiceLocator.getService("attachmentService");
BlackListService blackListService = (BlackListService)ServiceLocator.getService("blackListService");
File file = new File(fileName);
XSSFWorkbook xssfWorkbook = null;
Boolean flag = false;
try {
xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 循环工作表
for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
if (xssfSheet == null) {
continue;
}
List<BlackListVo> voList = new ArrayList<BlackListVo>();
BlackListVo vo = null;
// 循环行row
for (int rowNum = 1; rowNum < xssfSheet.getLastRowNum() + 1; rowNum++) {
vo = new BlackListVo();
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
if (xssfRow == null) {
continue;
}
// 循环列cell
for (int cellNum = 0; cellNum < xssfRow.getLastCellNum(); cellNum++) {
XSSFCell xssfCell = xssfRow.getCell(cellNum);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
if (xssfCell == null) {
System.out.print("null");
continue;
}
// 保存到BlackListVo
if(cellNum == 0){
vo.setCustomercode(getValue(xssfCell));
}else if (cellNum == 1) {
vo.setEnglishname(getValue(xssfCell));
}else if (cellNum == 2) {
vo.setCustomername(getValue(xssfCell));
}else if (cellNum == 3) {
vo.setCustname2(getValue(xssfCell));
}else if (cellNum == 4) {
vo.setMemo(getValue(xssfCell));
}else if (cellNum == 5) {
try {
vo.setRegistdate(sdf.parse(getValue(xssfCell).toString()));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if (cellNum == 6) {
try {
vo.setModifydate(sdf.parse(getValue(xssfCell).toString()));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if (cellNum == 7) {
vo.setIsvalid(getValue(xssfCell));
}else if (cellNum == 8) {
vo.setBlacktype(getValue(xssfCell));
}
System.out.print(" " + getValue(xssfCell));
}
voList.add(vo);
flag = true;
System.out.println();
}
blackListService.saveBatchBlackList(voList);
}
return flag;
}
/**
* 得到Excel表中的值* @param xssfCell
* @return
*/
@SuppressWarnings("static-access")
private String getValue(XSSFCell xssfCell) {
switch (xssfCell.getCellType()) {
//返回布尔类型的值
case Cell.CELL_TYPE_BOOLEAN:
return String.valueOf(xssfCell.getBooleanCellValue() ? "TRUE"
: "FALSE");
//先识别是否为日期,日期则返回String类型的值,因为数字会转成科学计数法
case Cell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(xssfCell)) {
// 如果是Date类型则,转化为Data格式
// 方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00
// xssfCell.getDateCellValue().toLocaleString();
// 方法2:这样子的data格式是不带时分秒的:2011-10-12
Date date = xssfCell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return String.valueOf(sdf.format(date));
//return String.valueOf(xssfCell.getDateCellValue().toLocaleString());
} else {
xssfCell.setCellType(Cell.CELL_TYPE_STRING);
return String.valueOf(xssfCell.getStringCellValue());
}
//返回字符串
case Cell.CELL_TYPE_STRING:
return String.valueOf(xssfCell.getStringCellValue());
//返回表达式计算的值
case Cell.CELL_TYPE_FORMULA:
return String.valueOf(xssfCell.getCellFormula());
default:
return "";
}
}