Sugo

          Goof off ? no way: never
          posts - 20, comments - 8, trackbacks - 73, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          日歷

          <2006年8月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          公告

          Do it , do better:)

          MyPet

          搜索

          •  

          最新評論

          ?????? 關于word文檔操作, 網上不少這樣的例子,好像都是一篇文章。word文檔嵌入到網頁中好像比較簡單一些。加個報文頭基本上就可以實現此功能。而導出數據到word文檔中,我可費了不少的腦細胞。閑話少說,步入正題。。。:)
          ????? 網頁中嵌入word文檔,在jsp程序開頭部分加入<%@ page contentType="application/msword;charset=GBK"%>這些代碼,即可實現。比較簡單,相信很多人都知道這一點了。
          ???? ?費腦筋的是將一張報表或者一張登記表導入到word文檔中。報表(登記表)都是動態生成的。那么首先的問題是如何獲得一個動態網頁中的內容。我以前使用的BufferedReader,PrintWriter,BufferedInputStream,BufferedOutputSteam都不起作用了,原因就是他們不能讀取動態生成的網頁內容。因此,我想到了URL這個類,獲得內容的代碼如下:
          ????? ?java.net.URL l_url = new java.net.URL("http://localhost:9000/rlzy/employee/employee_resume.jsp?id="+emp_id);

          ?????? java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();

          ????? ?l_connection.connect();

          ????? ?l_urlStream = l_connection.getInputStream();

          ???????java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream));

          ?????? while ((sCurrentLine = l_reader.readLine()) != null)

          ??????? {

          ??????????content+=sCurrentLine;

          ???????? }?
          ?????? content即為我們想要得到的動態內容。
          ?????? 導出數據的話,使用BufferedInputStream?和?BufferedOutputStream 。但前提是把得到的動態內容先輸出到一個臨時的html網頁中,即把它先變成一個靜態文件。這樣BufferedInputStream 和BufferedOutputStream 就可以獲得要讀取和輸出的內容。讀取完畢,再把這個臨時的html網頁刪除。這是網上流傳的代碼:
          ?????? BufferedInputStream bis = null;
          ?????? BufferedOutputStream bos = null;
          ?????? try {
          ???????????? ?bis = new BufferedInputStream(new FileInputStream(內容資源的路徑));
          ????????????? bos = new BufferedOutputStream(response.getOutputStream());

          ???????????? ?byte[] buff = new byte[2048];
          ??????????? ? int bytesRead;

          ???????????? ?while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
          ??????????????bos.write(buff,0,bytesRead);
          ??????????? }

          ???????? ?} catch(final IOException e) {
          ??????? System.out.println ( "出現IOException." + e );
          ????????? } finally {
          ???????? ?if (bis != null)
          ??????????? bis.close();
          ????????? if (bos != null)
          ??????????? bos.close();
          ?????? }
          ?????? 此外,導出內容到word文檔后,還可能會出現格式不正確的問題。我就遇到了html表格導出到word就發生變化的情況。這種情況具體原因應該是兩種不同文件編碼的問題。我是通過修改源html文件解決的問題。至于以后如果還碰到類似問題的話,首先看一下word文檔和html文檔是從哪個地方格式開始變化了,找到之后再對癥下藥。如果你覺得這種解決方法沒什么技術含量的話,你可以根據內容手動輸出。。。哈
          ????????????????????????????????????????????????? ? 。。。。。。。。。。。學到了一些東西。。。。。。。。。。。。


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


          網站導航:
           
          主站蜘蛛池模板: 中西区| 南漳县| 玉树县| 葫芦岛市| 朝阳市| 科技| 铁力市| 巧家县| 肃北| 宁城县| 洞头县| 慈利县| 樟树市| 衡东县| 大余县| 如东县| 鸡东县| 墨玉县| 巴楚县| 柘城县| 常熟市| 曲靖市| 永胜县| 六安市| 图木舒克市| 平谷区| 平利县| 梅河口市| 维西| 扶余县| 西华县| 德昌县| 景宁| 武义县| 饶平县| 普格县| 安平县| 依安县| 屯留县| 盘山县| 兴隆县|