1.读取所有的shtname
import com.excel;
import console;
function ReadExcelShtName(excelPath) {
// 打开 Excel 文件
var excel2 = com.excel();
var workbook = excel2.Workbooks.Open(excelPath);
// 获取所有工作表的名称
var sheetNames = {};
for(i = 1; workbook.Sheets.Count; 1) {
var sheet = workbook.Sheets.Item(i);
sheetNames[#sheetNames + 1] = sheet.Name;
}
return sheetNames
/*
// 输出所有工作表的名称
for(i, name in sheetNames) {
console.log("Sheet", i, ":", name);
}
*/
// 关闭 Excel 文件
workbook.Close(false);
excel2.Quit();
}
// 使用实例:
var t = ReadExcelShtName(filePath);
if(t != null) {
for(i=1;#t;1){
console.log(t[i]);//显示所有的shtname
}
}
2,指定excel的指定sheet 的所有 列名称
// 返回 指定excel的指定sheet 的所有 列名称(仅仅读取第一行),
function ReadExcelShtColName(excelPath,shtname) {
// 打开 Excel 文件
var excel = com.excel();
var workbook = excel.Workbooks.Open(excelPath);
for(i = 1; workbook.Sheets.Count; 1) {
var sheet = workbook.Sheets.Item(i);
if (sheet.Name = shtname ) {
// 根据工作表名称获取工作表
sheetUse = workbook.Sheets.Item(i);
break
};
}
// 获取第一行的列头
var headers = {};
for (col = 1; sheetUse.UsedRange.Columns.Count; 1) {
//var cell = ; // 第一行,第 col 列
headers[#headers + 1] = sheetUse.Cells(1, col).Value2;
//headers[col] = sheetUse.Cells(1, col).Value2; // 将列头值存入数组
}
return headers
// 关闭 Excel 文件
workbook.Close(false);
excel.Quit();
}