<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 閱讀(455) 評論(0)  編輯  收藏 所屬分類: 處理Excel poi
    主站蜘蛛池模板: 最近免费中文字幕mv在线电影| 亚洲AV无码一区二区乱子仑| aa午夜免费剧场| 免费一级黄色毛片| 污视频网站免费在线观看| 国产精品免费视频网站| 日韩欧美亚洲中文乱码| 免费国产美女爽到喷出水来视频| 亚洲日韩精品无码专区| 成年女人男人免费视频播放| 亚洲一区二区三区丝袜| 国产猛烈高潮尖叫视频免费| 杨幂最新免费特级毛片| 在线观看国产区亚洲一区成人 | 午夜一级免费视频| 亚洲自偷自偷在线成人网站传媒| 日韩高清免费观看| 一级毛片aaaaaa视频免费看| 亚洲爆乳无码一区二区三区| 免费无码一区二区三区| 亚洲AV无码成人专区| 国产免费无遮挡精品视频| 亚欧国产一级在线免费| 亚洲VA中文字幕无码毛片| 最近中文字幕高清免费中文字幕mv| 亚洲人成人77777网站不卡| 无码专区一va亚洲v专区在线 | 国产乱子伦精品免费女| 久久99久久成人免费播放| 久久精品国产亚洲av高清漫画| 成人免费毛片观看| 中文在线观看国语高清免费| 亚洲欧洲日产国码二区首页| 波多野结衣久久高清免费| a级毛片免费全部播放无码| 亚洲人成77777在线播放网站不卡| 久久久久亚洲精品男人的天堂| 日韩在线播放全免费| 一级做a毛片免费视频| 亚洲另类自拍丝袜第1页| 国产亚洲精久久久久久无码77777 国产亚洲精品成人AA片新蒲金 |