Jquery的一键上传组件OCUpload及POI解析Excel文件

时间:2024-03-08 17:54:45

第一步:将js文件引入页面

 

<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.ocupload-1.1.2.js"></script>

 

第二步:在页面中提供任意一个元素

第三步:调用插件提供的upload方法,动态修改HTML页面元素

 

1 <script type="text/javascript">
2     $(function(){
3         //页面加载完成后,调用插件的upload方法,动态修改了HTML页面元素
4         $("#myButton").upload({
5             action:\'xxx.action\',
6             name:\'myFile\'
7         });
8     });
9 </script>

 

第四步:服务费利用同名文件接收

POI需要在项目中引入依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.11</version>
</dependency>
 1 //使用POI解析
 2         HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(regionFile));
 3         HSSFSheet sheet = workbook.getSheetAt(0);
 4         List<Region> regionList = new ArrayList<Region>();
 5         for(Row row : sheet) {
 6             int rowNum = row.getRowNum();
 7             if(rowNum == 0) {
 8                 continue;
 9             }
10             String id = row.getCell(0).getStringCellValue();
11             String province = row.getCell(1).getStringCellValue();
12             String city = row.getCell(2).getStringCellValue();
13             String district = row.getCell(3).getStringCellValue();
14             String postcode = row.getCell(4).getStringCellValue();
15             //包装成区域对象
16             Region region = new Region();
17             region.setId(id);
18             region.setProvince(province);
19             region.setCity(city);
20             region.setDistrict(district);
21             region.setPostcode(postcode);
22             
23             province = province.substring(0, province.length()-1);
24             city = city.substring(0, city.length()-1);
25             district = district.substring(0, district.length()-1);
26             String info = province + city + district;
27             //利用pinyin获得首字母简码
28             String[] headByString = PinYin4jUtils.getHeadByString(info);
29             String shortcode = StringUtils.join(headByString);
30             //城市编码
31             String citycode = PinYin4jUtils.hanziToPinyin(city, "");
32             region.setCitycode(citycode);
33             region.setShortcode(shortcode);
34             
35             regionList.add(region);

 POI生成Excel文件并下载

 1 //分区数据导出
 2     public String exportXls() throws IOException {
 3         //查询所有数据
 4         List<Subarea> subareaList = subareaService.findAll();
 5         //利用POI写到Excel中
 6         HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
 7         //创建标签页
 8         HSSFSheet sheet = hssfWorkbook.createSheet("分区数据");
 9         //创建行
10         HSSFRow headRow = sheet.createRow(0);
11         headRow.createCell(0).setCellValue("分区编号");
12         headRow.createCell(1).setCellValue("开始编号");
13         headRow.createCell(2).setCellValue("结束编号");
14         headRow.createCell(3).setCellValue("位置信息");
15         headRow.createCell(4).setCellValue("省市区");
16         
17         for(Subarea subarea : subareaList) {
18             HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
19             dataRow.createCell(0).setCellValue(subarea.getId());
20             dataRow.createCell(1).setCellValue(subarea.getStartnum());
21             dataRow.createCell(2).setCellValue(subarea.getEndnum());
22             dataRow.createCell(3).setCellValue(subarea.getPosition());
23             dataRow.createCell(4).setCellValue(subarea.getRegion().getName());
24         }
25         //提供下载
26         String filename = "分区数据.xls";
27         
28         String contentType = ServletActionContext.getServletContext().getMimeType(filename);
29         ServletOutputStream outputStream = ServletActionContext.getResponse().getOutputStream();
30         hssfWorkbook.write(outputStream);
31         //获取客户端浏览器类型
32         String agent = ServletActionContext.getRequest().getHeader("User-Agent");
33         filename = FileUtils.encodeDownloadFilename(filename, agent);
34         //设置响应头
35         ServletActionContext.getResponse().setHeader("content-disposition", "attachment;filename="+filename);
36         //ServletActionContext.getResponse().setContentType("application/vnd.ms-excel");
37         ServletActionContext.getResponse().setContentType(contentType);
38         return NONE;
39     }