读取Excel文件数据 [英] Read data from Excel files
问题描述
我有一些麻烦,在C#Excel表格中读取。
我有这样的代码,我读了从A与X的每一个细胞
的System.Array myvalues;字符串[] strArray;
Microsoft.Office.Interop.Excel.Range范围=
worksheet.get_Range(A+ i.ToString(),W+ i.ToString());
,而(range.Count!= 0)
{
I ++;
//Console.WriteLine(i);
范围= worksheet.get_Range(A+ i.ToString(),W+ i.ToString());
myvalues =(System.Array的)range.Cells.Value;
strArray = ConvertToStringArray(myvalues);
NAME = clearstr(strArray [1]);
为(INT J = 1; J< = Int32.Parse(number_add_file)* 4; J ++)
{
NAME =;
lang_add =;
价格=;
说明=;
Console.WriteLine(我得到了!);
Microsoft.Office.Interop.Excel.Range range_add =
worksheet.get_Range(X+ i.ToString(),Type.Missing);
的System.Array值=(System.Array的)range_add.Cells.Value;
的String []海峡= ConvertToStringArray(值);
NAME =海峡[0];
lang_add = STR [1];
价格=海峡[2];
说明= STR [3];
Console.WriteLine(名称+
+ lang_add ++价格++说明);
addfile();
}
我的问题是:我怎么能读取下一个4 *号行?基于数字值的Excel
例如:
ABCDEFGHIJ
aaaaa 1 AAAA
F公司单元格的值是1,所以我想读(GHIJ)
若F的单元格的值是2,我想读(GHIJKLMN)
ABCDEFGHIJKLMN
aaaaa 2 AAAAAAAA
F公司格值3:
ABCDEFGHIJKLMNOPQR
aaaaa 3 aaaaaaaaaaaa
这是.NET 4.0:
;
Excel.Application xlApp =新Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(somefile.xls);
Excel.Worksheet xlWorksheet = xlWorkbook.Sheets [1]; //假设它是第一张
Excel.Range xlRange = xlWorksheet.UsedRange; //得到整个使用范围
int值= 0;
如果(Int32.TryParse(xlRange.Cells [1,6] .Value2.ToString(),超时值))//获得从第一行
{
INT numberOfColumnsToRead在F细胞=数值* 4;
为(INT COL = 7;西≤(numberOfColumnsToRead + 7);西++)
{
Console.WriteLine(xlRange.Cells [1,COL] .Value2.ToString()) ; //做任何与价值
}
}
这将打开工作簿并获得工作簿中的第一个工作表。然后,我们得到整个使用范围,并将该范围内的变量。从这里,我们试图解析INT列F排在第一位(这是第6列,它是基于1不是从0开始)。如果解析成功,我们再乘以4这个数字,看看你有多少列需要(在您的文章你说行,但你的例子是列)。我们使用for循环开始在G柱(柱7),并转到列数读+ 7(考虑到我们跳过列)。您将可以自由地做自己想做的价值观,但在这个例子中我只是写他们到控制台的。
I'm having some trouble reading from an Excel spreadsheet in C#. I have this code which I read every cell from A to X.
System.Array myvalues; string[] strArray;
Microsoft.Office.Interop.Excel.Range range =
worksheet.get_Range("A" + i.ToString(), "W" + i.ToString());
while(range.Count!=0)
{
i++;
//Console.WriteLine(i);
range = worksheet.get_Range("A" + i.ToString(), "W" + i.ToString());
myvalues = (System.Array)range.Cells.Value;
strArray = ConvertToStringArray(myvalues);
name = clearstr(strArray[1]);
for ( int j = 1 ; j <= Int32.Parse(number_add_file)*4 ; j++)
{
name = "";
lang_add = "";
price = "";
description = "";
Console.WriteLine("I got in!");
Microsoft.Office.Interop.Excel.Range range_add =
worksheet.get_Range("X" + i.ToString(),Type.Missing);
System.Array values = (System.Array)range_add.Cells.Value;
string[] str = ConvertToStringArray(values);
name = str[0];
lang_add = str[1];
price = str[2];
description = str[3];
Console.WriteLine(name + " "
+ lang_add + " " + price + " " + description);
addfile();
}
My question is: How could i read next 4 * "number" rows in excel based on "number" value ?
For example:
A B C D E F G H I J
a a a a a 1 a a a a
F's cell value is 1 so I would like to read ( G H I J) If F's cell value is 2 the I would like to read ( G H I J K L M N)
A B C D E F G H I J K L M N
a a a a a 2 a a a a a a a a
F's cell value 3 :
A B C D E F G H I J K L M N O P Q R
a a a a a 3 a a a a a a a a a a a a
This is for .NET 4.0:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open("somefile.xls");
Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; // assume it is the first sheet
Excel.Range xlRange = xlWorksheet.UsedRange; // get the entire used range
int value = 0;
if(Int32.TryParse(xlRange.Cells[1,6].Value2.ToString(), out value)) // get the F cell from the first row
{
int numberOfColumnsToRead = value * 4;
for(int col=7; col < (numberOfColumnsToRead + 7); col++)
{
Console.WriteLine(xlRange.Cells[1,col].Value2.ToString()); // do whatever with value
}
}
This will open the workbook and get the first worksheet in the workbook. We then get the entire used range and put that in the range variable. From there, we try to parse the int in column "F" (which is the 6th column, it is 1 based not zero based) in the first row. If that parsing is successful, we then multiply that number by 4 to see how many columns you need (in your post you said rows, but your examples were columns). We use a for loop to start at the G column (column 7) and go to the number of columns to read + 7 (to account for the columns that we skipped). You would be free to do what you want with the values but for this example I just wrote them to the console.
这篇关于读取Excel文件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!