<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Rising Sun

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      148 隨筆 :: 0 文章 :: 22 評論 :: 0 Trackbacks
    1. /*
    2. ?*?QuickExcel.java
    3. ?*?作者:楊慶成
    4. ?*?Created?on?2004年11月22日,?下午4:05
    5. ?*?在實際應用中經常要將數據庫中的表導入Excel
    6. ?*?本人在Apache的POI基礎上寫了一個簡單的類
    7. ?*?有不當指出請指正,謝謝!
    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對象輸出到文件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 閱讀(456) 評論(0)  編輯  收藏 所屬分類: 處理Excel poi
    主站蜘蛛池模板: 久久久久亚洲精品无码网址色欲| 无码乱肉视频免费大全合集| 午夜精品在线免费观看| 伊在人亚洲香蕉精品区麻豆| 性色av极品无码专区亚洲| 好大好硬好爽免费视频| 国产亚洲综合成人91精品| 香蕉免费一级视频在线观看| 亚洲人成电影网站免费| 国产亚洲玖玖玖在线观看| 毛片a级毛片免费播放100| 亚洲AV无码国产丝袜在线观看 | 久久国产福利免费| 亚洲av无码成人黄网站在线观看| 亚洲精品中文字幕无码A片老| 免费在线中文日本| 亚洲最大免费视频网| 毛片免费观看的视频在线| 久久亚洲精品无码AV红樱桃| 国产又大又粗又长免费视频 | 亚洲综合一区二区国产精品| 最近中文字幕免费mv在线视频 | 亚洲avav天堂av在线不卡| 黄色大片免费网站| 亚洲欧洲∨国产一区二区三区| 在线视频亚洲一区| 国产亚洲成av人片在线观看| 222www免费视频| 亚洲成a人无码亚洲成www牛牛| 国产在线观看麻豆91精品免费| 亚洲成AV人片一区二区密柚| 69天堂人成无码麻豆免费视频| 亚洲国产精品无码久久久秋霞2 | 手机在线毛片免费播放| 国产亚洲男人的天堂在线观看| 永久在线观看www免费视频| 亚洲熟女精品中文字幕| 亚洲中文字幕日产乱码高清app| 人人爽人人爽人人片av免费| 亚洲AV无码成人网站久久精品大| 国产婷婷成人久久Av免费高清|