分区数据导出功能(服务端实现)
生活随笔
收集整理的這篇文章主要介紹了
分区数据导出功能(服务端实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
服務端實現
第一步:查詢所有的分區數據
第二步:使用POI將數據寫到Excel文件中
第三步:使用輸出流進行文件下載
package com.learn.bos.web.action;import java.io.IOException; import java.util.List;import javax.annotation.Resource; import javax.servlet.ServletOutputStream;import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.struts2.ServletActionContext; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Restrictions; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller;import com.learn.bos.domain.Region; import com.learn.bos.domain.Subarea; import com.learn.bos.service.ISubareaService; import com.learn.bos.utils.FileUtils; import com.learn.bos.web.action.base.BaseAction; @Controller @Scope("prototype") public class SubareaAction extends BaseAction<Subarea>{@Resourceprivate ISubareaService subareaService;/*** 添加分區*/public String add(){subareaService.save(model);return LIST;}/*** 分區數據導出功能* @throws IOException */public String exportXls() throws IOException{//第一步:查詢所有的分區數據List<Subarea> list = subareaService.findAll();//第二步:使用POI將數據寫到Excel文件中//在內存中創建一個Excel文件HSSFWorkbook workbook = new HSSFWorkbook();//創建一個標簽頁HSSFSheet sheet = workbook.createSheet("分區數據");//創建標題行HSSFRow headRow = sheet.createRow(0);headRow.createCell(0).setCellValue("分區編號");headRow.createCell(1).setCellValue("開始編號");headRow.createCell(2).setCellValue("結束編號");headRow.createCell(3).setCellValue("位置信息");headRow.createCell(4).setCellValue("省市區");for (Subarea subarea : list) {HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);dataRow.createCell(0).setCellValue(subarea.getId());dataRow.createCell(1).setCellValue(subarea.getStartnum());dataRow.createCell(2).setCellValue(subarea.getEndnum());dataRow.createCell(3).setCellValue(subarea.getPosition());dataRow.createCell(4).setCellValue(subarea.getRegion().getName());}//第三步:使用輸出流進行文件下載(一個流、兩個頭)String filename = "分區數據.xls";String contentType = ServletActionContext.getServletContext().getMimeType(filename);ServletOutputStream out = ServletActionContext.getResponse().getOutputStream();ServletActionContext.getResponse().setContentType(contentType);//獲取客戶端瀏覽器類型String agent = ServletActionContext.getRequest().getHeader("User-Agent");filename = FileUtils.encodeDownloadFilename(filename, agent);ServletActionContext.getResponse().setHeader("content-disposition", "attachment;filename="+filename);workbook.write(out);return NONE;}} package com.learn.bos.utils;import java.io.IOException; import java.net.URLEncoder;import sun.misc.BASE64Encoder;public class FileUtils {/*** 下載文件時,針對不同瀏覽器,進行附件名的編碼* * @param filename* 下載文件名* @param agent* 客戶端瀏覽器* @return 編碼后的下載附件名* @throws IOException*/public static String encodeDownloadFilename(String filename, String agent)throws IOException {if (agent.contains("Firefox")) { // 火狐瀏覽器filename = "=?UTF-8?B?"+ new BASE64Encoder().encode(filename.getBytes("utf-8"))+ "?=";filename = filename.replaceAll("\r\n", "");} else { // IE及其他瀏覽器filename = URLEncoder.encode(filename, "utf-8");filename = filename.replace("+"," ");}return filename;} }?
總結
以上是生活随笔為你收集整理的分区数据导出功能(服务端实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分区数据导出功能(页面调整)
- 下一篇: POI文件导入:需求说明