JS代码将数据导入Excel

时间:2024-03-05 15:56:14

如果在别的浏览器中无法导入,尝试用IE浏览器

function AutomateExcelall(){
try {
oXL = new ActiveXObject(\'Excel.Application\');
}catch (e) {
alert("无法启动Excel!\n\n如果您确信您的电脑中已经安装了Excel,"+"那么请调整IE的安全级别。\n\n具体操作:\n\n"+"工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");
return false;
}
//var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();//新建一个Excel工作簿
var oSheet = oWB.ActiveSheet;//指定要写入内容的工作表为活动工作表
var tResultArray = Grid1Obj.getData();
var hang = tResultArray.length;//取数据源行数
var lie = tResultArray[0].length;//取数据源列数
oSheet.Cells(1, 1).Value = "序号";
oSheet.Cells(1, 2).Value = "单号";
oSheet.Cells(1, 3).Value = "发起日期";
oSheet.Cells(1, 4).Value = "发起人";
oSheet.Cells(1, 5).Value = "发起部门";
oSheet.Cells(1, 6).Value = "发起公司";
oSheet.Cells(1, 7).Value = "事由";
oSheet.Cells(1, 8).Value = "公章名称";
oSheet.Cells(1, 9).Value = "用章类型";
oSheet.Cells(1, 10).Value = "盖章分数";
oSheet.Cells(1, 11).Value = "使用时间";
oSheet.Cells(1, 12).Value = "归还时间";
for (i=0;i<hang;i++){//在Excel中写行
oSheet.Cells(i+2,1).Value = i+1;
for (j=0;j<lie;j++){//在Excel中写列
oSheet.Cells(i+2,j+2).NumberFormatLocal = "@";
oSheet.Cells(i+2,j+2).value = tResultArray[i][j];//向单元格写入值
}
}
oXL.Visible = true;
oXL.UserControl = true;
}