[英]Get Names of all Columns in Excel sheet in SpreadSheetLight
I'm trying to populate a combobox with the names of the columns in a spreadsheet. 我正在尝试使用电子表格中列的名称填充组合框。 I'm using the spreadsheetlight library.
我正在使用试算表库。 I can set the cell value using the following code where
A
refers to column name and 1
refers to row name. 我可以使用以下代码设置单元格值,其中
A
表示列名, 1
表示行名。 (Am I right?) (我对吗?)
But how can I get the the name of all columns in all sheets? 但是,如何获得所有工作表中所有列的名称?
SLDocument sl = new SLDocument();
sl.SetCellValue("A1", true);
First, get the last column index using SLWorksheetStatistics
: 首先,使用
SLWorksheetStatistics
获取最后一列索引:
SLWorksheetStatistics stats = sl.GetWorksheetStatistics();
int endColumnIndex = stats.EndColumnIndex;
Then iterate through the columns: 然后遍历各列:
var headers = new List<string>();
for (int i = 1; i <= endColumnIndex; i++){
headers.Add(sl.GetCellValueAsString(1, i));
}
The following will print the values "foo" and "bar" from the column list: 下面将从列列表中打印值“ foo”和“ bar”:
var fileName = "test.xlsx";
var sl = new SLDocument(fileName);
foreach (var sheetName in sl.GetWorksheetNames())
{
SLDocument sheet = new SLDocument(fileName, sheetName);
sheet.SetCellValue("A1", "foo");
sheet.SetCellValue("B1", "bar");
SLWorksheetStatistics stats = sheet.GetWorksheetStatistics();
int endColumnIndex = stats.EndColumnIndex;
var headers = new List<string>();
for (int i = 1; i <= endColumnIndex; i++)
{
headers.Add(sheet.GetCellValueAsString(1, i));
}
foreach (var column in headers)
{
Console.WriteLine(column);
}
Console.ReadKey();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.