java 導出並下載excel

使用框架:jxl   官網:http://www.andykhan.com/jexcelapi/download.html

action代碼:

		//
		if("export".equals(option)){
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			String [] headers=new String[]{"地區ID","地區編碼","地區名稱","簡名","拼音","備註"};
			try {
				WritableWorkbook workbook = Workbook.createWorkbook(os);
				WritableSheet sheet = workbook.createSheet("Sheet1", 0);
				//組件列名
				for(int i=0;i<headers.length;i++){
					Label label = new Label(i, 0, headers[i]);
					sheet.addCell(label);
				}
				//組件數據
				for(int i=0;i<areas.size();i++){
					Label labelAreaId = new Label(0, i+1, areas.get(i).getAreaId());
					sheet.addCell(labelAreaId);
					Label labelAreaCode = new Label(1, i+1, areas.get(i).getAreaCode());
					sheet.addCell(labelAreaCode);
					Label labelAreaName = new Label(2, i+1, areas.get(i).getAreaName());
					sheet.addCell(labelAreaName);
					Label labelAreaSimpleName = new Label(3, i+1, areas.get(i).getAreaSimpleName());
					sheet.addCell(labelAreaSimpleName);
					Label labelAreaPinyin = new Label(4, i+1, areas.get(i).getAreaPinyin());
					sheet.addCell(labelAreaPinyin);
					Label labelAreaComment = new Label(5, i+1, areas.get(i).getAreaComment());
					sheet.addCell(labelAreaComment);
				}
				workbook.write();
				workbook.close();
				exportExcelFileName=new String((DateUtil.dateToStr(new Date(), "yyyyMMddHHmmss")+"_地區.xls").getBytes(), "ISO8859-1");
				inputStream = new ByteArrayInputStream(os.toByteArray());
			}catch(Exception e){
				e.printStackTrace();
				throw new ErpRuntimeException("system_export_error_message");
			}
			return option;
		}

struts.xml配置

<result name="export" type="stream">
			    <param name="contentType">application/vnd.ms-excel</param>
			    <param name="inputName">inputStream</param>
			    <param name="contentDisposition">attachment;filename="${exportExcelFileName}"</param>
			    <param name="bufferSize">1024</param>
			 </result>



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