通过Javascript将数据导出到外部Excel文档的函数代码
时间:2024-04-22 22:32:00
function AutomateExcel() {
try {
//Start Excel and get Application object.
var oXL;
try
{
oXL = new ActiveXObject("Excel.Application");
}
catch(e)
{
alert("无法启动Excel!\n\n如果您确信您的电脑中已经安装了Excel," + "那么请调整IE的安全级别。\n\n具体操作:\n\n" + "工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");
return false;
}
//Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = $("#GridView1")[0];
var rows = table.rows;
var columns = table.rows(0).cells;
var codes = "";
//设置标题
var name = "第 <%= _CurrentPage %> 页";
oXL.Caption = name;
oSheet.Name = name;
//设置表头
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 (var i = 2; i <= rows.length; i++) {
codes += "'" + rows(i - 1).cells(0).innerText + "',";
}
codes += "''";
//获取数据并填充数据到EXCEL
$.post("../Handlers/ShippingApplyHandler.ashx",
{ Action: "ExportData", ExportCondition: codes },
function (views) {
if (views != null) {
var beginindex = 1;
var endindex = 1;
for (var i = 0; i < views.length; i++) {
endindex++;
oSheet.Cells(i + 2, 1).Value = views[i].SACode;
oSheet.Cells(i + 2, 2).Value = views[i].AmountSum;
oSheet.Cells(i + 2, 3).Value = views[i].Department;
oSheet.Cells(i + 2, 4).Value = views[i].SalesName;
oSheet.Cells(i + 2, 5).Value = views[i].ContractNo;
oSheet.Cells(i + 2, 6).Value = views[i].CustomerName;
oSheet.Cells(i + 2, 7).Value = views[i].CurrencyCode;
if (views[i].CustomerSchedule != null) {
oSheet.Cells(i + 2, 8).Value = ConvertToJSDate(views[i].CustomerSchedule).Format("yyyy-MM-dd");
}
oSheet.Cells(i + 2, 9).Value = views[i].Country;
oSheet.Cells(i + 2, 10).Value = views[i].VerificationNumber;
oSheet.Cells(i + 2, 11).Value = views[i].InvoiceNumber;
if (views[i].CustomsDate != null) {
oSheet.Cells(i + 2, 12).Value = ConvertToJSDate(views[i].CustomsDate).Format("yyyy-MM-dd");
}
if (i > 0 && views[i - 1].SACode == views[i].SACode) {
oSheet.Range(oSheet.Cells(beginindex, 1), oSheet.Cells(endindex, 1)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 2), oSheet.Cells(endindex, 2)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 3), oSheet.Cells(endindex, 3)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 4), oSheet.Cells(endindex, 4)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 5), oSheet.Cells(endindex, 5)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 6), oSheet.Cells(endindex, 6)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 7), oSheet.Cells(endindex, 7)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 8), oSheet.Cells(endindex, 8)).Merge();
beginindex = endindex;
}
else {
beginindex++;
}
}
}
}, "json");
//设置自动列宽
oSheet.Columns.AutoFit();
//设置excel为可见
oXL.Visible = true;
//将Excel交由用户控制
oXL.UserControl = true;
//禁止提示
oXL.DisplayAlerts = false;
//释放资源
//oXL = null;
//oWB = null;
//oSheet = null;
}
catch (e) {
}
}
标签:数据导出,Excel
0
投稿
猜你喜欢
python中MySQLdb模块用法实例
2024-01-25 01:19:27
PyTorch模型转TensorRT是怎么实现的?
2021-09-01 08:14:02
判断 iframe 是否加载完成的完美方法
2009-09-24 13:35:00
ASP:使用ImageMagickObject组件制作缩略图
2008-10-21 12:21:00
SqlServer常用函数及时间处理小结
2024-01-16 14:32:02
Python深度学习之Keras模型转换成ONNX模型流程详解
2023-01-02 00:32:44
Sanic框架安装与简单入门示例
2022-06-08 19:37:41
python调用虹软2.0第三版的具体使用
2022-03-23 06:32:19
如何用Python获取计算机名,ip地址,mac地址
2021-12-31 05:41:04
个人经验总结:完全卸载MySQL数据库5.0
2009-01-04 13:07:00
MySQL如何快速创建800w条测试数据表
2024-01-19 09:27:28
SQL Join的一些总结(实例)
2024-01-14 04:28:16
Python 实现进度条的六种方式
2022-09-26 02:22:08
Python中关于 *args与**args的用法及说明
2022-04-01 08:24:38
pandas实现导出数据的四种方式
2023-01-03 14:57:57
python实现时间序列自相关图(acf)、偏自相关图(pacf)教程
2023-03-01 06:37:02
SQL Server新特性SequenceNumber用法介绍
2024-01-15 02:38:34
Python Flask 上传文件测试示例
2021-01-08 05:51:34
golang中的时间格式化
2024-04-25 15:29:58
Dreamweaver使用快技法十三则总结
2008-05-01 17:32:00