使用DOM模型读取网页表格并保存为Excel

首先,要建立表格体TBODY

 

然后在该页建立一个隐藏层


以及按钮

="javascript:saveAsExcel('queryResultTable');">

saveAsExcel()函数内容如下

function saveAsExcel(TBODYNAME){
  var pos =prompt("/u8BF7/u8F93/u5165/u67E5/u8BE2/u7ED3/u679C/u6587/u4EF6/u4FDD/u5B58/u4F4D/u7F6E","C://HWMIS.xls");
  if( pos.length > 0){

  var tableNode = document.getElementById(TBODYNAME);
  var trCount = queryResultTable.childNodes.length;
  var tdCount = 0;
  var tdText = "";
  var j = trCount;
  var i = 0;
  var trNode;
 
  while(trCount > 0){
    trNode = tableNode.childNodes[j-trCount];
    tdCount = trNode.childNodes.length - 1;
   
    for(i=0;i   
      tdNode = trNode.childNodes[i];
     
      if(tdNode.childNodes[0] != null){
        if(tdNode.childNodes[0].nodeType == 3){
           //Text Node
           tdText += tdNode.childNodes[0].data + ",";
          }
        if(tdNode.childNodes[0].nodeType == 1){
           //Element Node
           if((tdNode.childNodes[0].childNodes[0] != null)&&(tdNode.childNodes[0].childNodes[0].nodeType == 3)){
             tdText += tdNode.childNodes[0].childNodes[0].nodeValue + ",";
           }else{
             tdText += " ,";
           }
        }
      }else{
        tdText += " ,";
      }
    }
    trCount--;
    tdText += ";";
  }
  document.getElementById("infoDIV").innerHTML = "";
  }
}

SaveAsExcelAction将javaScript函数读取的内容接收,内容如下

 public ActionForward perform(ActionMapping actionMapping,
   ActionForm actionForm, HttpServletRequest req,
   HttpServletResponse res) throws ServletException {
  
  try {
   String saveAsPos = "";
   String inputText  = "";
   if (req.getParameter("SAVEASPOS") != null){
    saveAsPos = req.getParameter("SAVEASPOS").toString();
   }
   if (req.getParameter("INPUTTEXT") != null){
    inputText = req.getParameter("INPUTTEXT").toString();
   }

      req.getSession().setAttribute("saveResult",new SaveAsExcel().saveAsExcel(saveAsPos,inputText,",",";"));
   return actionMapping.findForward("success");
  } catch (Exception ex) {
   ex.printStackTrace();
   return actionMapping.findForward("success");
  }

 }


import java.util.*;
import java.io.*;

import jxl.*;

public class SaveAsExcel {


 
 public String saveAsExcel(String path, String inputData, String columnSplit,String rowSplit) {
  try {
   File targetFile = new File(path);
   if ((targetFile.exists()) && (!(targetFile.isFile()))) {
    return "文件位置不合法,请重新输入!";
   }
   // Create Workbook
   jxl.write.WritableWorkbook wwb = Workbook
     .createWorkbook(targetFile);

   // Create Sheet
   jxl.write.WritableSheet ws = wwb.createSheet("HWMISSheet1", 0);

   // Create Label
   // System.out.println(al.get(0).toString());
   // Class beo = Class.forName(beoClassName);
   // Class[] cl = new Class[1];
   // cl[0] = Integer.TYPE;
   // Object Beo = ct.

   String tempRow = "";
   int rowSplitPos = 0;
   int columnSplitPos = 0;
   int i = 0,j = 0 ;
   while((rowSplitPos = inputData.indexOf(rowSplit)) > 0){
    j = 0;
    tempRow = inputData.substring(0,rowSplitPos);
    while((columnSplitPos = tempRow.indexOf(columnSplit)) > 0){     
     jxl.write.Label labelC = new jxl.write.Label(j, i, tempRow.substring(0,columnSplitPos));
     ws.addCell(labelC);     
     tempRow = tempRow.substring(columnSplitPos + 1);
     j++;
    }
    
    inputData = inputData.substring(rowSplitPos + 1);
    i++ ;
   }
   // jxl.write.Label labelC = new jxl.write.Label(0, 0,
   // "This is a Label cell");

   // Add Cell
   // ws.addCell(labelC);
   // al.get(0)

   // WriteBack
   wwb.write();

   // Close
   wwb.close();

  } catch (Exception ex) {
   ex.printStackTrace();
   return "文件位置不合法或该文件已被其他程序打开,请重新输入其他位置!";
  }
  return "查询结果另存为 " + path + " 成功。";
 }
}

保存完成后,按照struts-config.xml配置

     type="SaveAsExcelAction">
   
  
 

将隐藏层内页面指向显示保存结果的saveAs.jsp


<%
if (session.getAttribute("saveResult") != null) {
    
%>

<%
   }

%>

最终结果就是在显示结果的页面点击保存按钮,经过操作,在本页显示保存结果,不用跳转,方便

相关链接

全面挖掘Java Excel API 使用方法 http://qaid.blogchina.com/2553076.html 

jxl http://sourceforge.net/projects/jxl

你可能感兴趣的:(Java)