日期:2014-05-16  浏览次数:20541 次

已经解决的使用JS从JSP页面到处Excel表格(需要自己保存)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html> 

<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gbk"/> 
    <title>WEB页面导出为EXCEL文档的方法</title>
    <SCRIPT LANGUAGE="javascript"> 
    
   
   
function method1(tableid) {
     var curTbl = document.getElementById(tableid); 
    var oXL;
   try{
    oXL = new ActiveXObject("Excel.Application"); 
    
   }catch (e) {

      alert("请将地址加入信任站点!");
    return;
   }
     var oWB = oXL.Workbooks.Add(); 
     var oSheet = oWB.ActiveSheet; 
    //控制行的高度
   // oSheet.Rows(1).EntireRow.RowHeight = 5;
    var lenRow = curTbl.rows.length;
    var rowId = 0;
    var maxLength = 0;
    //找出列的最大长度
    for(rowId=0;rowId<lenRow;rowId++){
    var cellsLength = curTbl.rows.item(rowId).cells;
    var colLength = cellsLength.length;
    if(colLength > maxLength){
       maxLength = colLength;
    }
    }
    //循环控制excel报表每列的宽度
    if(maxLength != null && maxLength != "" && maxLength > 0){
     for(var i=1;i<=maxLength;i++){
      oSheet.Columns(i).EntireColumn.ColumnWidth = 30;
      }
   }
     var sel = document.body.createTextRange(); 
     sel.moveToElementText(curTbl); 
     //sel.select(); 
     sel.execCommand("Copy"); 
     oSheet.Paste(); 
   //整个表格粘贴完毕后,再设置excel表格样式
   
   for(rowId=0;rowId<lenRow;rowId++){
   if(rowId == 0){
       oSheet.Rows("1:1").Interior.ColorIndex=10; //标题背景设置为绿色
       oSheet.Rows("1:1").Font.ColorIndex=2;     //标题字体颜色设置为白色
    }else{
       oSheet.Rows((rowId+1)+":"+(rowId+1)).Interior.ColorIndex=0; //除标题外,其它行把格式清除
    }
    }
     oXL.Visible = true; 
} 
//数字被自动以科学计数法的形式显示,搜了好多资料也没有解决,用NumberFormatLocal="@"好像不行,但是可以在数字后面加&nbsp;解决
function method2(tableid) //读取表格中每个单元到EXCEL中   
{   
    var curTbl = document.getElementById(tableid);   
    var oXL = new ActiveXObject("Excel.Application");   
    //创建AX对象excel   
    var oWB = oXL.Workbooks.Add();   
    //获取workbook对象   
    var oSheet = oWB.ActiveSheet;   
    //激活当前sheet   
    
    var rowLength = curTbl.rows.length;
    //得到表格行数
    if(rowLength != null && rowLength != "" && rowLength >= 1){
    var lastRowIndex = rowLength - 1;
    //得到最后一行序号
   }
    var colLength = curTbl.rows.item(lastRowIndex).cells.length;
    //根据最后一行获取表格列数
    if(colLength != null && colLength != ""){
       for(var i=1;i<=colLength;i++){
         oSheet.Columns(i).ColumnWidth = 20;
         //循环控制每一列的宽度
        }
    }
    
    var Lenr = curTbl.rows.length;   
    //取得表格行数   
    for (i = 0; i < Lenr; i++)   
    {   
        var Lenc = curTbl.rows(i).cells.length;   
        //取得每行的列数   
        for (j = 0; j < Lenc; j++)   
        {   
        
            oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;   
            
            //赋值   
        }   
    }   
    oXL.Visible = true;   
    //设置excel可见属性   
}   

function getXlsFromTbl(inTblId, inWindow) {   
    try {   
        var allStr = "";   
        var curStr = "";   
        //alert("getXlsFromTbl");   
        if (inTblId != null && inTblId != "" && inTblId != "null") {   
            curStr = getTblData(inTblId, inWindow);   
        }   

        if (curStr != null) {   
            allStr += curStr;   
        }   
        else {   
            alert("你要导出的表不存在!");   
            return;   
        }   

        var fileName = getExcelFileName();   
        doFileExport(fileName, allStr);   
    }   
    catch(e) {   
        alert("导出发生异常:" + e.name + "->" + e.description + "!");   
    }   
}   

function getTblData(inTbl, inWindow) {   
    var rows = 0;   
    //alert("getTblData is " + inWindow);   
    var tblDocument = document