posts - 0,  comments - 5,  trackbacks - 0

          要用到poi-2.5.1-final-20040804.jar包

          ?1 import ?java.io.FileOutputStream;
          ?2 import ?java.sql.Connection;
          ?3 import ?java.sql.ResultSet;
          ?4 import ?java.sql.ResultSetMetaData;
          ?5
          ?6 import ?org.apache.poi.hssf.usermodel.HSSFCell;
          ?7 import ?org.apache.poi.hssf.usermodel.HSSFRow;
          ?8 import ?org.apache.poi.hssf.usermodel.HSSFSheet;
          ?9 import ?org.apache.poi.hssf.usermodel.HSSFWorkbook;
          10 import ?org.dom4j.Document;
          11 import ?org.dom4j.Element;
          12
          13 import ?wufalong.exportServer.ConnectBean;
          14 import ?wufalong.exportServer.CreateConnection;
          15 import ?wufalong.exportServer.GetResultSet;
          16 import ?wufalong.exportServer.LoadRootElementByConfigFile;
          17 import ?exportxls.JsUserBasicData;
          18
          19 public ? class ?Person20070517
          20 {
          21 ?? public ? static ? void ?main(String?args[])?
          22 ?? {
          23 ??????Person20070517?c = new ?Person20070517();
          24 ?????? int ?allcount = 0 ;
          25 ??????allcount = allcount + c.createPersonMobile( " c:/人才.xls " );
          26 ??}

          27 ??
          28 ?? public ? int ?createPersonMobile(String?filename)
          29 ?? {
          30 ??????Connection?con? = ? null ;
          31 ??????ResultSet?rs? = ? null ;
          32 ??????Document?doc? = ?LoadRootElementByConfigFile.getRootElement();
          33 ??????Element?e? = ?doc.getRootElement().element( " import " );
          34 ?????? try
          35 ?????? {
          36 ????????????con? = ?CreateConnection.getConnect(e);
          37 ????????????rs? = ?GetResultSet.getResule(con,ConnectBean.getSql(e));
          38 ??????????FileOutputStream?fOut? = ? null ;
          39 ??????????HSSFWorkbook?workbook? = ? new ?HSSFWorkbook();
          40 ??????????HSSFSheet?sheet? = ?workbook.createSheet();
          41 ??????????HSSFRow?row? = ? null ;
          42 ??????????HSSFCell?cell? = ? null ;
          43 ?????????? int ?i? = ? 1 ;
          44 ?????????? int ?writeadd? = ? 0 ;
          45 ??????????String??cellvalue? = ? "" ;
          46 ??????????JsUserBasicData?jub? = ? new ?JsUserBasicData();
          47 ??????????row? = ?sheet.createRow( 0 );
          48 ??????????cell? = ?row.createCell(( short )? 0 );
          49 ??????????cell.setCellType(HSSFCell.CELL_TYPE_STRING);
          50 ??????????cell.setEncoding(HSSFCell.ENCODING_UTF_16);
          51 ??????????cell.setCellValue( " 姓名 " );
          52 ??????????cell? = ?row.createCell(( short )? 1 );
          53 ??????????cell.setCellType(HSSFCell.CELL_TYPE_STRING);
          54 ??????????cell.setEncoding(HSSFCell.ENCODING_UTF_16);
          55 ??????????cell.setCellValue( " 手機 " );
          56 ??????????
          57 ?????????? while ?(rs.next())? {
          58
          59 ??????????????System.out.println( " 正在生成第 " ? + ?i? + ? " 條記錄 " );
          60 ??????????????ResultSetMetaData?rsmd? = ?rs.getMetaData();
          61 ??????????????row? = ?sheet.createRow(( short )?i);
          62 ?????????????? int ?colcount? = ?rsmd.getColumnCount();
          63 ?????????????? for ?( int ?icc? = ? 0 ;?icc? < ?colcount;?icc ++ )? {
          64 ??????????????????cellvalue? = ?rs.getString(icc? + ? 1 );?
          65 ??????????????????cell? = ?row.createCell(( short )?icc);
          66 ??????????????????System.out.println( " md: " ? + ?cellvalue);?
          67 ??????????????????cell.setCellType(HSSFCell.CELL_TYPE_STRING);
          68 ??????????????????cell.setEncoding(HSSFCell.ENCODING_UTF_16);
          69 ??????????????????cell.setCellValue(cellvalue);
          70 ??????????????}

          71 ??????????????i ++ ;
          72 ??????????}

          73
          74 ??????????fOut? = ? new ?FileOutputStream(filename);
          75 ??????????workbook.write(fOut);
          76 ??????????fOut.flush();
          77 ??????????fOut.close();
          78 ??????????System.out.println(filename? + ? " 文件生成 " ? + ?i? + ? " " );
          79 ??????????rs.close();
          80 ??????????rs? = ? null ;
          81 ??????????con.close();
          82 ??????????con? = ? null ;
          83 ?????????? return ?i;
          84 ??????}
          ? catch ?(Exception?ee)?
          85 ?????? {
          86 ??????????????System.out.println( " 發生了 " ? + ?ee.getMessage()? + ? " 例外 " );
          87 ??????}
          ?
          88 ?????? finally ?
          89 ?????? {
          90 ??????????????GetResultSet.closeResultSet(rs);
          91 ??????????????CreateConnection.closeConnect(con);
          92 ??????}

          93 ????? return ? 0 ;
          94 ??}

          95 }

          96
          posted on 2007-09-03 13:56 crazy 閱讀(1790) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 定陶县| 高密市| 普安县| 阳城县| 通化县| 彭山县| 仙游县| 白玉县| 泸水县| 商南县| 西畴县| 介休市| 乌兰察布市| 灵武市| 盐津县| 大英县| 佛山市| 禹城市| 定南县| 安阳市| 庄河市| 塔城市| 都江堰市| 手游| 云浮市| 聊城市| 洛浦县| 祁阳县| 辉南县| 竹溪县| 芷江| 汶上县| 同德县| 高碑店市| 宁阳县| 邓州市| 手机| 收藏| 屯留县| 澳门| 连州市|