action中的方法 /** * Excel文件下載處理 * @return
*/ @RequestMapping("/downloanExcel") public ModelAndView downloanExcel(){ List<AuContract> list = new ArrayList<AuContract>(); list= service.findAuContractList();//獲得數據庫所有的合同集合
Map<String,List<AuContract>> map = new HashMap<String, List<AuContract>>(); map.put("infoList", list); ExcelView ve = new ExcelView(); return new ModelAndView(ve,map); } excel操作工具類 ExcelView package com.ekyb.common.auContract.action; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.springframework.web.servlet.view.document.AbstractExcelView; import com.ekyb.common.auContract.entity.AuContract; import com.ekyb.common.util.CharEncodingEdit; /** * 下載Excel視圖 * * @author gx */
public class ExcelView extends AbstractExcelView { @SuppressWarnings("static-access") protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { @SuppressWarnings("unchecked") List<AuContract> list = (List<AuContract>) model.get("infoList"); HSSFFont font= workbook.createFont(); font.setFontHeightInPoints((short)12); //設置字體的大小
font.setFontName("微軟雅黑"); //設置字體的樣式,如:宋體、微軟雅黑等
font.setItalic(false); //斜體true為斜體
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //對文中進行加粗
font.setColor(HSSFColor.PINK.index); //設置字體的顏色
HSSFCellStyle style = workbook.createCellStyle(); style.setFont(font); if (list != null && list.size() != 0) { int length = list.size(); Sheet sheet = workbook.createSheet(); // 第一行文字說明
Row row = sheet.createRow(0); Cell cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(style); cell.setCellValue("合同名稱"); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(style); cell.setCellValue("合同單位"); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(style); cell.setCellValue("合同登記時間"); cell = row.createCell(3, Cell.CELL_TYPE_STRING); cell.setCellStyle(style); cell.setCellValue("合同金額"); cell = row.createCell(4, Cell.CELL_TYPE_STRING); cell.setCellStyle(style); cell.setCellValue("履行方式"); cell = row.createCell(5, Cell.CELL_TYPE_STRING); cell.setCellStyle(style); cell.setCellValue("合同類型"); cell = row.createCell(6, Cell.CELL_TYPE_STRING); cell.setCellStyle(style); cell.setCellValue("開始時間"); cell = row.createCell(7, Cell.CELL_TYPE_STRING); cell.setCellStyle(style); cell.setCellValue("結束時間"); cell = row.createCell(8, Cell.CELL_TYPE_STRING); cell.setCellStyle(style); cell.setCellValue("備注"); // 下面是具體內容
for (int i = 0; i < length; i++) { sheet.setColumnWidth((short) i, (short) (35.7 * 100)); row = sheet.createRow(i + 1); // 合同名稱
cell = row.createCell(0, cell.CELL_TYPE_STRING); cell.setCellValue(list.get(i).getName()); // 合同單位
cell = row.createCell(1, cell.CELL_TYPE_STRING); cell.setCellValue(list.get(i).getUnit()); // 合同登記時間
cell = row.createCell(2, cell.CELL_TYPE_STRING); cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(list.get(i).getReg_time())); // 合同金額
cell = row.createCell(3, cell.CELL_TYPE_STRING); //把float對象轉換為String對象
float con_money=list.get(i).getCon_money(); String str =String.valueOf(con_money); cell.setCellValue(str); // 合同履行方式
cell = row.createCell(4, cell.CELL_TYPE_STRING); cell.setCellValue(list.get(i).getPerform_style()); // 合同類型
cell = row.createCell(5, cell.CELL_TYPE_STRING); cell.setCellValue(list.get(i).getCon_type()); // 開始時間
cell = row.createCell(6, cell.CELL_TYPE_STRING); cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(list.get(i).getCon_start_time())); // 結束時間
cell = row.createCell(7, cell.CELL_TYPE_STRING); cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(list.get(i).getCon_end_time())); // 備注
cell = row.createCell(8, cell.CELL_TYPE_STRING); cell.setCellValue(list.get(i).getRemark()); } //web瀏覽通過MIME類型判斷文件是excel類型
response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setCharacterEncoding("utf-8"); // 對文件名進行處理。防止文件名亂碼
String fileName = CharEncodingEdit.processFileName(request, "合同.xls"); // Content-disposition屬性設置成以附件方式進行下載
response.setHeader("Content-disposition", "attachment;filename=" + fileName); OutputStream os = response.getOutputStream(); workbook.write(os); os.flush(); os.close(); } } } jsp頁面代碼用的是jQueryUI <a href="<%=basePath%>/auContract/downloanExcel" class="easyui-linkbutton"data-options="plain:'true',iconCls:'icon-down'">下載Excel</a>
//可以直接通過a標簽的href直接連接到action中的url映射地址