I am a little confounded with Microsoft’s interop Library. It doesn’t seem all that intuitive, and I think if they had just stuck with multidimensional arrays this would have been a much better library. That being said though, I am a little confused about how to write to columns using C#. How would I iterate through a single column and place data into each individual column. Better question, how do I loop through an individual excel column in excel. I am basically attempting to loop through columns and add data to other columns based on the value in the first column. Would I pull all of the data out and put it into some kind of data structure (most likely an array) and then recast it as a Range or something like that? Kind of confused. Here is the code that I use to create new columns that I need to populate with data that I get from other columns in that same row…
public static void setUpSheet(Worksheet wkSheet)
{
Range rng = (Range)wkSheet.get_Range("A1", Type.Missing);
rng.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
XlInsertFormatOrigin.xlFormatFromRightOrBelow);
wkSheet.Range["A1", Type.Missing].Value2 = "R_EMPIID";
Range rng2 = (Range)wkSheet.get_Range("A1", Type.Missing);
rng2.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
XlInsertFormatOrigin.xlFormatFromRightOrBelow);
wkSheet.Range["A1", Type.Missing].Value2 = "PopulationID";
Range rng3 = (Range)wkSheet.get_Range("A1", Type.Missing);
rng3.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
XlInsertFormatOrigin.xlFormatFromRightOrBelow);
wkSheet.Range["A1", Type.Missing].Value2 = "PopPatID";
wkSheet.SaveAs("C:\\CorrectDirectory\\App_Data\\test", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
So I can add a new column and name the column, Now I just need to figure out how to get the right data out of other columns and place them in another column in that row (those columns would be the new columns I just created here). Any ideas?
UPDATE: trying to be more specific
I know you can get a range of values in an excel column. But there is no way of knowing how large the dataset is. For example
(Excel.Range)excelWorksheet.get_Range("A5:A35", Type.Missing);
That would get you a range of columns with data in them. But, lets say you haven’t seen the excel file before hand. How would you loop through ALL of the rows in that column if you don’t know how many rows are in it? Or should you just do a catch all and just do .get_Range(A2:A10000,Type.Missing). And then after that, how would you change the exact same rows but in a different column?
I think the easiest way to deal with the mappings is to create a function to map 0 based integer column to Excel column names: eg. 0, 1, 2, … 26, 27, 28 => A, B, C … AA, AB, AC. (You may or may not need the reverse mapping).
It’s then pretty easy to create a mapping function between Cell addresses and row/column indexes since you know for any cell address <alpha><numeric> becomes <column><row>.
It’s a bit of extra work up front that they didn’t provide index based ranges, but if you start doing advanced formulas you’d be doing this mapping anyways.
—-edited based on updated question—–
I believe
Worksheet.Cells.WidthandWorksheet.Cells.Heightmight be of some use here. It’s possible for a worksheet to contain a lot of empty cells, but it should at least be a starting point.Note: I’m looking at version 12 of the type lib, not sure what version you’re on…