Oo ' Smiling on Java ' oO

          從夢里回來,依然記得夢里有你...

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            8 隨筆 :: 0 文章 :: 10 評論 :: 0 Trackbacks
          試試用POI操作excel囖.
          純粹為了自己好找.
          要有人看了這些的話,隨意就好啦...歡迎指出不當之處.
          package?xls;

          import?java.io.FileInputStream;
          import?java.io.FileNotFoundException;
          import?java.io.FileOutputStream;
          import?java.io.IOException;
          import?java.io.OutputStreamWriter;
          import?java.io.UnsupportedEncodingException;
          import?java.text.SimpleDateFormat;
          import?java.util.ArrayList;
          import?java.util.Date;
          import?java.util.List;

          import?org.apache.poi.hssf.usermodel.HSSFCell;
          import?org.apache.poi.hssf.usermodel.HSSFRow;
          import?org.apache.poi.hssf.usermodel.HSSFSheet;
          import?org.apache.poi.hssf.usermodel.HSSFWorkbook;

          public?class?ReadXL
          {
          ????
          public?static?String?fileName?=?"C:\\xls\\EXCEL.xls";
          ????
          public?static?String?path?=?"C:\\xls\\";?//為方便,就這樣寫了.
          ????public?static?void?main(String?argv[])
          ????
          {
          ????????List?list?
          =?readExcel();
          ????????String?xmldata?
          =?buildXML(list);
          ????????createXMLFile(xmldata);
          ????}


          ????
          /**
          ?????*?讀取磁盤上的EXCEL文件的內容
          ?????*?
          @return?List
          ?????
          */

          ????
          public?static?List?readExcel()
          ????
          {
          ????????List?list?
          =?null;
          ????????UserBean?ub?
          =?null;
          ????????
          try
          ????????
          {
          ????????????HSSFWorkbook?workbook?
          =?new?HSSFWorkbook(new?FileInputStream(
          ????????????????????fileName));?
          //得到excel對象
          ????????????HSSFSheet?sheet?=?workbook.getSheetAt(0);?//得到第一個sheet
          ????????????int?rows?=?sheet.getPhysicalNumberOfRows();?//得到行數
          ????????????list?=?new?ArrayList();
          ????????????
          for?(int?i?=?1;?i?<?rows;?i++)
          ????????????
          {
          ????????????????HSSFRow?row?
          =?sheet.getRow(i);
          ????????????????ub?
          =?new?UserBean();
          ????????????????HSSFCell?cell?
          =?row.getCell((short)?0);?//得到列0(下標0,為第一列)
          ????????????????ub.setName(cell.getStringCellValue());
          ????????????????cell?
          =?row.getCell((short)?1);?//得到列?1
          ????????????????ub.setEmail(cell.getStringCellValue());
          ????????????????cell?
          =?row.getCell((short)?2);?//得到列2
          ????????????????ub.setPhone(cell.getStringCellValue());
          ????????????????cell?
          =?row.getCell((short)?3);?//得到列3
          ????????????????ub.setPasswd(cell.getStringCellValue());
          ????????????????list.add(ub);
          ????????????}

          ????????????
          return?list;
          ????????}

          ????????
          catch?(Exception?e)
          ????????
          {
          ????????????
          return?null;
          ????????}

          ????}


          ????
          /**
          ?????*?組裝xml格式字符串
          ?????*?
          @param?list?List
          ?????*?
          @return?String
          ?????
          */

          ????
          public?static?String?buildXML(List?list)
          ????
          {
          ????????StringBuffer?sb?
          =?new?StringBuffer();
          ????????sb.append(
          "<contents-list>\n\t");
          ????????sb.append(
          "<content>\n\t\t");
          ????????
          for?(int?i?=?0;?i?<?list.size();?i++)
          ????????
          {
          ????????????UserBean?bean?
          =?(UserBean)?list.get(i);
          ????????????sb.append(
          "<userinfo>\n\t\t\t");
          ????????????sb.append(
          "<name>"?+?bean.getName()?+?"</name>\n\t\t\t");
          ????????????sb.append(
          "<email>"?+?bean.getEmail()?+?"</email>\n\t\t\t");
          ????????????sb.append(
          "<phone>"?+?bean.getPhone()?+?"</phone>\n\t\t\t");
          ????????????sb.append(
          "<passwd>"?+?bean.getPasswd()?+?"</passwd>\n\t\t");
          ????????????
          if?(i?+?1?<?list.size())
          ????????????
          {
          ????????????????sb.append(
          "</userinfo>\n\t\t");
          ????????????}

          ????????????
          else
          ????????????
          {
          ????????????????sb.append(
          "</userinfo>\n\t");
          ????????????}

          ????????}

          ????????sb.append(
          "</content>\n");
          ????????sb.append(
          "</contents-list>");
          ????????
          return?sb.toString();
          ????}



          ????
          /**
          ?????*?輸出到文件
          ?????*?
          @param?xmldata?String
          ?????
          */

          ????
          public?static?void?createXMLFile(String?xmldata)
          ????
          {
          ????????String?createTime?
          =?createTime();
          ????????String?filename?
          =?path?+?createTime?+?".xml";
          ????????OutputStreamWriter?osw?
          =?null;
          ????????FileOutputStream?output?
          =?null;
          ????????
          try
          ????????
          {
          ????????????output?
          =?new?FileOutputStream(filename);
          ????????????osw?
          =?new?OutputStreamWriter(output,?"utf-8");
          ????????????osw.write(xmldata);
          ????????????osw.flush();
          ????????}

          ????????
          catch?(Exception?ex)
          ????????
          {
          ????????}
          ????????
          ????????
          finally
          ????????
          {
          ????????????
          try
          ????????????
          {
          ????????????????
          if?(null?!=?output)
          ????????????????
          {
          ????????????????????output.close();
          ????????????????}

          ????????????}

          ????????????
          catch?(IOException?ex2)
          ????????????
          {
          ????????????}

          ????????????
          try
          ????????????
          {
          ????????????????
          if?(null?!=?osw)
          ????????????????
          {
          ????????????????????osw.close();
          ????????????????}

          ????????????}

          ????????????
          catch?(IOException?ex3)
          ????????????
          {
          ????????????}

          ????????}

          ????}


          ????
          /**
          ?????*?用時間作為文件名
          ?????*?
          @return?String
          ?????
          */

          ????
          public?static?String?createTime()
          ????
          {
          ????????Date?date?
          =?new?Date();
          ????????SimpleDateFormat?dateFormat?
          =?new?SimpleDateFormat("yyyyMMddmmss");
          ????????String?createTime?
          =?dateFormat.format(date);
          ????????
          return?createTime;
          ????}


          }


          再就一個UserBean,就幾個屬性.
          posted on 2007-12-11 23:28 Jwin 閱讀(2225) 評論(1)  編輯  收藏 所屬分類: ioOpr

          評論

          # re: poi讀excel 2016-06-14 23:00 Mary james
          thank you for the great tutorial.
          I recommend this website:
          http://how-to-program-in-java.com/
          It was really helpful for me.  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 内乡县| 海南省| 华蓥市| 莱芜市| 凭祥市| 平凉市| 怀远县| 玉山县| 新绛县| 汉沽区| 积石山| 正定县| 临沧市| 北流市| 临安市| 孟连| 蒙城县| 察哈| 浦县| 汪清县| 麦盖提县| 遂平县| 民丰县| 锦州市| 成武县| 霍城县| 普兰店市| 柳州市| 习水县| 教育| 阆中市| 南充市| 长寿区| 桃园市| 永宁县| 共和县| 永川市| 京山县| 南京市| 姜堰市| 大冶市|