js实现导入导出打印预览

<%@ page contentType="application/vnd.ms-excel;charset=gb2312" %>
<html>
<head>
<style media=print mce_bogus="1"> .Noprint{display:none;} .PageNext{page-break-after:always;} </style>
</head>
<link rel="stylesheet" type="text/css" href="file:///D|/soft/ext-3.0.3/resources/css/ext-all.css" />
<script language="javascript" type="text/javascript" src="file:///D|/soft/ext-3.0.3/adapter/ext/ext-base.js"></script>
<script language="javascript" type="text/javascript" src="file:///D|/soft/ext-3.0.3/ext-all.js"></script>
<script language="javascript" type="text/javascript">
Ext.onReady(function(){

var data=[ [1, 'EasyJWeb', 'EasyJF','www.easyjf.com'],

[2, 'jfox', 'huihoo','www.huihoo.org'],

[3, 'jdon', 'jdon','www.jdon.com'],

[4, 'springside', 'springside','www.springside.org.cn'] ];
var sm=new Ext.grid.CheckboxSelectionModel();
var store=new Ext.data.SimpleStore({data:data,fields:["id","name","organization","homepage"]});

var grid = new Ext.grid.GridPanel({

renderTo:"hello",

title:"中国Java开源产品及团队",

height:150,

width:600,

columns:[sm,{header:"项目名称",dataIndex:"name"},

{header:"开发团队",dataIndex:"organization"},

{header:"网址",dataIndex:"homepage"}],
sm:sm,
store:store,

autoExpandColumn:2

});

});
</script>
<body >
<br />
<div id="hello"></div>
<br/><br>
<div id="newTable" style="font-size:12px;" ></div>
<br><br>
<span class="Noprint">
<input type="button" onClick="javascript:AllAreaExcel();" value="导出Excel">
<input type="button" onClick="javascript:OpenWord();" value="导出Word">
<input type=button value=打印 οnclick=document.all.WebBrowser.ExecWB(6,1)> <input type=button value=页面设置 οnclick=document.all.WebBrowser.ExecWB(8,1)>
<input name=Button οnclick=document.all.WebBrowser.ExecWB(7,1) type="button" value="打印预览"><OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
<input type="file" name="file" id="file"/><input type="button" id="excelimport" name="excelimport" value="Excel导入" onClick="javascript:readExcel()" />
</span>
</div>
<script language="javascript" type="text/javascript">
function readExcel()
{

if(document.getElementById("file").value.split(".")[1]=="xls"){
var oXL = new ActiveXObject("Excel.Application");
try{
var oWB = oXL.Workbooks.open(document.getElementById("file").value);
}catch(e){
alert('打开文件失败!');
}
var oSheet = oWB.ActiveSheet;
var table=document.createElement("table");
table.setAttribute("border","1");
table.setAttribute("cellpadding","0");
table.setAttribute("cellspacing","0");
table.setAttribute("border-collapse","collapse");
table.setAttribute("width","60%");
//table.setAttribute("style-fontSize","12");
table.setAttribute("id","mytable");
document.body.appendChild(table);
for(var i=0;i<oSheet.usedrange.rows.count;i++){
var row=table.insertRow(i);
for(var j=0;j<(oSheet.usedrange.cells.count/oSheet.usedrange.rows.count);j++){
//oSheet.Cells(i,j).Font.Size = 10;
//alert(oSheet.Cells(i+1,j+1).value);

if(oSheet.Cells(i+1,j+1).value==null){
row.insertCell(j).appendChild(document.createTextNode(""));
}
else{
row.insertCell(j).appendChild(document.createTextNode(oSheet.Cells(i+1,j+1).value));
}
}
}
var div=document.getElementById("newTable");
div.appendChild(table);
//alert(oSheet.Rows.rows);
// alert(oSheet.Cells(1,1).value);//cell的值
// alert(oSheet.usedrange.cells.count);//cell的值
// alert(oSheet.usedrange.rows.count);//使用的行数
// alert(oWB.Worksheets.count);//得到sheet的个数
oSheet=null;
oWB.close();
oXL=null;
}else{
if(document.getElementById("file").value==""){
window.alert("请选择Excel文件");
return false;
}else{
window.alert("您选择的不是Excel文件");
return false;
}
}
}
//导入Excel
function AllAreaExcel()
{
//创建Excel格式对象
var oXL = new ActiveXObject("Excel.Application");
//var oXL = createOXL();
//获取workbook对象
var oWB = oXL.Workbooks.Add();

//激活当前sheet
var oSheet = oWB.ActiveSheet;
//oSheet.Cells(1,1).Font.Size = 30;

var sel=document.body.createTextRange();

//把表格中的内容移到TextRange中
sel.moveToElementText(hello);

//全选TextRange中内容
sel.select();

//复制TextRange中内容
sel.execCommand("Copy");

//粘贴到活动的EXCEL中
oSheet.Paste();

//设置excel可见属性
oXL.Visible = true;
//oXL.Quit();
}
//导出word
function OpenWord(){

var ExcelSheet = new ActiveXObject('word.Application');
// 使 Excel 通过 Application 对象可见。
ExcelSheet.Application.Visible = true;
var orgNum=ExcelSheet.Documents.Add("",0,1);
var myRange =orgNum.Range(0,1)
var sel=document.body.createTextRange();
var oTblExport =document.getElementById("hello");
if (oTblExport != null) {
sel.moveToElementText(oTblExport);
sel.execCommand('Copy');
document.body.blur();
myRange.Paste();
}
//window.close();
//ExcelSheet.Application.Quit();
}
</script>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值