function print(){
try {
var xls = new ActiveXObject ("Excel.Application"); //获取xls对象,需要浏览器支持ActiveX控件和插件全部启用
}
catch(e) {
alert( "要导出该表,您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件");
return "";
}
xls.visible =true; //设置excel为可见,新建一个excel文件并打开
var xlBook = xls.Workbooks.Add;//新建一个文档
var xlSheet = xlBook.Worksheets(1);//取第一个sheet页
var cm = grid.getColumnModel(); //取表格的标题列
var colCount = cm.getColumnCount(); //标题列数量
var temp_obj = [];
//只下载没有隐藏的列(isHidden()为true表示隐藏,其他都为显示)
//临时数组,存放所有当前显示列的下标
for(var i=0;i <colCount;i++){
if(cm.isHidden(i) == true){
}else{
temp_obj.push(i);
}
}
for(var i=1;i <temp_obj.length;i++){
xlSheet.Columns(i).NumberFormatLocal="@";//设置excel中显示字符而不是数字
}
xlSheet.Range(xlSheet.Cells(1,1),xlSheet.Cells(1,temp_obj.length)).mergecells=true;//合并第一行用来当做标题
xlSheet.Range(xlSheet.Cells(1,1),xlSheet.Cells(1,temp_obj.length)).value="单卡历史交易查询报表";
xlSheet.Rows(1).RowHeight = 25;//设置行高
xlSheet.Cells(3,1).Value = "打印日期";
xlSheet.Cells(3,2).Value = new Date().toLocaleDateString();
xlSheet.Cells(5,1).Value = "序号";
for(i=2;i <temp_obj.length+1;i++){
//循环显示列的列标题
xlSheet.Cells(5,i).Value = cm.getColumnHeader(temp_obj[i-1]);
}
//获取表格数据
var store = grid.getStore();
var recordCount = store.getCount();
var view = grid.getView();
for(i=1;i <=recordCount;i++){
for(var j=1;j <temp_obj.length+1;j++){
//EXCEL数据从第二行开始,故row = i + 1;
xlSheet.Cells(i + 5,j).Value =view.getCell(i - 1,temp_obj[j - 1]).innerText;
}
}
xlSheet.Range( xlSheet.Cells(1,1),xlSheet.Cells(recordCount+5,temp_obj.length)).HorizontalAlignment =-4108;//数据区格式居中
xlSheet.Range(xlSheet.Cells(1,1),xlSheet.Cells(recordCount+5,temp_obj.length)).Font.Size = 13;//字体
xlSheet.Columns.AutoFit;
xls.ActiveWindow.Zoom = 75
xls.UserControl = true; //很重要,不能省略,不然会出问题 意思是excel交由用户控制
//释放资源
xls=null;
xlBook=null;
xlSheet=null;
try {
var xls = new ActiveXObject ("Excel.Application"); //获取xls对象,需要浏览器支持ActiveX控件和插件全部启用
}
catch(e) {
alert( "要导出该表,您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件");
return "";
}
xls.visible =true; //设置excel为可见,新建一个excel文件并打开
var xlBook = xls.Workbooks.Add;//新建一个文档
var xlSheet = xlBook.Worksheets(1);//取第一个sheet页
var cm = grid.getColumnModel(); //取表格的标题列
var colCount = cm.getColumnCount(); //标题列数量
var temp_obj = [];
//只下载没有隐藏的列(isHidden()为true表示隐藏,其他都为显示)
//临时数组,存放所有当前显示列的下标
for(var i=0;i <colCount;i++){
if(cm.isHidden(i) == true){
}else{
temp_obj.push(i);
}
}
for(var i=1;i <temp_obj.length;i++){
xlSheet.Columns(i).NumberFormatLocal="@";//设置excel中显示字符而不是数字
}
xlSheet.Range(xlSheet.Cells(1,1),xlSheet.Cells(1,temp_obj.length)).mergecells=true;//合并第一行用来当做标题
xlSheet.Range(xlSheet.Cells(1,1),xlSheet.Cells(1,temp_obj.length)).value="单卡历史交易查询报表";
xlSheet.Rows(1).RowHeight = 25;//设置行高
xlSheet.Cells(3,1).Value = "打印日期";
xlSheet.Cells(3,2).Value = new Date().toLocaleDateString();
xlSheet.Cells(5,1).Value = "序号";
for(i=2;i <temp_obj.length+1;i++){
//循环显示列的列标题
xlSheet.Cells(5,i).Value = cm.getColumnHeader(temp_obj[i-1]);
}
//获取表格数据
var store = grid.getStore();
var recordCount = store.getCount();
var view = grid.getView();
for(i=1;i <=recordCount;i++){
for(var j=1;j <temp_obj.length+1;j++){
//EXCEL数据从第二行开始,故row = i + 1;
xlSheet.Cells(i + 5,j).Value =view.getCell(i - 1,temp_obj[j - 1]).innerText;
}
}
xlSheet.Range( xlSheet.Cells(1,1),xlSheet.Cells(recordCount+5,temp_obj.length)).HorizontalAlignment =-4108;//数据区格式居中
xlSheet.Range(xlSheet.Cells(1,1),xlSheet.Cells(recordCount+5,temp_obj.length)).Font.Size = 13;//字体
xlSheet.Columns.AutoFit;
xls.ActiveWindow.Zoom = 75
xls.UserControl = true; //很重要,不能省略,不然会出问题 意思是excel交由用户控制
//释放资源
xls=null;
xlBook=null;
xlSheet=null;
}
实现导入
oXL = new ActiveXObject("Excel.Application");
oWB = oXL.Workbooks.open(file);//(path);
oSheet = oWB.ActiveSheet;
var colCount = oWB.WorkSheets(1).UsedRange.Cells.Rows.Count;
var colColumn = oWB.WorkSheets(1).UsedRange.Columns.Count;
if(strim(oSheet.Cells(1,1).Value)!="序号" || strim(oSheet.Cells(1,2).Value)!="项目名称" || strim(oSheet.Cells(1,3).Value)!="责任处室"){
alert("模板错误");
oWB.Close();
oXL.Quit();
oSheet = null;
oWB = null;
oXL = null;
return false;
}