Rising Sun

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            148 隨筆 :: 0 文章 :: 22 評論 :: 0 Trackbacks
          在導出數據生成excel時,定義excel單元格格式為文本。
          最好拿你的導出代碼貼出來看看,主要是生成excel時代碼

          --------------------------------------------------------------------------------

          參考一下.
          <% @?page?contentType = " text/html;?charset=gb2312 " %>
          <% @?page? import = " java.io.* " %>
          <% @?page? import = " org.apache.poi.hssf.util.HSSFColor " %>
          <% @?page? import = " org.apache.poi.hssf.util.Region " %>
          <% @?page? import = " org.apache.poi.hssf.usermodel.* " %>
          <%
          // 初始化
          // 工作簿
          HSSFWorkbook?wb = new ?HSSFWorkbook();
          // 工作表
          HSSFSheet?sheet = wb.createSheet();
          wb.setSheetName(
          0 , " Excel演示! " ,HSSFWorkbook.ENCODING_UTF_16);
          // 準備完成
          // 建樣式
          // 標題字
          HSSFFont?font_Header = wb.createFont();
          font_Header.setFontName(
          " headerFont " );
          font_Header.setFontHeightInPoints((
          short ) 12 );
          HSSFCellStyle?cellStyle_Header
          = wb.createCellStyle();
          cellStyle_Header.setAlignment(HSSFCellStyle.ALIGN_CENTER);
          cellStyle_Header.setFont(font_Header);
          // 通用行
          HSSFCellStyle?cellStyle_Normal = wb.createCellStyle();
          cellStyle_Normal.setAlignment(HSSFCellStyle.ALIGN_LEFT);
          // cellStyle_Normal.setBorderBottom(HSSFCellStyle.BORDER_THIN);
          // cellStyle_Normal.setBorderLeft(HSSFCellStyle.BORDER_THIN);
          // cellStyle_Normal.setBorderRight(HSSFCellStyle.BORDER_THIN);
          // cellStyle_Normal.setBorderTop(HSSFCellStyle.BORDER_THIN);
          // 表格頭
          HSSFCellStyle?cellStyle_Column = wb.createCellStyle();
          cellStyle_Column.setAlignment(HSSFCellStyle.ALIGN_CENTER);
          cellStyle_Column.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
          cellStyle_Column.setBorderBottom(HSSFCellStyle.BORDER_THIN);
          cellStyle_Column.setBorderLeft(HSSFCellStyle.BORDER_THIN);
          cellStyle_Column.setBorderRight(HSSFCellStyle.BORDER_THIN);
          cellStyle_Column.setBorderTop(HSSFCellStyle.BORDER_THIN);
          cellStyle_Column.setFillPattern(HSSFCellStyle.BIG_SPOTS);
          cellStyle_Column.setFillBackgroundColor((
          short )HSSFColor.WHITE.index);
          cellStyle_Column.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
          // 數據行
          HSSFCellStyle?cellStyle_Cell = wb.createCellStyle();
          cellStyle_Cell.setAlignment(HSSFCellStyle.ALIGN_LEFT);
          cellStyle_Cell.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
          cellStyle_Cell.setBorderBottom(HSSFCellStyle.BORDER_THIN);
          cellStyle_Cell.setBorderLeft(HSSFCellStyle.BORDER_THIN);
          cellStyle_Cell.setBorderRight(HSSFCellStyle.BORDER_THIN);
          cellStyle_Cell.setBorderTop(HSSFCellStyle.BORDER_THIN);
          cellStyle_Cell.setWrapText(
          true );
          // 樣式結束
          // 置標題
          HSSFRow?row = sheet.createRow(( short ) 0 );
          HSSFCell?cell
          = row.createCell(( short ) 0 );
          cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
          cell.setCellValue(
          " Excel演示! " );
          sheet.addMergedRegion(
          new ?Region( 0 ,( short ) 0 , 0 ,( short ) 5 ));
          cell.setCellStyle(cellStyle_Header);
          // 完成標題
          // 行信息
          row = sheet.createRow(( short ) 1 );
          cell
          = row.createCell(( short ) 0 );
          cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
          cell.setCellValue(
          " FieldName " );
          cell.setCellStyle(cellStyle_Normal);
          cell
          = row.createCell(( short ) 1 );
          cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
          cell.setCellValue(
          " FieldValue " );
          sheet.addMergedRegion(
          new ?Region( 1 ,( short ) 1 , 1 ,( short ) 2 ));
          cell.setCellStyle(cellStyle_Normal);
          cell
          = row.createCell(( short ) 3 );
          cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
          cell.setCellValue(
          " FieldName " );
          cell.setCellStyle(cellStyle_Normal);
          cell
          = row.createCell(( short ) 4 );
          cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
          cell.setCellValue(
          " FieldValue " );
          sheet.addMergedRegion(
          new ?Region( 1 ,( short ) 4 , 1 ,( short ) 5 ));
          cell.setCellStyle(cellStyle_Normal);
          // 表數據
          for ( int ?mIndex = 2 ;mIndex < 10 ;mIndex ++ )
          {
          row
          = sheet.createRow(( short )mIndex);
          for ( int ?nIndex = 0 ;nIndex < 6 ;nIndex ++ )
          {
          cell
          = row.createCell(( short )nIndex);
          cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
          cell.setCellValue(
          " 00.00 " );
          cell.setCellStyle(cellStyle_Cell);
          }

          }

          // 全局調
          sheet.setHorizontallyCenter( true );
          for ( int ?kIndex = 0 ;kIndex < 10 ;kIndex ++ )
          {
          sheet.setColumnWidth((
          short )kIndex,( short ) 4500 );
          }

          sheet.setMargin(HSSFSheet.BottomMargin,(
          double ) 0.5 );
          sheet.setMargin(HSSFSheet.LeftMargin,(
          double ) 0.1 );
          sheet.setMargin(HSSFSheet.RightMargin,(
          double ) 0.1 );
          sheet.setMargin(HSSFSheet.TopMargin,(
          double ) 0.5 );
          // 調整結束
          // 輸出Excel
          OutputStream?outData = null ;
          outData
          = response.getOutputStream();
          response.setContentType(
          " application/vnd.ms-excel " );
          wb.write(outData);
          outData.flush();
          response.flushBuffer();
          // 完成
          %>
          posted on 2006-09-19 15:59 brock 閱讀(403) 評論(0)  編輯  收藏 所屬分類: 處理Excel poi
          主站蜘蛛池模板: 杭锦旗| 怀柔区| 庆云县| 尼木县| 叶城县| 壤塘县| 广丰县| 普兰县| 沂水县| 纳雍县| 志丹县| 吴堡县| 河北省| 安远县| 荥经县| 台北县| 阜新| 临汾市| 静安区| 徐州市| 鄂伦春自治旗| 堆龙德庆县| 武冈市| 扎囊县| 义马市| 徐汇区| 芮城县| 湘西| 呈贡县| 修文县| 清镇市| 陵水| 常宁市| 噶尔县| 疏勒县| 黄骅市| 福建省| 红河县| 通海县| 张家口市| 霍邱县|