Nice idea kaefer (as usual).
If I have some time, I'd add some WriteRange() method to the ExcelWriter class.
Hi Everybody
I'm a rather discontinuos learner of C# and NET, with the main purpose of Autocad Automation and its Excel connection
These days, I peeped in this thread to try and learn "true life" coding and state-of-the-art "how to"'s,
While learning a lot (and realizing there's a very lot more to - probably too much!), I assumed it could be a useful exercise to make some add-on and see what I actually grasped of it
so I'm attacching a LateBinding solution "ExcelReader" class with minor modifications to let you deal with Excel data written in and read from ranges not beginning at cell (1,1)
In detail, I modifed the following ExcelReader methods:
- ExcelReader main constructor
- SetActiveSheet()
- Peak()
- NewLine()
- Read() (the main method)
- ReadLine() (the main method)
- ReadRange() (both methods)
- RangeToDataTable() (both methods)
all modifications are clearly marked and I also mantained (commented) the corresponding original code lines
to test it I slightly modified Gile's using example as follows:
Using Example:
class Program
{
static void Main(string[] args)
{
using (ExcelWriter xlw = new ExcelWriter(@"C:\Users\Riccardo\Documents\Cartel2.xlsx"))
{
xlw.Clear();
xlw.WriteLine(new object[3] { "foo", "bar", "baz" }, 3, 2); //<------ modified by RICVBA to have a usedrange not beginning at cells(1,1)
xlw.WriteLine(new object[3] { 1, 2, 3 }, 4, 3); //<------ modified by RICVBA to have a usedrange not beginning at cells(1,1)
xlw.Save();
}
using (ExcelReader xlr = new ExcelReader(@"C:\Users\Riccardo\Documents\Cartel2.xlsx"))
{
while (!xlr.StreamEnded)
{
foreach (object obj in xlr.ReadLine())
{
Console.Write("{0} ", obj);
}
Console.WriteLine();
}
//------------------------------------------
//added by RICVBA to test some methods after ExcelReader class modifications
wl(xlr.ReadToEnd()); // should work as it was
wl(xlr.ReadRange(2, 1)); // modified
//------------------------------------------
}
Console.ReadLine();
}
//------------------------------------------
//added by RICVBA
static void wl(object[,] range)
{
for (int i = 0; i < range.GetLength(0); i++)
{
for (int j = 0; j < range.GetLength(1); j++)
{
Console.Write("{0} ", range[i, j]);
}
Console.WriteLine();
}
}
//------------------------------------------
as per my tests, it worked.
if confirmed, same modifications for Dynamic solution's same class should be straightforward
If anybody interested in giving feedbacks, please do.
thank you