|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionAs my searches on the Internet show, there are enormous ways and libraries (mostly commercial) to use Excel files and Charts in a programmatic way. One of the official ways relating to the .NET world is using Primary Interop Assemblies or PIAs as a bridge between Office components and .NET programs. It is true that Interop for many COM types could be generated automatically by "Type Library Importer (Tlbimp.exe)" and works well in many cases, but for some reason Microsoft decided to publish a special set of pre-generated Interops to work with its Office components. These Interops can be downloaded here and all you have to do at this point is to make a reference in your C# or VB project to the Interop. Unfortunately, working with these Interops (specially The library introduced here works as wrapper around official Interop and hides its intricacies. Moreover another way for using Excel is presented which is a new approach inspired by the way some O/R mappers are using nowadays. You will be able to save an instance of a class into an Excel file or read data from different Excel files into an object all at once. BackgroundThe way I used here is different from Visual Studio Tools for Office which involves attaching managed code to documents and the code responds to events that occur in the document or workbook. Also, the method used is NOT creating *.xml files with Office defined schema. We are going to create and use REAL Excel files by our REAL programs. By REAL Excel file, I mean, all files that are directly used by Excel like XLS and XLT and by REAL program I mean any EXE or ASP.NET Web page you may create to answer customers' needs. On the other hand, we are not limited to file types and deployment platforms. The first thing you need is the Your project also needs a reference to Taramon.Exceller.dll (which is provided in this article's download section) and after these two references, you will be ready to use the code. Using the CodeTwo main classes are provided for you in
using (ExcelManager em = new ExcelManager())
{
// Code that uses Excel goes here, like:
em.Open("myfile.xls");
}
As you can see, From now on, every access to Excel would be through the created instance of Four groups of methods are available:
The first two groups are simple enough. An example of using them could be as follows: using (ExcelManager em = new ExcelManager())
{
em.Open("myfile.xls");
em.ActivateSheet("Sheet 2");
em.RenameCurrentSheet("My Sheet");
em.SaveAs("myfile2.xls");
}
For using Cell and Range related methods, you should be familiar with the concept of Category: In Excel, every cell may have two different values according to its formatting. Cell's numeric value represents the real value in the cell. This value can be used in calculations (like any numeric type, for example In my library, I have used an The following excerpt shows how to use a using (ExcelManager em = new ExcelManager())
{
em.Open("myfile.xls");
double? n = em.GetNumericValue("A1");
if (n.HasValue)
{
Console.WriteLine("half of A1 is {0}", n.Value / 2);
}
else
{
Console.WriteLine("A1 is empty");
}
}
You can see that the returning type of In Cell related group of methods, there is only a value setter method - Range methods are similar for Cell group methods with only one difference: you have to specify both start and end addresses, like: using (ExcelManager em = new ExcelManager())
{
em.Open("myfile.xls");
em.SetRangeValue("A1","B10","Hello!");
em.Save();
}
In this group, there are two setter methods:
Map For EaseAnother class of the library is The first thing you need is a map class. This could be one of your program's business objects or a class you like to use as a tool to exchange data with Excel. A map class looks like this: class Student
{
private string _Name;
[FromCell("A2")]
public string Name
{
get { return _Name; }
set { _Name = value; }
}
}
It is a normal class with a single property ( ExcelMapper mapper = new ExcelMapper();
Student info = new Student();
mapper.Read(info, "C:\\Book1.xls");
Console.WriteLine(map.Name);
The above code reads Books.xls and looks for When we plan to write a property to a cell, // Map class:
class Map
{
private ArrayList _Range;
[ToRange("A1", "A4")]
public ArrayList Range
{
get { return _Range; }
set { _Range = value; }
}
}
// Somewhere in program:
ExcelMapper mapper = new ExcelMapper();
Map map = new Map();
map.Range = new System.Collections.ArrayList();
map.Range.Add(10);
map.Range.Add(11);
map.Range.Add(12);
map.Range.Add(13);
map.Range.Add(14);
mapper.Write(map, "C:\\Book1.xls");
Accessing cells in different sheets would be easy. Just add class Map
{
private string _Test;
[UseSheet("Sheet 2")]
[FromCell("A1")]
public string Test
{
get { return _Test; }
set { _Test = value; }
}
}
You can see that working with Excel cells and sheets is so easy and straightforward with this library. Points of InterestSince I have written this library (which was almost two years ago), I found that it can answer 90 percent of my Excel-related needs. Other special requirements could be added to the library with ease. For example, one may need saving charts as images which would be few lines in the History
|
||||||||||||||||||||||