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
          主站蜘蛛池模板: 盐亭县| 瑞安市| 边坝县| 南丰县| 宁化县| 达日县| 兴化市| 西林县| 民丰县| 台湾省| 中阳县| 五原县| 虎林市| 阿拉善右旗| 平舆县| 仪陇县| 永州市| 永春县| 和龙市| 龙井市| 鄂托克前旗| 玉环县| 乌苏市| 锦屏县| 新泰市| 甘泉县| 平和县| 南华县| 松阳县| 永靖县| 克东县| 黄冈市| 营口市| 宁海县| 民权县| 大英县| 丹棱县| 德庆县| 佛坪县| 红河县| 钟山县|