我在公司有一项任务,我只允许将JS代码与Jquery / Ajax甚至SheetJS一起使用。此代码应从excel表格中读取,可能是30张。 每个工作表都将显示在单独的HTML分区中。
下面是我的代码,它只能从一张纸读取,而我无法读取所有列。
-如何从同一MS excel工作簿的不同工作表中读取内容?
var xml_file_location = "kpi.xml";
var main_sheet = "Reports";
var Reports = new Array();
function Report() {
this.KPI_Title = "";
this.division_Title = "";
this.Report_Committee = "";
this.committee = "";
this.frequency = "";
this.KPI_Target = "";
this.KPI_Actual = "";
}
function Main() {
$.ajax({
url: xml_file_location,
type: "GET",
dataType: "xml",
data: "",
contentType: "text/xml; charset='utf-8'",
cache: false,
complete: parse_data,
error: function (xml, status, message) {
alert("something " + message + " something");
}
});
}
function get_sheet(data, sheet_name) {
return $(data.responseXML).find("Worksheet[ss\\:Name|='" + sheet_name + "']").contents().find("Row");
}
function parse_data(xData, status) {
var lastKPI = 0;
var rowIndex = 1;
var current_report_row;
var sheet = get_sheet(xData, main_sheet);
// console.log(sheet.text());
sheet.each(function () {
var current_row = $(this).find("Cell");
if ($(this).find("Cell:first[ss\\:Index]").length == 0) {
current_report_row = new Report();
current_report_row.KPI_Title = (current_row[1].firstChild!=null)? current_row[1].firstChild.textContent : "";
current_report_row.division_Title = (current_row[2].firstChild!=null)? current_row[2].firstChild.textContent : "";
current_report_row.Report_Committee = (current_row[3].firstChild!=null)? current_row[3].firstChild.textContent : "";
current_report_row.frequency = (current_row[4].firstChild!=null)? current_row[4].firstChild.textContent : "";
current_report_row.KPI_Target = (current_row[5].firstChild!=null)? current_row[5].firstChild.textContent : "";
current_report_row.KPI_Actual = (current_row[6].firstChild!=null)? current_row[6].firstChild.textContent : "";
}
Reports[rowIndex] = current_report_row;
//console.log(rowIndex);
//console.log(current_report_row);
rowIndex++;
})
var myJSON = JSON.stringify(Reports);
table_head = build_table_header();
table_body = build_table_body();
document.getElementById("report_list").innerHTML = table_head + table_body;
document.getElementById("Recc_list").innerHTML = table_head + table_body;
//document.getElementById("report_list").innerHTML = table_head;
//console.log(Reports);
}
function build_table_header() {
var table_head = "";
table_head += '<thead><tr class="headings">';
var keys = objectValues(Reports[1]);
for (i=0; i<keys.length; i++){
table_head += '<th class="column-title">'+ keys[i] +'</th>'
}
table_head += '</tr></thead>';
return table_head;
}
function build_table_body() {
var table_body = "";
table_body += '<tbody>'
var index = 0;
var i, j;
for (i=0; i<Reports.length; i++){
table_body += '<tr>';
if (i == 0 || i == 1) {continue;}
var keys = objectValues(Reports[i]);
for (j=0;j<keys.length; j++){
table_body += '<td>'+ keys[j] +'</td>';
}
table_body += '</tr>';
}
table_body += '</tbody>';
return table_body
}
function objectValues(obj) {
var res = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
res.push(obj[i]);
}
}
return res;
}
Main();