Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

javascript - 使用Javascript将对象数组导出到Excel(Export array of objects into Excel using Javascript)

I'm writing a client side method, that creates an array of objects.I open an existing excel to write the values from the array.

(我正在编写一个客户端方法,该方法创建一个对象数组。我打开一个现有的excel,以从数组中写入值。)

I get the values using getProperty and store in a variable.

(我使用getProperty获取值并将其存储在变量中。)

When I try to write those in the excel, I get "event handler failed with message";" ".

(当我尝试在excel中编写这些代码时,出现“事件处理程序失败并显示消息”;“”。)

Code:

(码:)

var getItemtoExcel=document.thisItem.newItem("ToExcel", "get");
getItemtoExcel=getItemtoExcel.apply();

var arrToExcel = Array();
for (var j=0; j<getItemtoExcel.getItemCount(); j++) 
{
var gotItemForExcel=getItemtoExcel.getItemByIndex(j);
arrToExcel.push(gotItemForExcel);
}
var Excel = new ActiveXObject("Excel.Application");
Excel.Visible = true;
Excel.Workbooks.Open("C:\test.xls");
var offset=0;
var row=2;
for (var c=0; c<arrToExcel.length; c++)
{
var createExcel = arrToExcel[c];
var Number =createExcel.getProperty("nb");
var Type=createExcel.getProperty("type");
var Code=createExcel.getProperty("code");
var State=createExcel.getProperty("state");
Excel.Worksheets("sheet11").Range("A" & row + 1 +  offset).Value = Number;
Excel.Worksheets("sheet11").Range("B" & row + 1 + offset).Value = Type;
Excel.Worksheets("sheet11").Range("C" & row + 1 + offset).Value = Code;
Excel.Worksheets("sheet11").Range("D" & row + 1 + offset).Value = State;
row=row+1;
}
offset=offset+1;
return this;

document.thisItem.newItem() is from ARASPLM.

(document.thisItem.newItem()来自ARASPLM。)

Its the standard used to call an ItemType(Item) in ARAS

(它是在ARAS中调用ItemType(Item)的标准)

  ask by GBI translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i think using this you can get what you want but you need to pass the your Object's value with this that i have mentioned here as (Your Data(Object))

(我认为使用它可以得到您想要的东西,但您需要使用我在这里提到的(您的数据(对象))传递您的对象的价值)

 window.open('data:application/vnd.ms-excel,' + **(Your Data(Object))**); 

here i'm providing simple code for get data into excel format with jquery

(在这里,我正在提供简单的代码,以便通过jquery将数据转换为excel格式)

SAMPLE DEMO

(样本演示)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...