熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

使用java生成excel功能實現

2022-06-13   來源: Java核心技術 

  jsp頁面

  發送請求地址sp_createExceldo

  public String createExcel() {
  try {
   HttpServletResponse response = ServletActionContextgetResponse();
   OutputStream os = responsegetOutputStream();
   responsereset();
   responsesetContentType(application/msexcel;charset=gbk);  //設置響應格式

  //設置響應頭即報表頭
   responsesetHeader(Contentdisposition attachment;filename= + new String(業務受理量統計報表getBytes() iso) + xls);

  HSSFWorkbook hwb = new HSSFWorkbook();
   HSSFSheet sheet = hwbcreateSheet(new Sheet);
   hwbsetSheetName( 業務受理量統計);

  HSSFRow row = sheetcreateRow((short) );
   sheetcreateFreezePane( );//設置第一行顯示內容
   createCell(hwb row (short) 業務類型);
   createCell(hwb row (short) 操作名稱);
   createCell(hwb row (short) 數量);
   createCell(hwb row (short) 收取金額);
   createCell(hwb row (short) 工號);

  //busBeans列表為要顯示的數據

  for (int i = ; i <= busBeanssize(); i++) {
    HSSFRow row = sheetcreateRow((short) i);
    for (int j = ; j < ; j++) {
     if (j == ) {
      createCell(hwb row (short) j busBeansget(i )getStatName());
     } else if (j == ) {
      createCell(hwb row (short) j busBeansget(i )getFunName());
     } else if (j == ) {
      createCell(hwb row (short) j busBeansget(i )getCountNum());
     } else if (j == ) {
      createCell(hwb row (short) j busBeansget(i )getMoney());
     } else if (j == ) {
      createCell(hwb row (short) j busBeansget(i )getLoginNo());
     }
    }
   hwbwrite(os);
   osclose();
   return createSuccess;
  } catch (Throwable e) {
   eprintStackTrace();
   thisforwardError(egetMessage());
  }
  return fail;
 }

  public void createCell(HSSFWorkbook wb HSSFRow row short col String val) {
  HSSFCell cell = rowcreateCell(col);
  cellsetCellValue(val);
  HSSFCellStyle cellStyle = wbcreateCellStyle();
  cellStylesetAlignment(HSSFCellStyleALIGN_CENTER_SELECTION);
  cellsetCellStyle(cellStyle);
 }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26089.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.