js实现前台导出
2014-04-30 21:28
239 查看
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;
}
实现导入
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;
}
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;
}
相关文章推荐
- rsa实现js前台加密java后台解密
- JS前台加密,java后台解密实现
- 前台js改变Session的值(用ajax实现)
- js实现将json数组显示前台table中
- 5丶electron-vue安装js-xlsx,实现Excel导入导出
- js实现前台动态添加文本框,后台获取文本框内容
- 纯前端利用 js-xlsx 实现 Excel 文件导入导出功能示例
- JS实现导出Excel的五种方法详解【附源码下载】
- 手把手教你用DWR实现js前台调用后台程序而不需要AJAX
- js实现EasyUI-datagrid前台分页
- 转:不用AJAX实现前台JS调用后台C#方法(小技巧)
- js实现一键导出Excel
- JS实现BASE64加密解密-后台加密前台解密案例分析
- js实现EasyUI-datagrid前台分页
- 详解nginx过滤url实现前台js的配置问题
- js实现excel导出
- 不用AJAX实现前台JS调用后台C#方法(小技巧)
- 使用js实现将后台传入的json数据放在前台显示
- JS前台加密,java后台解密实现
- jspdf + highcharts 实现导出存在highcharts图表的html页面