码迷,mamicode.com
首页 > 其他好文 > 详细

业务操作系统(五)

时间:2016-08-11 20:43:02      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

一、文件异步上传

     传统表单提交文件上传会刷新页面,使用OCUpload插件可以实现异步上传。

(1)页面部分

技术分享

 

(2)Action

@Controller
@Scope("prototype")
public class RegionAction extends BaseAction<Region>{

	@Autowired
	private RegionService regionService;
	
	private File myFile;
	private String myFileFileName;
	
	public String importExcel() throws IOException{
		
		if(myFile != null){
			
			//是否是Excel
			if(myFileFileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){
				
				String flag = "1";
				
				try{
					regionService.importExcel(myFile,myFileFileName);
				}catch(Exception e){
					e.printStackTrace();
					flag = "0";
				}
				
				ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
				ServletActionContext.getResponse().getWriter().print(flag);	
			}
		}
		return NONE;
	}
	
	public void setMyFile(File myFile){
		this.myFile = myFile;
	}

	public String getMyFileFileName(){
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName){
		this.myFileFileName = myFileFileName;
	}
}

 

(3)POI读取Excel

@Service
@Transactional
public class RegionServiceImpl implements RegionService{

	@Autowired
	private RegionDao regionDao;

	@Override
	public void importExcel(File myFile,String fileName) throws Exception{

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

		boolean is03Excel = fileName.matches("^.+\\.(?i)(xls)$");

		FileInputStream inputStream = new FileInputStream(myFile);

		// 1、读取工作簿
		Workbook workbook = is03Excel ? new HSSFWorkbook(inputStream) : new XSSFWorkbook(inputStream);
		// 2、读取第一个工作表
		Sheet sheet = workbook.getSheetAt(0);

		// 遍历获取每行
		for(Row row : sheet){

			// 获取行号
			int num = row.getRowNum();

			// 第一行不保存到数据库
			if(num != 0){

				row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
				row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
				row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
				row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
				row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);

				// 获取省市县
				String id = row.getCell(0).getStringCellValue();
				String province = row.getCell(1).getStringCellValue();
				String city = row.getCell(2).getStringCellValue();
				String district = row.getCell(3).getStringCellValue();
				String postcode = row.getCell(4).getStringCellValue();

				Region region = new Region(id,province,city,district,postcode,null,null,null);
				list.add(region);
			}
		}

		regionDao.saveBatch(list);

	}
}
@Repository
public class RegionDaoImpl extends BaseDaoImpl<Region> implements RegionDao{

	@Override
	public void saveBatch(List<Region> list){
		for(Region region : list){
			saveOrUpdate(region);
		}
	}
}

业务操作系统(五)

标签:

原文地址:http://www.cnblogs.com/yangang2013/p/5762473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!