HSSFWorkBooK用法

public ActionResult excelPrint() {    
    HSSFWorkbook workbook = new HSSFWorkbook();// 建立一個Excel文件    
    HSSFSheet sheet = workbook.createSheet();// 建立一個Excel的Sheet   能夠傳一個參數,即sheet的名字 
    sheet.createFreezePane(1, 3);// 凍結    
    // 設置列寬  sheet.setColumnWidth(0, 1000);    
    sheet.setColumnWidth(1, 3500);    
    sheet.setColumnWidth(2, 3500);    
    sheet.setColumnWidth(3, 6500);    
    sheet.setColumnWidth(4, 6500);    
    sheet.setColumnWidth(5, 6500);    
    sheet.setColumnWidth(6, 6500);    
    sheet.setColumnWidth(7, 2500);    
    // Sheet樣式    
    HSSFCellStyle sheetStyle = workbook.createCellStyle();    
    // 背景色的設定    
    sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);    
    // 前景色的設定    
    sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);    
    // 填充模式    
    sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);    
    // 設置列的樣式    
    for (int i = 0; i <= 14; i++) {    
      sheet.setDefaultColumnStyle((short) i, sheetStyle);    
    }    
    // 設置字體    
    HSSFFont headfont = workbook.createFont();    
    headfont.setFontName("黑體");    
    headfont.setFontHeightInPoints((short) 22);// 字體大小    
    headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗    
    // 另外一個樣式    
    HSSFCellStyle headstyle = workbook.createCellStyle();    
    headstyle.setFont(headfont);    
    headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中    
    headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中    
    headstyle.setLocked(true);    
    headstyle.setWrapText(true);// 自動換行    
    // 另外一個字體樣式    
    HSSFFont columnHeadFont = workbook.createFont();    
    columnHeadFont.setFontName("宋體");    
    columnHeadFont.setFontHeightInPoints((short) 10);    
    columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);    
    // 列頭的樣式    
    HSSFCellStyle columnHeadStyle = workbook.createCellStyle();    
    columnHeadStyle.setFont(columnHeadFont);    
    columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中    
    columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中    
    columnHeadStyle.setLocked(true);    
    columnHeadStyle.setWrapText(true);    
    columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左邊框的顏色    
    columnHeadStyle.setBorderLeft((short) 1);// 邊框的大小    
    columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右邊框的顏色    
    columnHeadStyle.setBorderRight((short) 1);// 邊框的大小    
    columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 設置單元格的邊框爲粗體    
    columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 設置單元格的邊框顏色    
    // 設置單元格的背景顏色(單元格的樣式會覆蓋列或行的樣式)    
    columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);    
    
    HSSFFont font = workbook.createFont();    
    font.setFontName("宋體");    
    font.setFontHeightInPoints((short) 10);    
    // 普通單元格樣式    
    HSSFCellStyle style = workbook.createCellStyle();    
    style.setFont(font);    
    style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中    
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中    
    style.setWrapText(true);    
    style.setLeftBorderColor(HSSFColor.BLACK.index);    
    style.setBorderLeft((short) 1);    
    style.setRightBorderColor(HSSFColor.BLACK.index);    
    style.setBorderRight((short) 1);    
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 設置單元格的邊框爲粗體    
    style.setBottomBorderColor(HSSFColor.BLACK.index); // 設置單元格的邊框顏色.    
    style.setFillForegroundColor(HSSFColor.WHITE.index);// 設置單元格的背景顏色.    
    // 另外一個樣式    
    HSSFCellStyle centerstyle = workbook.createCellStyle();    
    centerstyle.setFont(font);    
    centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中    
    centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中    
    centerstyle.setWrapText(true);    
    centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);    
    centerstyle.setBorderLeft((short) 1);    
    centerstyle.setRightBorderColor(HSSFColor.BLACK.index);    
    centerstyle.setBorderRight((short) 1);    
    centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 設置單元格的邊框爲粗體    
    centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 設置單元格的邊框顏色.    
    centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 設置單元格的背景顏色.    
    
    try {    
      // 建立第一行    
      HSSFRow row0 = sheet.createRow(0);    
      // 設置行高    
      row0.setHeight((short) 900);    
      // 建立第一列    
      HSSFCell cell0 = row0.createCell(0);    
      cell0.setCellValue(new HSSFRichTextString("中非發展基金投資項目調度會工做落實狀況對照表"));    
      cell0.setCellStyle(headstyle);    
      /**  
       * 合併單元格  
       *    第一個參數:第一個單元格的行數(從0開始)  
       *    第二個參數:第二個單元格的行數(從0開始)  
       *    第三個參數:第一個單元格的列數(從0開始)  
       *    第四個參數:第二個單元格的列數(從0開始)  
       */    
      CellRangeAddress range = new CellRangeAddress(0, 0, 0, 7);    
      sheet.addMergedRegion(range);    
      // 建立第二行    
      HSSFRow row1 = sheet.createRow(1);    
      HSSFCell cell1 = row1.createCell(0);    
      cell1.setCellValue(new HSSFRichTextString("本次會議時間:2009年8月31日       前次會議時間:2009年8月24日"));    
      cell1.setCellStyle(centerstyle);    
      // 合併單元格    
      range = new CellRangeAddress(1, 2, 0, 7);    
      sheet.addMergedRegion(range);    
      // 第三行    
      HSSFRow row2 = sheet.createRow(3);    
      row2.setHeight((short) 750);    
      HSSFCell cell = row2.createCell(0);    
      cell.setCellValue(new HSSFRichTextString("責任者"));    
      cell.setCellStyle(columnHeadStyle);    
      cell = row2.createCell(1);    
      cell.setCellValue(new HSSFRichTextString("成熟度排序"));    
      cell.setCellStyle(columnHeadStyle);    
      cell = row2.createCell(2);    
      cell.setCellValue(new HSSFRichTextString("事項"));    
      cell.setCellStyle(columnHeadStyle);    
      cell = row2.createCell(3);    
      cell.setCellValue(new HSSFRichTextString("前次會議要求\n/新項目的項目概要"));    
      cell.setCellStyle(columnHeadStyle);    
      cell = row2.createCell(4);    
      cell.setCellValue(new HSSFRichTextString("上週工做進展"));    
      cell.setCellStyle(columnHeadStyle);    
      cell = row2.createCell(5);    
      cell.setCellValue(new HSSFRichTextString("本週工做計劃"));    
      cell.setCellStyle(columnHeadStyle);    
      cell = row2.createCell(6);    
      cell.setCellValue(new HSSFRichTextString("問題和建議"));    
      cell.setCellStyle(columnHeadStyle);    
      cell = row2.createCell(7);    
      cell.setCellValue(new HSSFRichTextString("備 注"));    
      cell.setCellStyle(columnHeadStyle);    
      // 訪問數據庫,獲得數據集    
      List<DeitelVO> deitelVOList = getEntityManager().queryDeitelVOList();    
      int m = 4;    
      int k = 4;    
      for (int i = 0; i < deitelVOList.size(); i++) {    
        DeitelVO vo = deitelVOList.get(i);    
        String dname = vo.getDname();    
        List<Workinfo> workList = vo.getWorkInfoList();    
        HSSFRow row = sheet.createRow(m);    
        cell = row.createCell(0);    
        cell.setCellValue(new HSSFRichTextString(dname));    
        cell.setCellStyle(centerstyle);    
        // 合併單元格    
        range = new CellRangeAddress(m, m + workList.size() - 1, 0, 0);    
        sheet.addMergedRegion(range);    
        m = m + workList.size();    
    
        for (int j = 0; j < workList.size(); j++) {    
          Workinfo w = workList.get(j);    
          // 遍歷數據集建立Excel的行    
          row = sheet.getRow(k + j);    
          if (null == row) {    
            row = sheet.createRow(k + j);    
          }    
          cell = row.createCell(1);    
          cell.setCellValue(w.getWnumber());    
          cell.setCellStyle(centerstyle);    
          cell = row.createCell(2);    
          cell.setCellValue(new HSSFRichTextString(w.getWitem()));    
          cell.setCellStyle(style);    
          cell = row.createCell(3);    
          cell.setCellValue(new HSSFRichTextString(w.getWmeting()));    
          cell.setCellStyle(style);    
          cell = row.createCell(4);    
          cell.setCellValue(new HSSFRichTextString(w.getWbweek()));    
          cell.setCellStyle(style);    
          cell = row.createCell(5);    
          cell.setCellValue(new HSSFRichTextString(w.getWtweek()));    
          cell.setCellStyle(style);    
          cell = row.createCell(6);    
          cell.setCellValue(new HSSFRichTextString(w.getWproblem()));    
          cell.setCellStyle(style);    
          cell = row.createCell(7);    
          cell.setCellValue(new HSSFRichTextString(w.getWremark()));    
          cell.setCellStyle(style);    
        }    
        k = k + workList.size();    
      }    
      // 列尾    
      int footRownumber = sheet.getLastRowNum();    
      HSSFRow footRow = sheet.createRow(footRownumber + 1);    
      HSSFCell footRowcell = footRow.createCell(0);    
      footRowcell.setCellValue(new HSSFRichTextString("                    審  定:XXX      審  核:XXX     匯  總:XX"));    
      footRowcell.setCellStyle(centerstyle);    
      range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0, 7);    
      sheet.addMergedRegion(range);    
    
      HttpServletResponse response = getResponse();    
      HttpServletRequest request = getRequest();    
      String filename = "未命名.xls";//設置下載時客戶端Excel的名稱    
      // 請見:http://zmx.iteye.com/blog/622529    
      filename = Util.encodeFilename(filename, request);    
      response.setContentType("application/vnd.ms-excel");    
      response.setHeader("Content-disposition", "attachment;filename=" + filename);    
      OutputStream ouputStream = response.getOutputStream();    
      workbook.write(ouputStream);    
      ouputStream.flush();    
      ouputStream.close();    
    
    } catch (Exception e) {    
      e.printStackTrace();    
    }    
    return null;    
  }    
 
引自 :http://zmx.iteye.com/blog/622536


2: Util.encodeFilename 這個方法

Java代碼  收藏代碼
package com.tm.ppms.util;  
  
import java.net.URLEncoder;  
  
import javax.servlet.http.HttpServletRequest;  
  
import org.apache.commons.lang.StringUtils;  
  
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;  
  
public class Util {  
    /**  
       * 設置下載文件中文件的名稱  
       *   
       * @param filename  
       * @param request  
       * @return  
       */    
      public static String encodeFilename(String filename, HttpServletRequest request) {    
        /**  
         * 獲取客戶端瀏覽器和操做系統信息  
         * 在IE瀏覽器中獲得的是:User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)  
         * 在Firefox中獲得的是:User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6  
         */    
        String agent = request.getHeader("USER-AGENT");    
        try {    
          if ((agent != null) && (-1 != agent.indexOf("MSIE"))) {    
            String newFileName = URLEncoder.encode(filename, "UTF-8");    
            newFileName = StringUtils.replace(newFileName, "+", "%20");    
            if (newFileName.length() > 150) {    
              newFileName = new String(filename.getBytes("GB2312"), "ISO8859-1");    
              newFileName = StringUtils.replace(newFileName, " ", "%20");    
            }    
            return newFileName;    
          }    
          if ((agent != null) && (-1 != agent.indexOf("Mozilla")))    
            return MimeUtility.encodeText(filename, "UTF-8", "B");    
        
          return filename;    
        } catch (Exception ex) {    
          return filename;    
        }    
      }    
}
相關文章
相關標籤/搜索