數據的導入導出
阿新 • • 發佈:2017-07-04
數據統計 stk pri con imp response rip tac keywords
一.Excel的數據導入
A:OCUpload實現文件的上傳
1、 將js文件復制到項目中
2、 在頁面中引入一鍵上傳js文件
<script type="text/javascript" src="../js/ocupload/jquery.ocupload-1.1.2.js"></script>
必須引入jQuery
3、在頁面中提供任意一個元素
<input id="mybutton" type="button" value="upload">
4、調用一鍵上傳插件提供的upload方法,作用是動態修改頁面HTML代碼
$(function(){ $("#mybutton").upload({ name:‘myFile‘, action:‘xx.action‘ }); });
action指定訪問的後臺action的位置.服務端解析上傳的文件.
B:Apache POI解析Excel文件
1.poi的maven坐標引入
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>${poi.version}</version> </dependency>
2.頁面方法調用(例)
$("#button-import").upload({
name:"areaFile",
action:"../../areaAction_importXls.action"
});
3.後臺處理
private File areaFile; public void setAreaFile(File areaFile) { this.areaFile = areaFile; } @Autowired private AreaService service; @Action(value="areaAction_importXls") public String importXls() throws Exception { //通過輸入流加載excle文件 HSSFWorkbook workbook= new HSSFWorkbook(new FileInputStream(areaFile)); //獲得第一個sheet表單 HSSFSheet sheetAt = workbook.getSheetAt(0); //定義一個集合,遍歷所有行,獲得數據後通過構造方法生成area對象,在把area對象添加到集合中 List<Area> list = new ArrayList<>(); for (Row row : sheetAt) { if(row.getRowNum() == 0){ continue; } String id = row.getCell(0).getStringCellValue(); String provice = row.getCell(1).getStringCellValue(); String city = row.getCell(2).getStringCellValue(); String district = row.getCell(3).getStringCellValue(); String postcode = row.getCell(4).getStringCellValue(); Area area = new Area(id,provice,city,district,postcode); list.add(area); } //調用service的保存方法 service.save(list); return NONE; }
二.Excel文件的導出(例)
1.修改頁面導出按鈕,綁定方法
function doExport(){
//發送ajax請求,通過action查詢所有分區,通過poi導出到Excel中,通過流進行下載
window.location.href="../../subAreaAction_exportXls.action";
}
2、通過POI將查詢到的分區數據寫到Excel文件中,通過輸出流將文件寫回客戶端進行文件下載
@Action(value="subareaAction_exportXls") public String exportXls() throws IOException{ //1、查詢所有分區數據 List<SubArea> list = service.findAll(); //2、基於POI在內存中創建一個Excel文件 HSSFWorkbook excel = new HSSFWorkbook(); //3、在Excel文件中創建一個sheet頁 HSSFSheet sheet = excel.createSheet("分區數據統計"); //4、在標簽頁中創建行 HSSFRow title = sheet.createRow(0); //5、在行中創建列 title.createCell(0).setCellValue("編號"); title.createCell(1).setCellValue("分區起始編號"); title.createCell(2).setCellValue("分區結束編號"); title.createCell(3).setCellValue("分區關鍵字"); title.createCell(4).setCellValue("輔助關鍵字"); title.createCell(5).setCellValue("區域信息"); for(SubArea subarea : list){ //每個分區對象對應一行數據 HSSFRow data = sheet.createRow(sheet.getLastRowNum() + 1); //在行中創建列 data.createCell(0).setCellValue(subarea.getId()); data.createCell(1).setCellValue(subarea.getStartNum()); data.createCell(2).setCellValue(subarea.getEndNum()); data.createCell(3).setCellValue(subarea.getKeyWords()); data.createCell(4).setCellValue(subarea.getAssistKeyWords()); data.createCell(5).setCellValue(subarea.getArea().getName()); } //6、通過輸出流寫回Excel文件到瀏覽器,文件下載需要一個流(輸出流)、兩個頭(設置頭信息) ServletOutputStream out = ServletActionContext.getResponse().getOutputStream(); String filename = "分區數據統計結果.xls"; String agent = ServletActionContext.getRequest().getHeader("User-Agent");//客戶端使用的瀏覽器類型 //處理中文文件名 filename = FileUtils.encodeDownloadFilename(filename, agent); String mimeType = ServletActionContext.getServletContext().getMimeType(filename); ServletActionContext.getResponse().setContentType(mimeType); ServletActionContext.getResponse().setHeader("content-disposition", "attachment;filename=" + filename); excel.write(out); return NONE; }
數據的導入導出