Microsoft.Office.Interop.Excel.Application();
string filePath = Server.MapPath(@"~\\Staf\\TestLucene\\" + strFileName.ToString());
Microsoft.Office.Interop.Excel.Workbook workbook = application.Workbooks.Open(filePath, Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
int numSheet = workbook.Sheets.Count;
for (int num = 1; num < numSheet + 1; num++)
{
Microsoft.Office.Interop.Excel.Worksheet sheet = (Worksheet)workbook.Worksheets[num];
Range excelRange = (Range) sheet.get_Range("A1", "H10");
object[,] valueArray = (object[,])excelRange.Value2;
for (int i = 1; i <= valueArray.GetLength(0); i++ )
{
for (int j = 1; j <= valueArray.GetLength(1); j++)
{
string text = Convert.ToString(valueArray[i, j]);
}
}
}
// Close word.
workbook.Close(false);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
application.Application.Quit();
With this code, I just got the information in range A1:H10. Can I make it automatic? Like when I count of existing sheets in workbook?
int numSheet = workbook.Sheets.Count; it means I will always read no matter how much the existing sheet in a workbook. cz I 'll do looping as much as number of that sheet. Can I do this thing in range? so that I should not determining the range A1:H10
Im sorry if its Code is messy, or if i missed to give any info. i just dont know what i should post to help you giving me an answer.
thx,
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Suthish NairRank #92 · 22.8K reputationPosted Feb 2, 2011, 5:26 AM EST
0
Hi,
In excel A1 means Column A and Row Number 1. Same as H10, Coulmn H and Row Number 10.
So you can read/loop the rows in excel sheet upto (new version row count 1048576).
You can also create column list like A,B,C ..... AA, AB,...
using System.Collections.Generic;
Java
inti=0, j = 0, k = 0;
ListarryCols=newList();
for (i = 65; i < 92; i++)
{
if (i == 91) { i = 65; }
if (arryCols.Count < 26)
{
arryCols.Insert(j, ((char)i).ToString()); j = j + 1;
}
else
{
if (k < 26)
{
arryCols.Insert(j, arryCols[k] + ((char)i).ToString());
j = j + 1;
if (i == 90) { k = k + 1; }
}
else
{
break;
}
}
}
Hope this you looking for or something else?
0
Sam HobbsRank #107 · 20.5K reputationPosted Feb 2, 2011, 4:35 PM EST
0
kieky, I probably misunderstand what you are asking. My guess is that you want to process all the data in a worksheet, regardless of how many rows and columns it is. Are you asking how to process all rows and all columns dynamically without the need to put the range in your program at compile-time?
Note that I did not see your code until I was making a reply. Oooops, now I see it. You can make your code easier to read in forum messages if you put " " at the end of each line of source code, but if you do that, you must use the HTML tab of the editor.
0
Suthish NairRank #92 · 22.8K reputationPosted Feb 4, 2011, 9:23 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Feb 2, 2011, 5:26 AM EST
In excel A1 means Column A and Row Number 1. Same as H10, Coulmn H and Row Number 10.
So you can read/loop the rows in excel sheet upto (new version row count 1048576).
You can also create column list like A,B,C ..... AA, AB,...
using System.Collections.Generic;
int i = 0, j = 0, k = 0; List arryCols = new List(); for (i = 65; i < 92; i++) { if (i == 91) { i = 65; } if (arryCols.Count < 26) { arryCols.Insert(j, ((char)i).ToString()); j = j + 1; } else { if (k < 26) { arryCols.Insert(j, arryCols[k] + ((char)i).ToString()); j = j + 1; if (i == 90) { k = k + 1; } } else { break; } } }Hope this you looking for or something else?
Sam HobbsRank #107 · 20.5K reputationPosted Feb 2, 2011, 4:35 PM EST
Note that I did not see your code until I was making a reply. Oooops, now I see it. You can make your code easier to read in forum messages if you put "
" at the end of each line of source code, but if you do that, you must use the HTML tab of the editor.
Suthish NairRank #92 · 22.8K reputationPosted Feb 4, 2011, 9:23 AM EST