package com.thinkgem.jeesite.modules.publicoption.util; import java.util.List; import java.util.regex.Pattern; import javax.servlet.ServletOutputStream; import com.thinkgem.jeesite.modules.publicoption.entity.PublicOpinion; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.util.CellRangeAddress; public class ExcelUtil { private final static String regxpForHtml = "<([^>]*)>"; // 過濾全部以<開頭以>結尾的標籤 private final static String regxpForImgTag = "<\\s*img\\s+([^>]*)\\s*>"; // 找出IMG標籤 private final static String regxpForImaTagSrcAttrib = "src=\"([^\"]+)\""; // 找出IMG標籤的SRC屬性 /** * 導出用戶的全部列表到excel * * [@param](http://my.oschina.net/u/2303379) outputStream 輸出流 */ public void exportPublicOpinionExcel(List<PublicOpinion> list, ServletOutputStream outputStream) { try { //一、建立工做簿 HSSFWorkbook workbook = new HSSFWorkbook(); //1.一、建立合併單元格對象 // CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 6);//起始行號,結束行號,起始列號,結束列號 //1.二、頭標題樣式 //HSSFCellStyle style1 = createCellStyle(workbook, (short)16); //1.三、列標題樣式 HSSFCellStyle style2 = createCellStyle(workbook, (short)13); //二、建立工做表 HSSFSheet sheet = workbook.createSheet("回覆狀況"); //2.一、加載合併單元格對象 //sheet.addMergedRegion(cellRangeAddress); //設置默認列寬 sheet.setDefaultColumnWidth(30); //三、建立行 //3.一、建立頭標題行;而且設置頭標題 // HSSFRow row1 = sheet.createRow(0); //HSSFCell cell1 = row1.createCell(0); //加載單元格樣式 // cell1.setCellStyle(style1); // cell1.setCellValue("回覆狀況"); //3.二、建立列標題行;而且設置列標題 HSSFRow row2 = sheet.createRow(0); String[] titles = {"編號","領導是否批示", "標題", "日期", "來源" ,"部門","相關連接","回覆"}; for(int i = 0; i < titles.length; i++){ HSSFCell cell2 = row2.createCell(i); //加載單元格樣式 cell2.setCellStyle(style2); cell2.setCellValue(titles[i]); } //四、操做單元格;將用戶列表寫入excel if(list != null){ for(int j = 0; j < list.size(); j++){ HSSFRow row = sheet.createRow(j+1); HSSFCell cell11 = row.createCell(0); cell11.setCellValue(j+1); HSSFCell cell12 = row.createCell(1); cell12.setCellValue(""); HSSFCell cell13 = row.createCell(2); cell13.setCellValue(list.get(j).getTitle()); HSSFCell cell14 = row.createCell(3); cell14.setCellValue(list.get(j).getReleaseTime()); HSSFCell cell15 = row.createCell(4); cell15.setCellValue(list.get(j).getCollectOrigin()); HSSFCell cell16 = row.createCell(5); cell16.setCellValue(list.get(j).getInvolveTownName()); HSSFCell cell7 = row.createCell(6); cell7.setCellValue(list.get(j).getOriginUrl()); HSSFCell cell8 = row.createCell(7); cell8.setCellValue(Html2Text(list.get(j).getReplyContent())); } } //五、輸出 workbook.write(outputStream); outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 建立單元格樣式 * [@param](http://my.oschina.net/u/2303379) workbook 工做簿 * [@param](http://my.oschina.net/u/2303379) fontSize 字體大小 * [@return](http://my.oschina.net/u/556800) 單元格樣式 */ private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontSize) { HSSFCellStyle style = workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 //建立字體 HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗字體 font.setFontHeightInPoints(fontSize); //加載字體 style.setFont(font); return style; } public static String Html2Text(String inputString) { String htmlStr = inputString; // 含html標籤的字符串 String textStr = ""; java.util.regex.Pattern p_script; java.util.regex.Matcher m_script; java.util.regex.Pattern p_style; java.util.regex.Matcher m_style; java.util.regex.Pattern p_html; java.util.regex.Matcher m_html; try { String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定義script的正則表達式{或<script[^>]*?>[\\s\\S]*?<\\/script> // } String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定義style的正則表達式{或<style[^>]*?>[\\s\\S]*?<\\/style> // } String regEx_html = "<[^>]+>"; // 定義HTML標籤的正則表達式 inputString = inputString.replaceAll("\\r\\n\\t",""); p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); m_script = p_script.matcher(htmlStr); htmlStr = m_script.replaceAll(""); // 過濾script標籤 p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); m_style = p_style.matcher(htmlStr); htmlStr = m_style.replaceAll(""); // 過濾style標籤 p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); m_html = p_html.matcher(htmlStr); htmlStr = m_html.replaceAll(""); // 過濾html標籤 textStr = htmlStr; } catch (Exception e) { System.err.println("Html2Text: " + e.getMessage()); } return textStr;// 返回文本字符串 } }