1.項目組負責人讓我實現這個接口,由於之前作過相似的,中間並無遇到什麼太困難的事情。其餘不說,先上代碼:java
1 package com.tydic.eshop.action.feedback; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.FileInputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.text.SimpleDateFormat; 9 import java.util.Date; 10 import java.util.List; 11 12 import javax.annotation.Resource; 13 import javax.servlet.http.HttpServletRequest; 14 import org.apache.struts2.ServletActionContext; 15 16 import com.tydic.eshop.dto.feedback.vo.FeedBackVO; 17 import com.tydic.eshop.model.feedback.FeedBack; 18 import com.tydic.eshop.service.feedback.FeedBackService; 19 import com.tydic.eshop.util.feedback.ExportExcelUtils; 20 import com.tydic.framework.base.dataobject.Pagination; 21 import com.tydic.framework.base.dataobject.ResultDto; 22 import com.tydic.framework.base.exception.ServiceException; 23 import com.tydic.framework.base.web.struts.BaseSupportAction; 24 25 /** 26 * @ClassName: FeedBackAction 27 * @Description: ECP後臺增長意見日誌導出功能 28 * @author liuren 29 * @date 2016年3月25日 30 * 31 */ 32 public class FeedBackAction extends BaseSupportAction{ 33 34 private static final long serialVersionUID = 5684835836631112914L; 35 @Resource(name = "feedBackService") 36 private FeedBackService feedbackService; 37 38 private FeedBack feedback; 39 private FeedBackVO feedbackVO; 40 41 private List<FeedBack> feedbackList; 42 43 private String feedbackId; 44 45 private ResultDto<FeedBack> feedbackPage; 46 47 private InputStream excelStream; //輸出流變量 48 private String excelFileName; //下載文件名 49 /** 50 * 總頁數 51 */ 52 private int totalPage; 53 private FileInputStream stream; 54 55 public String execute() throws Exception { 56 //這裏可加入權限控制 57 return "downloadsuccess"; 58 } 59 /** 60 * 意見反饋查詢 61 * @return 62 * @throws Exception 63 */ 64 public String queryFeedBackList() throws ServiceException{ 65 if(feedbackVO == null){ 66 feedbackVO = new FeedBackVO(); 67 } 68 HttpServletRequest request = ServletActionContext.getRequest(); 69 String currentPage = request.getParameter("index"); 70 int currentPagei=0; 71 if(currentPage!=null){ 72 currentPagei = Integer.parseInt(currentPage); 73 } 74 Pagination pagination = new Pagination(currentPagei); 75 String pagesize = request.getParameter("pageSize"); 76 if(pagesize==null){ 77 pagesize="10"; 78 } 79 pagination.setPageSize(Integer.parseInt(pagesize)); 80 81 feedbackVO.setPagination(pagination); 82 try { 83 feedbackPage = feedbackService.findFeedBackPage(feedbackVO); 84 totalPage = feedbackPage.getPagination().getTotalPage(); 85 } catch (Exception e) { 86 throw new ServiceException("數據分頁查詢失敗"+e.getMessage()); 87 } 88 return "toFeedBackListPage"; 89 } 90 91 /** 92 * @throws IOException 93 * @throws 94 * @Title: exportExecl 95 * @Description: 報表導出並下載 96 * @param @return 97 * @param @throws ServiceException 參數 98 * @return String 返回類型 99 * @throws 100 */ 101 public String exportExecl() throws ServiceException, IOException{ 102 ExportExcelUtils<FeedBack> ex = new ExportExcelUtils<FeedBack>(); 103 Date fileDate = new Date(); 104 System.out.println(fileDate); 105 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss"); 106 String ds = sdf.format(fileDate); 107 String fileName = "意見反饋報表"+ds+".xls"; //生成的文件名稱 108 System.out.println(fileName); 109 String exportPath = request.getSession().getServletContext().getRealPath("/WEB-INF/download/"+fileName); 110 String[] headers = { "序號", "渠道標示", "手機號碼", "終端機型", "反饋信息內容", "建立時間", "圖片地址", "版本號", "狀態" }; 111 if (exportPath==null||exportPath==""||exportPath==" ") { 112 throw new ServiceException("請選擇導出目錄"); 113 } 114 try { 115 feedbackList = feedbackService.findFeedBackList(feedbackVO); 116 } catch (Exception e) { 117 e.printStackTrace(); 118 throw new ServiceException("數據查詢失敗"+e.getMessage()); 119 } 120 121 ByteArrayOutputStream out = new ByteArrayOutputStream(); 122 ByteArrayOutputStream os = ex.exportExcel("海航通訊意見反饋報表",headers, feedbackList, out,"yyyy/MM/dd HH:mm:ss"); 123 124 byte[] fileContent = os.toByteArray(); 125 ByteArrayInputStream is = new ByteArrayInputStream(fileContent); 126 127 excelStream = is; //文件流 128 excelFileName = new String(fileName.getBytes(), "ISO8859-1"); //設置下載的文件名 129 System.out.println(excelFileName); 130 131 return "successdownload"; 132 } 133 /** 134 * @Title: queryFeedBackById 135 * @Description: 根據意見反饋Id查詢意見反饋詳情 136 * @param @return 137 * @param @throws ServiceException 參數 138 * @return String 返回類型 139 * @throws 140 */ 141 public String queryFeedBackById() throws ServiceException{ 142 feedback = feedbackService.queryFeedBackById(feedbackId); 143 return "toFeedBackDetailPage"; 144 } 145 146 public FeedBackVO getFeedbackVO() { 147 return feedbackVO; 148 } 149 public void setFeedbackVO(FeedBackVO feedbackVO) { 150 this.feedbackVO = feedbackVO; 151 } 152 public List<FeedBack> getFeedbackList() { 153 return feedbackList; 154 } 155 public void setFeedbackList(List<FeedBack> feedbackList) { 156 this.feedbackList = feedbackList; 157 } 158 public String getFeedbackId() { 159 return feedbackId; 160 } 161 public void setFeedbackId(String feedbackId) { 162 this.feedbackId = feedbackId; 163 } 164 165 public ResultDto<FeedBack> getFeedbackPage() { 166 return feedbackPage; 167 } 168 public void setFeedbackPage(ResultDto<FeedBack> feedbackPage) { 169 this.feedbackPage = feedbackPage; 170 } 171 public int getTotalPage() { 172 return totalPage; 173 } 174 public void setTotalPage(int totalPage) { 175 this.totalPage = totalPage; 176 } 177 public FeedBack getFeedback() { 178 return feedback; 179 } 180 public void setFeedback(FeedBack feedback) { 181 this.feedback = feedback; 182 } 183 public InputStream getExcelStream() { 184 return excelStream; 185 } 186 public void setExcelStream(InputStream excelStream) { 187 this.excelStream = excelStream; 188 } 189 public String getExcelFileName() { 190 return excelFileName; 191 } 192 public void setExcelFileName(String excelFileName) { 193 this.excelFileName = excelFileName; 194 } 195 public FileInputStream getStream() { 196 return stream; 197 } 198 public void setStream(FileInputStream stream) { 199 this.stream = stream; 200 } 201 202 203 }
2.而後是struts的配置文件 struts.xml中action的配置web
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <package name="feedback" extends="default" namespace="/feedback"> 8 9 <action name="feedBackAction" class="feedBackAction" method="initFeedBack"> 10 <result name="success" type="redirect">feedBackAction!queryFeedBackList.do</result> 11 <result name="toFeedBackDetailPage">/jsp/feedback/feedBackDetail.jsp</result> 12 <result name="toFeedBackListPage">/jsp/feedback/feedBackList.jsp</result> 13 <result name="successdownload" type="stream"> 14 <!-- 下載文件的類型,若是你不知道是什麼格式,能夠去 tomcat\conf\web.xml下找 --> 15 <param name="contentType">application/vnd.ms-excel</param> 16 <!-- 返回流 excelStream爲action中的流變量名稱 --> 17 <param name="inputName">excelStream</param> 18 <!-- attachment 這個位置的參數挺特殊的,能夠設置成下載時,是否出現個下載提示框,或者直接下載之類的。 19 fileName指定生成的文件名字(適合動態生成文件名,好比作報表時,通常都要說是幾月的統計數據之類)爲action中變量--> 20 <param name="contentDisposition"> 21 attachment;filename=${excelFileName} 22 </param> 23 <param name="bufferSize">1024</param> 24 </result> 25 </action> 26 </package> 27 28 </struts>
3.而後是導出報表的工具類正則表達式
1 package com.tydic.eshop.util.feedback; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 import java.io.OutputStream; 6 import java.lang.reflect.Field; 7 import java.lang.reflect.InvocationTargetException; 8 import java.lang.reflect.Method; 9 import java.text.SimpleDateFormat; 10 import java.util.Collection; 11 import java.util.Date; 12 import java.util.Iterator; 13 import java.util.regex.Matcher; 14 import java.util.regex.Pattern; 15 16 import org.apache.poi.hssf.usermodel.HSSFCell; 17 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 18 import org.apache.poi.hssf.usermodel.HSSFClientAnchor; 19 import org.apache.poi.hssf.usermodel.HSSFComment; 20 import org.apache.poi.hssf.usermodel.HSSFFont; 21 import org.apache.poi.hssf.usermodel.HSSFPatriarch; 22 import org.apache.poi.hssf.usermodel.HSSFRichTextString; 23 import org.apache.poi.hssf.usermodel.HSSFRow; 24 import org.apache.poi.hssf.usermodel.HSSFSheet; 25 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 26 import org.apache.poi.hssf.util.HSSFColor; 27 28 /** 29 * @ClassName: ExportExcel 30 * @Description: 利用開源組件POI3.0.2動態導出EXCEL文檔 轉載時請保留如下信息,註明出處! 31 * @author liuren 32 * @date 2016年3月28日 33 * 34 * @param <T> 35 * 應用泛型,表明任意一個符合javabean風格的類 36 * 注意這裏爲了簡單起見,boolean型的屬性xxx的get器方式爲getXxx(),而不是isXxx() 37 * byte[]表jpg格式的圖片數據 38 */ 39 public class ExportExcelUtils<T> { 40 41 public void exportExcel(Collection<T> dataset, ByteArrayOutputStream out) { 42 this.exportExcel("意見反饋報表", null, dataset, out, "yyyy-MM-dd"); 43 } 44 45 public void exportExcel(String[] headers, Collection<T> dataset, ByteArrayOutputStream out) { 46 this.exportExcel("意見反饋報表", headers, dataset, out, "yyyy-MM-dd"); 47 } 48 49 public void exportExcel(String[] headers, Collection<T> dataset, ByteArrayOutputStream out, String pattern) { 50 this.exportExcel("意見反饋報表", headers, dataset, out, pattern); 51 } 52 /** 53 * 這是一個通用的方法,利用了JAVA的反射機制,能夠將放置在JAVA集合中而且符號必定條件的數據以EXCEL 的形式輸出到指定IO設備上 54 * 55 * @param title 56 * 表格標題名 57 * @param headers 58 * 表格屬性列名數組 59 * @param dataset 60 * 須要顯示的數據集合,集合中必定要放置符合javabean風格的類的對象。此方法支持的 61 * javabean屬性的數據類型有基本數據類型及String,Date,byte[](圖片數據) 62 * @param out 63 * 與輸出設備關聯的流對象,能夠將EXCEL文檔導出到本地文件或者網絡中 64 * @param pattern 65 * 若是有時間數據,設定輸出格式。默認爲"yyy-MM-dd" 66 */ 67 public ByteArrayOutputStream exportExcel(String title, String[] headers, Collection<T> dataset, ByteArrayOutputStream out, String pattern) { 68 // 聲明一個工做薄 69 HSSFWorkbook workbook = new HSSFWorkbook(); 70 // 生成一個表格 71 HSSFSheet sheet = workbook.createSheet(title); 72 // 設置表格默認列寬度爲15個字節 73 sheet.setDefaultColumnWidth(15); 74 // 生成一個樣式 75 HSSFCellStyle style = workbook.createCellStyle(); 76 // 設置這些樣式 77 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); 78 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 79 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); 80 style.setBorderLeft(HSSFCellStyle.BORDER_THIN); 81 style.setBorderRight(HSSFCellStyle.BORDER_THIN); 82 style.setBorderTop(HSSFCellStyle.BORDER_THIN); 83 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 84 // 生成一個字體 85 HSSFFont font = workbook.createFont(); 86 font.setColor(HSSFColor.VIOLET.index); 87 font.setFontHeightInPoints((short) 12); 88 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 89 // 把字體應用到當前的樣式 90 style.setFont(font); 91 // 生成並設置另外一個樣式 92 HSSFCellStyle style2 = workbook.createCellStyle(); 93 style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); 94 style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 95 style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); 96 style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); 97 style2.setBorderRight(HSSFCellStyle.BORDER_THIN); 98 style2.setBorderTop(HSSFCellStyle.BORDER_THIN); 99 style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); 100 style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 101 // 生成另外一個字體 102 HSSFFont font2 = workbook.createFont(); 103 font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); 104 // 把字體應用到當前的樣式 105 style2.setFont(font2); 106 107 // 聲明一個畫圖的頂級管理器 108 HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); 109 // 定義註釋的大小和位置,詳見文檔 110 HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5)); 111 // 設置註釋內容 112 // comment.setString(new HSSFRichTextString("能夠在POI中添加註釋!")); 113 // 設置註釋做者,當鼠標移動到單元格上是能夠在狀態欄中看到該內容. 114 comment.setAuthor("海航通訊"); 115 116 // 產生表格標題行 117 HSSFRow row = sheet.createRow(0); 118 for (int i = 0; i < headers.length; i++) { 119 HSSFCell cell = row.createCell(i); 120 cell.setCellStyle(style); 121 HSSFRichTextString text = new HSSFRichTextString(headers[i]); 122 cell.setCellValue(text); 123 } 124 125 // 遍歷集合數據,產生數據行 126 Iterator<T> it = dataset.iterator(); 127 int index = 0; 128 while (it.hasNext()) { 129 index++; 130 row = sheet.createRow(index); 131 T t = (T) it.next(); 132 // 利用反射,根據javabean屬性的前後順序,動態調用getXxx()方法獲得屬性值 133 Field[] fields = t.getClass().getDeclaredFields(); 134 for (int i = 0; i < fields.length; i++) { 135 HSSFCell cell = row.createCell(i); 136 cell.setCellStyle(style2); 137 Field field = fields[i]; 138 String fieldName = field.getName(); 139 String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); 140 try { 141 Class tCls = t.getClass(); 142 Method getMethod = tCls.getMethod(getMethodName, new Class[] {}); 143 Object value = getMethod.invoke(t, new Object[] {}); 144 // 判斷值的類型後進行強制類型轉換 145 String textValue = null; 146 if (value==null) { 147 value=""; 148 } 149 if (value instanceof Boolean) { 150 boolean bValue = (Boolean) value; 151 textValue = "男"; 152 if (!bValue) { 153 textValue = "女"; 154 } 155 } else if (value instanceof Date) { 156 Date date = (Date) value; 157 SimpleDateFormat sdf = new SimpleDateFormat(pattern); 158 textValue = sdf.format(date); 159 } else if (value instanceof byte[]) { 160 // 有圖片時,設置行高爲60px; 161 row.setHeightInPoints(60); 162 // 設置圖片所在列寬度爲80px,注意這裏單位的一個換算 163 sheet.setColumnWidth(i, (short) (35.7 * 80)); 164 // sheet.autoSizeColumn(i); 165 byte[] bsValue = (byte[]) value; 166 HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 6, index, (short) 6, 167 index); 168 anchor.setAnchorType(2); 169 patriarch.createPicture(anchor, workbook.addPicture(bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG)); 170 } else { 171 // 其它數據類型都看成字符串簡單處理 172 textValue = value.toString(); 173 } 174 // 若是不是圖片數據,就利用正則表達式判斷textValue是否所有由數字組成 175 if (textValue != null) { 176 Pattern p = Pattern.compile("^\\d+(\\.\\d+)?$"); 177 Matcher matcher = p.matcher(textValue); 178 if (matcher.matches()) { 179 // 是數字看成double處理 180 cell.setCellValue(Double.parseDouble(textValue)); 181 } else { 182 HSSFRichTextString richString = new HSSFRichTextString(textValue); 183 HSSFFont font3 = workbook.createFont(); 184 font3.setColor(HSSFColor.BLUE.index); 185 richString.applyFont(font3); 186 cell.setCellValue(richString); 187 } 188 } 189 } catch (SecurityException e) { 190 e.printStackTrace(); 191 } catch (NoSuchMethodException e) { 192 e.printStackTrace(); 193 } catch (IllegalArgumentException e) { 194 e.printStackTrace(); 195 } catch (IllegalAccessException e) { 196 e.printStackTrace(); 197 } catch (InvocationTargetException e) { 198 e.printStackTrace(); 199 } finally { 200 // 清理資源 201 } 202 } 203 204 } 205 try { 206 workbook.write(out); 207 } catch (IOException e) { 208 e.printStackTrace(); 209 } 210 return out; 211 } 212 }
4.而後是須要導入的poi架包,本人是用Maven作的項目,導入POI的包,只須要導入下面這一個包就好了。apache
1 <!-- 導入poi包 --> 2 <dependency> 3 <groupId>org.apache.poi</groupId> 4 <artifactId>poi-ooxml</artifactId> 5 <version>3.9</version> 6 </dependency>
5.而後就是效果圖兩張數組
導出的excel的內容:tomcat