使用C#到阵列读取Excel第一列 [英] Read Excel First Column using C# into Array
问题描述
我试图在第一列的值读入的阵列。什么是做到这一点的最好办法?下面是我到目前为止的代码。基本上我试图让数据的范围该列,所以我可以拉单元格的值到系统阵列。
Microsoft.Office.Interop.Excel.Application xlsApp =新Microsoft.Office.Interop.Excel.Application();
如果(xlsApp == NULL)
{
Console.WriteLine(EXCEL无法启动,请检查你的办公室安装和项目引用是正确的。);
返回NULL;
}
//xlsApp.Visible = TRUE;
工作簿WB = xlsApp.Workbooks.Open(文件名,0,真实,5,,,真实,XlPlatform.xlWindows,\t,假的,假的,0,真);
表表= wb.Worksheets;
表WS =(表)sheets.get_Item(1);
// ***符这里***
ListColumn柱= ws.ListObjects [1] .ListColumns [1];
范围范围= column.DataBodyRange;
的System.Array myvalues =(System.Array的)range.Cells.Value;
下面是我最后用得到它工作。一旦你知道,其实列返回一个范围,其存储的方式似乎编译和运行正常。这里是我的ExcelReader类的工作方式。我打算使用此对webdriver的测试数据驱动。
公共静态字符串[] FirstColumn(字符串文件名)
{
Microsoft.Office.Interop.Excel.Application xlsApp =新Microsoft.Office.Interop.Excel.Application();
如果(xlsApp == NULL)
{
Console.WriteLine(EXCEL无法启动,请检查你的办公室安装和项目引用是正确的。);
返回NULL;
}
//显示Excel,以便你可以看到发生了什么
//xlsApp.Visible = TRUE;
工作簿WB = xlsApp.Workbooks.Open(文件名,
0,真实,5,,,真实,XlPlatform.xlWindows,\t,假的,假的,0,真);
表表= wb.Worksheets;
表WS =(表)sheets.get_Item(1);
范围firstColumn = ws.UsedRange.Columns [1];
的System.Array myvalues =(System.Array的)firstColumn.Cells.Value;
的String [] = strArray&myvalues.OfType LT;对象>()选择(O => o.ToString())。ToArray的()。
返回strArray;
}
I'm trying to read in the values of the first column into an array. What's the best way to do that? Below is the code I have so far. Basically I'm trying to get the range of data for that column so I can pull the cell values into a system array.
Microsoft.Office.Interop.Excel.Application xlsApp = new Microsoft.Office.Interop.Excel.Application();
if (xlsApp == null)
{
Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
return null;
}
//xlsApp.Visible = true;
Workbook wb = xlsApp.Workbooks.Open(filename, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true);
Sheets sheets = wb.Worksheets;
Worksheet ws = (Worksheet)sheets.get_Item(1);
//***Breaks Here***
ListColumn column = ws.ListObjects[1].ListColumns[1];
Range range = column.DataBodyRange;
System.Array myvalues = (System.Array)range.Cells.Value;
Here is what I ended up using to get it to work. Once you know that Columns actually returns a range, storing it that way seems to compile and run fine. Here is the working method in my ExcelReader class. I plan to use this for test driven data on WebDriver.
public static string[] FirstColumn(string filename)
{
Microsoft.Office.Interop.Excel.Application xlsApp = new Microsoft.Office.Interop.Excel.Application();
if (xlsApp == null)
{
Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
return null;
}
//Displays Excel so you can see what is happening
//xlsApp.Visible = true;
Workbook wb = xlsApp.Workbooks.Open(filename,
0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true);
Sheets sheets = wb.Worksheets;
Worksheet ws = (Worksheet)sheets.get_Item(1);
Range firstColumn = ws.UsedRange.Columns[1];
System.Array myvalues = (System.Array)firstColumn.Cells.Value;
string[] strArray = myvalues.OfType<object>().Select(o => o.ToString()).ToArray();
return strArray;
}
这篇关于使用C#到阵列读取Excel第一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!