Rising Sun

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            148 隨筆 :: 0 文章 :: 22 評(píng)論 :: 0 Trackbacks
          1. /*
          2. ?*?QuickExcel.java
          3. ?*?作者:楊慶成
          4. ?*?Created?on?2004年11月22日,?下午4:05
          5. ?*?在實(shí)際應(yīng)用中經(jīng)常要將數(shù)據(jù)庫(kù)中的表導(dǎo)入Excel
          6. ?*?本人在Apache的POI基礎(chǔ)上寫了一個(gè)簡(jiǎn)單的類
          7. ?*?有不當(dāng)指出請(qǐng)指正,謝謝!
          8. ?*?
          9. ?*/
          10. package ?yqc.poi;
          11. import ?java.sql.*;
          12. import ?java.util.*;
          13. import ?java.io.*;
          14. import ?java.io.ByteArrayInputStream;
          15. import ?java.io.FileInputStream;
          16. import ?java.io.FileOutputStream;
          17. import ?java.io.IOException;
          18. import ?org.apache.poi.hssf.usermodel.*;
          19. import ?org.apache.poi.poifs.filesystem.POIFSFileSystem;
          20. import ?org.apache.poi.hssf.record.*;
          21. import ?org.apache.poi.hssf.model.*;
          22. import ?org.apache.poi.hssf.usermodel.*;
          23. import ?org.apache.poi.hssf.util.*;import?yqc.sql.*;
          24. /**
          25. ?*
          26. ?*?@author??Administrator
          27. ?*/
          28. public ?class?QuickExcel?{
          29. ????
          30. ????/**?Creates?a?new?instance?of?QuickExcel?*/
          31. ????private?QuickExcel(String?file){
          32. ????????_file=file;
          33. ????}
          34. ????
          35. ????private?void?open()throws?IOException{
          36. ????????InputStream?stream?=?null;
          37. ????????Record[]?records?=?null;
          38. ????????POIFSFileSystem?fs?=
          39. ????????????new?POIFSFileSystem(new?FileInputStream(_file));
          40. ????????_wb?=?new?HSSFWorkbook(fs);
          41. ????}
          42. ????
          43. ????private?void?create(){
          44. ????????_wb=new?HSSFWorkbook();
          45. ????}
          46. ????
          47. ????public?static?QuickExcel?newInstance?(String?file){
          48. ????????QuickExcel?qe=new?QuickExcel(file);
          49. ????????qe.create();
          50. ????????return?qe;
          51. ????}
          52. ????
          53. ????public?static?QuickExcel?openInstance(String?file)?throws?IOException?{
          54. ????????QuickExcel?qe=new?QuickExcel(file);
          55. ????????qe.open();
          56. ????????return?qe;
          57. ????}
          58. ????
          59. ????public?void?close(){
          60. ????????try{
          61. ????????????FileOutputStream?fileOut?=?new?FileOutputStream(_file);
          62. ????????????_wb.write(fileOut);//把Workbook對(duì)象輸出到文件workbook.xls中
          63. ????????????fileOut.close();
          64. ????????}
          65. ????????catch?(Exception?ex){
          66. ????????????System.out.println(ex.getMessage());
          67. ????????}
          68. ????}
          69. ????
          70. ????private?void?removeSheet(String?sheetName){
          71. ????????int?i=_wb.getSheetIndex("sheetName");
          72. ????????if?(i>=0)?_wb.removeSheetAt(i);
          73. ????}
          74. ????
          75. ????public?int?fillSheet?(ResultSet?rs,String?sheetName)throws?SQLException?{
          76. ????????HSSFSheet?st=?_wb.createSheet(sheetName);
          77. ????????ResultSetMetaData?rsmd=?rs.getMetaData();
          78. ????????int?index=0;
          79. ????????int?result=0;
          80. ????????HSSFRow?row=st.createRow(index++);
          81. ????????for(int?i=1;i<=rsmd.getColumnCount();++i){
          82. ????????????HSSFCell?cell=row.createCell((short)(i-1));
          83. ????????????cell.setCellValue(rsmd.getColumnName(i));
          84. ????????}
          85. ????????while(rs.next())?{
          86. ????????????result++;
          87. ????????????row=st.createRow(index++);
          88. ????????????for(int?i=1;i<=rsmd.getColumnCount();++i){
          89. ????????????????HSSFCell?cell=row.createCell((short)(i-1));
          90. ????????????????cell.setEncoding(cell.ENCODING_UTF_16);
          91. ????????????????cell.setCellValue(rs.getString(i));
          92. ????????????}
          93. ????????}
          94. ????????return?result;
          95. }
          96. ????
          97. ????public?static?void?main(String[]?args){
          98. ????????try{
          99. ????????????QuickConnection?qc=new?MssqlConnection("jdbc:microsoft:sqlserver://192.168.0.100:1433;DatabaseName=ls");
          100. ????????????QuickExcel?qe=QuickExcel.newInstance("a.xls");
          101. ????????????qc.connect();
          102. ????????????String?sql="select?*?from?ls.dbo.radio1_emcee";
          103. ????????????ResultSet?rs=qc.getStatement().executeQuery(sql);
          104. ????????????qe.fillSheet(rs,"MT");
          105. ????????????qe.close();
          106. ????????????qe=QuickExcel.openInstance("a.xls");
          107. ????????????qe.fillSheet(rs,"MO");
          108. ????????????qe.close();
          109. ????????????qc.close();
          110. ????????}
          111. ????????catch?(SQLException?ex){
          112. ????????????System.out.println(ex.getMessage());
          113. ????????}
          114. ????????catch?(IOException?ex){
          115. ????????????System.out.println(ex.getMessage());
          116. ????????}
          117. ????}
          118. ????
          119. ????HSSFWorkbook?_wb;
          120. ????String?_file="new.xls";
          121. }
          posted on 2006-07-26 15:30 brock 閱讀(459) 評(píng)論(0)  編輯  收藏 所屬分類: 處理Excel poi
          主站蜘蛛池模板: 潮州市| 东丰县| 南溪县| 鱼台县| 蓬溪县| 武邑县| 黔江区| 时尚| 响水县| 那坡县| 大理市| 溧水县| 常山县| 海城市| 阳高县| 彩票| 延安市| 钟祥市| 界首市| 江山市| 交城县| 宝坻区| 全州县| 宝山区| 马关县| 定南县| 沿河| 周宁县| 旬阳县| 丹东市| 郓城县| 房产| 淮安市| 监利县| 敦煌市| 垣曲县| 天津市| 潜山县| 东安县| 邵武市| 通榆县|