您的位置:首页 > 编程语言 > Java开发

struts2 上传

2015-10-15 15:32 615 查看

private File excel;

private List<String> resultInfos;

/**

 * 上传遗留学员信息

 */

@Action(value = "uploadLeaveStuInfo", results = {

@Result(name = "success", type = "dispatcher", location = "result.jsp"),

@Result(name = "error", type = "dispatcher", location = "upload.jsp") })

public String uploadLeaveStuInfo() {

if (log.isDebugEnabled()) {

log.debug(" uploadLeaveStuInfo() ======>> start ");

}

try {

// 处理上传信息

resultInfos = servic.handExamApply(excel, excelFileNamel);

resultInfos.add("<font color=\"green\">请勿关闭页面,正在拼命处理您的上传数据!<br>点击查看进度查看</font>");

} catch (Exception e) {

}

if (log.isDebugEnabled()) {

log.debug(" uploadLeaveStuInfo() <<====== end ");

}

return SUCCESS;

}

 

public List<String> hand(File file, String fileName) {

if (log.isDebugEnabled()) {

log.debug();

}

List<Map<Object, Object>> result = new LinkedList<Map<Object, Object>>();

String[] title = { "", "", "", "","" };

result = ExcelUtils.operaExcel(file, fileName, title);

RESULT_SIZE = result.size() - 1;

this.setResult(result);

 

new Thread(new runThreadA()).start();

 

List<String> list = new ArrayList<String>();

if (log.isDebugEnabled()) {

}

return list;

}

 

处理excel方法

public static List<Map<Object, Object>> operaExcel(File file,

String fileName, String excelTitle[]) {

List<Map<Object, Object>> resultList = new LinkedList<Map<Object, Object>>();

Workbook workbook = null;

if (null != file && null != fileName) {

try {

InputStream in = new FileInputStream(file);

if (fileName.endsWith("xls")) {

workbook = new HSSFWorkbook(in);

int sheets = workbook != null ? workbook

.getNumberOfSheets() : 0;

for (int i = 0; i < sheets; i++) {

// 读取各个sheet

Sheet sheet = workbook.getSheetAt(i);

// 获得行数

int rows = sheet.getPhysicalNumberOfRows();

// 获得列表头

Row titleRow = sheet.getRow(0);

if (null == titleRow) {

continue;

}

int titleCount = titleRow.getLastCellNum();

Map<Object, Object> errorMap = new HashMap<Object, Object>();

if (titleCount != excelTitle.length) {

errorMap.put("errorInfo",

"您所上传的excel文件与模板不符,请使用下载的模板文件。");

resultList.add(errorMap);

} else {

// 遍历每一行的信息

for (int j = 0; j < rows; j++) {

// 实例化rowMap

Map<Object, Object> rowMap = new HashMap<Object, Object>();

Row row = sheet.getRow(j);

// 获得总列数

int cells = row.getLastCellNum();

// 遍历每一列

for (int k = 0; k < cells; k++) {

Cell cell = row.getCell(k);

if (null == cell) {

continue;

}

switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_NUMERIC:// 日期类型

if (HSSFDateUtil

.isCellDateFormatted(cell)) {

cell.setCellType(Cell.CELL_TYPE_NUMERIC);

rowMap.put(titleRow.getCell(k)

.getStringCellValue(), cell

.getDateCellValue());

} else {

cell.setCellType(Cell.CELL_TYPE_STRING);

rowMap.put(titleRow.getCell(k)

.getStringCellValue(), cell

.getStringCellValue()

.trim());

}

break;

case HSSFCell.CELL_TYPE_STRING:// 字符串类型

cell.setCellType(Cell.CELL_TYPE_STRING);

rowMap.put(titleRow.getCell(k)

.getStringCellValue(), cell

.getStringCellValue().trim());

break;

default:// 其他

cell.setCellType(Cell.CELL_TYPE_STRING);

rowMap.put(titleRow.getCell(k)

.getStringCellValue(), cell

.getStringCellValue().trim());

}

}

resultList.add(rowMap);

}

}

}

}

} catch (FileNotFoundException e) {

} catch (IOException e) {

}

}

return resultList;

}

}

 

 

另起一个线程处理

class runThreadA implements Runnable {

@Override

public void run() {

String msg = processData(result);

}

}

 

public synchronized String processData(List<Map<Object, Object>> result) {

Map<String, Object> errorMap = new HashMap<String, Object>();

if (null == result || result.size() <= 0) {

errorMap.put("tips", "未从excel中解析到数据");

} else {

Period period = new Period();

period = examApplyPeriodService

.selectAvailableEaxmSelfPeriod(school);

if (result.size() <= 1) {

errorMap.put("error", "请使用下载的excel模板,并将excel录入数据后再上传");

}

forcase: for (int i = 1; i < result.size(); i++) {

Date date = new Date();

Map<Object, Object> resultMap = result.get(i);

String errorInfo = (String) resultMap.get("errorInfo");

if (null != errorInfo && !"".equals(errorInfo)) {

errorMap.put("error", errorInfo);

} else {

处理业务 把相关内容保存进数据库

}

}

}

errorMap.put("successCount", SUCCESS_PASS_MESSAGE);

errorMap.put("errorCount", ERROR_PASS_MESSAGE);

Set<String> set = errorMap.keySet();

StringBuffer message = new StringBuffer();

for (Iterator<String> it = set.iterator(); it.hasNext();) {

String name = it.next();

if (!"successCount".equals(name) && !"errorCount".equals(name)) {

message.append("<font color=\"red\">" + errorMap.get(name)

+ "</font>" + "<br>");

}

}

return message.toString();

}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: