導出下載多個Excel文件並打包成zip文件

導出下載單個Excel也許簡單,但是有的時候也會用到下載多個excel並且打包成zip文件的情況,最近遇到了,現在簡單整理一下!

1、ftl文件

 <a class="btn  " type="button" onclick="exportSelectExcel()"><i class="fa fa-share-square-o text-olive"> </i> 導出</a>

	  <table id="annualPlanTable" 
	        	class="table table-striped table-hover"
	        	data-sort-name="createTime"
		        data-order-name="desc"
		        data-toggle="table"
		        data-url="listManage_sgJSONData"
		        data-query-params="getBootstrapTableParams"
		        data-side-pagination="server"	
		        data-pagination="true"
	        	>
		<thead>
			<tr>
		<span style="white-space:pre">	</span><th data-checkbox="true"></th>
			<th data-field="planName" data-formatter="planNameFormatter">名稱</th>
			<th data-field="planNo">計劃編號</th>
			<th data-field="version">版本編號</th>
			<th data-field="commandName">指揮部</th>
			<th data-field="sectionName">標段</th>
			<th data-field="createTime" data-formatter="GT.BootStrap.formatToYYYYMMDD">提報時間</th>
			<th data-field="entrPsnName">提報人</th>
			<th data-field="status">審覈狀態</th>
			<th data-field="operation" data-formatter="operationFormatter">操作</th>
			</tr>
		</thead>
	  </table>

2、js文件

//導出中
function exportSelectExcel(){
	var checkedData = GT.BootStrap.checkDataIdsToBootstrap("annualPlanTable");
	if (checkedData==0) { gAlert("請至少擇一個要導出的項目","友情提示");return;}
	exportPlanMaterialExcelById(checkedData);
}
function exportPlanMaterialExcelById(checkedData){
	if(checkedData){
		var url = BASEUrl+"exportPlanMaterialExcelById?ids="+checkedData;
		window.open(url);
	}
}

3、controller方法

import com.gt.util.common.ExcelImport;
import com.gt.util.common.FileDownloadBean;
import com.gt.util.common.FileDownloadUtil;
import com.gt.util.common.FileDownloadZipUtil;

/**
	 * 導出需求計劃中的物資信息
	 * @param request
	 *            中ids 每一個需求計劃導出一個excel
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/exportPlanMaterialExcelById")
	public void exportPlanMaterialExcelById(@RequestParam(value = "ids") String ids,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		try {
			List<FileDownloadBean> fileDownloadBeans=new ArrayList<FileDownloadBean>();
			List<Long> idArry=stringToLongList(ids);
			for (Long id : idArry) {
				FileDownloadBean fileDownloadBean=<span style="color:#ff0000;">createFileDownloadBean</span>(request, response, id, null);
				fileDownloadBeans.add(fileDownloadBean);
			}
			FileDownloadZipUtil.downLoadExcelToZip(fileDownloadBeans,"全線年度需求計劃",request, response);
		} catch (Exception e) {
			log.error("全線/需求計劃管理頁-導出需求計劃中的物資信息報錯。" + e.getMessage(),e);
		}
	}


/**
	 * 
	 * <構造下載所需的javabean> 
	 * <功能詳細描述>
	 * @author XXX
	 * @date 2016年5月28日
	 * @param request
	 * @param response
	 * @param blngsPtbId
	 * @param searchMaterialParams
	 * @return
	 */
	@SuppressWarnings({"rawtypes" })
	private FileDownloadBean  <span style="color:#ff0000;">createFileDownloadBean</span>(HttpServletRequest request,HttpServletResponse response,Long blngsPtbId,String searchMaterialParams){
	    FileDownloadBean fileDownloadBean=new FileDownloadBean();
		try {  
			    DemandSupplyPlanVO demandSupplyPlanVO =  demandSupplyPlanService.getById(blngsPtbId);
				BootstrapPager pager=new BootstrapPager();
				pager.setlimit(Integer.MAX_VALUE);
				List<DemandSupplyMaterialVO> demandSupplyMaterials=demandSupplyMaterialService.findByBlngsPtbId(pager, blngsPtbId, searchMaterialParams).getRows();
				String path = null;
				if(demandSupplyPlanVO.getPlanTypeCde().getCode()==PlanType.All.getCode()){
					path = request.getServletContext().getRealPath("/excel") + "/"+EXCEL_TEMPLATE_FILENAME_ALL+".xls";
				}else if(demandSupplyPlanVO.getPlanTypeCde().getCode()==PlanType.YEAR.getCode()){
					path = request.getServletContext().getRealPath("/excel") + "/"+EXCEL_TEMPLATE_FILENAME_YEAR+".xls";
				}
				String fileName = demandSupplyPlanVO.getSectionName()+"標段的"+demandSupplyPlanVO.getPlanName()+"物資";
				fileDownloadBean.setFileName(fileName);
				fileDownloadBean.setPath(path);
				fileDownloadBean.setRs(demandSupplyMaterials);
				fileDownloadBean.setExtendsName(".xls");
			} catch (Exception e) {
				log.error("全線年度需求計劃-導出物資報錯。" + e.getMessage(),e);
			}
		return fileDownloadBean;
		}

4、需要的jar文件,以及xls文件

打包zip下載Excel文件 (提取碼:5188)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章