Showing posts with label Open Source. Show all posts
Showing posts with label Open Source. Show all posts

NPOI with Excel Table and dynamic Chart

A reader of the blog called Zip wrote a comment on the post Creating Excel spreadsheets .XLS and .XLSX in C#.

This is an excerpt from Zip’s comment:

if I add rows using NPOI in C#, rows added under the table won't be automatically included in the table, and my chart is not updated the way I would like it to be.
How can I work around this problem?

I tried to simulate the problem with a simple spreadsheet and I was getting the same problem stated by Zip, that is, if I added one row just beneath the last row in the table, such added row wasn’t included in Excel’s data table and consequently the chart bound to the table wasn’t updated to reflect the new data.

To workaround this problem, let’s consider the following spreadsheet shown in Figure 1:

NPOI with Excel Table and dynamic Chart 
Figure 1 - NPOI with Excel Table and dynamic Chart

As you see we have a simple Excel data table with a players column that represents the name arguments of the chart, 4 columns for the months that form the category labels arguments (X axis) and the values arguments for the months going from Jan through Apr (Y axis).

Using NPOI to insert a new row in the table shown above we do the following:

// Creating a new row... 0 is the first row for NPOI.
HSSFRow row = sheet.CreateRow(5); // Row 6 in Excel
// Creating new cells in the row... 0 is the first column for NPOI.
row.CreateCell(1).SetCellValue("Eve Paradise"); // Column B
row.CreateCell(2).SetCellValue(4); // Column C
row.CreateCell(3).SetCellValue(3); // Column D
row.CreateCell(4).SetCellValue(2); // Column E
row.CreateCell(5).SetCellValue(1); // Column F 

The result is shown in Figure 2:

NPOI with Excel Table and dynamic Chart - Adding a new row
Figure 2 - NPOI with Excel Table and dynamic Chart - Adding a new row

Figure 2 shows us the problem stated by Zip in his comment. The new row we just added wasn’t included in the table. The chart that is linked to the table won’t update because it isn’t aware of the new row.

How to workaround this problem? That’s the question!

After playing with this case for 4 hours I’ve found a way of doing what Zip asks for.

Here’s how I did it:

Expand your Excel data table to row 10. I expanded only 4 rows just to show you how to workaround NPOI’s current limitation.

To expand your table, click in the minuscule handle in the lower-right corner of the cell occupying the lower-right corner of the table. This handle gives you a way to expand the table. Usually, it’s easier just to add data and let Excel expand the table - what doesn’t work with NPOI. But if you want to add several new rows or columns all at once, the handle is a good way to do it.

After expanding your table save the spreadsheet. It’ll be the template spreadsheet used to create new spreadsheets.

Figure 3 shows how the above spreadsheet looks like when the table is expanded to row 10:

NPOI with Excel Table and dynamic Chart - Expanding the Table
Figure 3 - NPOI with Excel Table and dynamic Chart - Expanding the Table

We can see that row 6 added using NPOI is now part of the table because we expanded the table. The chart now shows the new data but we got a new problem: the chart shows empty (blank series) that are the reflection of the the empty rows we have on the data table - take a look at the chart’s legend for example and you’ll see squares that represent nothing.

How to get over this? Well, we just need to filter the data in the table as shown in

Figure 4:

NPOI with Excel Table and dynamic Chart - Filtering Data (blank series)
Figure 4 - NPOI with Excel Table and dynamic Chart - Filtering Data (blank series)

Filter out players removing the blank rows by unchecking (Blanks) circled in red in Figure 4. Doing so the chart will reflect the change showing only the filtered data as you see in Figure 5:

NPOI with Excel Table and dynamic Chart - Filtered Data (no empty rows)
Figure 5 - NPOI with Excel Table and dynamic Chart - Filtered Data (no empty rows)

Now we have an Excel data table that is filtered (take a look at the funnel symbol) in the Player column. Other difference is that the rows that contain data are marked in blue. Although we have only 4 rows of data being displayed, our table has indeed 8 rows of data because we expanded it. The other 4 rows are hidden because they were filtered for not having any data yet.

Positioning the mouse cursor within the Excel data table, I’ll add a Total Row (option circled in red) in the table so that I can summarize data the way I want for each column as shown in Figure 6:

NPOI with Excel Table and dynamic Chart - Adding Total Row
Figure 6 - NPOI with Excel Table and dynamic Chart - Adding Total Row

With this Excel template spreadsheet we can now use NPOI to fill our sheet with more 4 rows of data. Let’s do it. This is the code I used:

HSSFRow row7 = sheet.CreateRow(6);

row7.CreateCell(1).SetCellValue("David Goliath");
row7.CreateCell(2).SetCellValue(7);
row7.CreateCell(3).SetCellValue(7);
row7.CreateCell(4).SetCellValue(7);
row7.CreateCell(5).SetCellValue(7);

HSSFRow row8 = sheet.CreateRow(7);

row8.CreateCell(2).SetCellValue("Moses of Egypt");
row8.CreateCell(3).SetCellValue(8);
row8.CreateCell(4).SetCellValue(8);
row8.CreateCell(5).SetCellValue(8);
row8.CreateCell(6).SetCellValue(8);

HSSFRow row9 = sheet.CreateRow(8);

row9.CreateCell(1).SetCellValue("David Shepherd");
row9.CreateCell(2).SetCellValue(9);
row9.CreateCell(3).SetCellValue(9);
row9.CreateCell(4).SetCellValue(9);
row9.CreateCell(5).SetCellValue(9);

HSSFRow row10 = sheet.CreateRow(9);

row10.CreateCell(2).SetCellValue("Jesus of Nazareth");
row10.CreateCell(3).SetCellValue(10);
row10.CreateCell(4).SetCellValue(10);
row10.CreateCell(5).SetCellValue(10);
row10.CreateCell(6).SetCellValue(10);
// Forcing formula recalculation so that the Total Row gets updated
sheet.ForceFormulaRecalculation = true;

After filling the spreadsheet we get the result shown in Figure 7:

NPOI with Excel Table and dynamic Chart - Chart updated automatically/dynamically
Figure 7 - NPOI with Excel Table and dynamic Chart - Chart updated automatically/dynamically

This is the workaround! :o)

The rows added with NPOI now are part of the table and are shown in the chart.

As a last hint: remember to expand your Excel data table to the number of rows you think your spreadsheet will store so that the rows added with NPOI get included in the table and the chart gets updated.

Again this is a good proof of what free software as is the case of NPOI can make for us. Even when dealing with more elaborated concepts as is the case of Excel tables and charts NPOI makes it easy to get the job done.

I wish that the next version of NPOI does what Zip wants automatically, that is, recognize rows added under the last row of an Excel table. At least we could have a parameter to let the user define if s/he wants the row to make part of the table or not.

Hope you enjoy this post.

Visual Studio 2008 C# ASP.NET MVC Web Application
You can get the Microsoft Visual Studio Project at:

http://leniel.googlepages.com/NPOIExcelTableChartMvcProject.zip

To try out the code you can use the free Microsoft Visual Web Developer 2008 Express Edition that you can get at: http://www.microsoft.com/express/vwd/Default.aspx

A tribute to Mozilla Firefox nightly builds

Ever wished you could have the latest version of Mozilla Firefox? Think no more. You actually can have it. Keep reading and you'll see how.

On the date of this post the latest version of Firefox that I'm running is the one shown on the following picture (I'm downloading right now the 20080507 nightly build):

 MozillaFirefoxMinefieldVersion

As you can see my current version is the one from April, 30th. Look inside the red rectangle. I don't download the nightly build everyday.

One thing interesting is the name: Minefield. Believe me: It's not that minefield as the name states.

Nightly builds
This is what the folks from the Mozilla developer division say about the nightly builds:

Nightly builds are created most weekdays from the previous day's work, these builds may or may not work. Use them to verify that a bug you're tracking has been fixed.

We make nightly builds for testing only. We write code and post the results right away so people like you can join our testing process and report bugs. You will find bugs, and lots of them. Mozilla might crash on startup. It might delete all your files and cause your computer to burst into flames. Don't bother downloading nightly builds if you're unwilling to put up with problems.

I've been using the nightly builds for a long time and from what I found you can have the latest innovations in browsing technologies without worrying about lots of bugs or crashes. Using a nightly build is perfect. You can use it for your day to day navigation. It's really stable. From what I remember it has crashed 2 or 3 times in a time span of more than 1 year. Besides that, no worries!

Firefox features I like most 
From the innovations and my own tweaks, the ones that I like most are:

- URL auto-completion
One of the greatest features (there are lots of them) is URL auto-completing - they call it "Location bar and auto-complete". Type the title or tag of a page in the location bar to quickly find the site you were looking for in your history and bookmarks. Favicons, bookmark, and tag indicators help you see where the results are coming from.

This is implemented through a lightweight database that stores the URLs already visited. The more you access a URL the high will be its rank in the list of visited URLs. You just start typing the address and instantly you get the URL you're after. You don't need to type the whole thing every time. It's a must. I use it all the time. See it in action:

MozillaFirefoxMinefieldAddressAutoCompleting

- New revamped Download Manager
The Download Manager is fantastic too. You don't need to start your downloads all over again in case you interrupt them. You can resume from the point you stopped. You can check more info about its improvements in the next version of Firefox at the page Download Manager improvements in Firefox 3. See its face:

MozillaFirefoxMinefieldDownloadManager

- More available screen size (this is my own tweak)
More size to see the web page content. You can personalize your browser so that your toolbar buttons don't fill the space you could use to visualize content and more content. Check how I customize my browser so that I have the best fit:

 MozillaFirefoxMinefieldCustomizedToolbar

I've shrunk the above window to show it here, but you have a big space to type your URLs in the location/address bar.

I don't have any toolbar. I just put all the buttons I use most on the left of the address bar and the standard menus on the right. To do this simply right-click on one of the menus and select "Customize...". From there you can change the disposition of the buttons. Click and hold one of the buttons and put it anywhere you want. As I did I put them on the left and right side of the address bar. I just don't use the standard toolbars that are: the Navigation toolbar and the Bookmarks toolbar. To not use them, uncheck them. What you gain? More space.

- Restore Previous Session
Restore Previous Session is the other one that I think is really helpful. You see, while I was composing this post I had to get some screenshots. To do this I should have pressed the key Print Screen on the keyboard but instead I pressed the key Power that is just above the Print Screen one. What happened? My computer hibernated. I did this for three times in a row. The last one I hit the restart button on my machine while it was going to hibernate. I don't know why I did it. I lost all the applications that were in memory. Firefox was one of them. Now I just came back to continue typing this post and opened Firefox again. What did I get? The following dialog:

MozillaFirefoxMinefieldRestorePreviousSession

Clicking on Restore Previous Session, Firefox brought back all the tabs that were open the time the computer was turned off accidentally. Fantastic, isn't it? It can help a lot in case of power failure too.

The change from Internet Explorer to Firefox
I remember that I was reluctant in changing from Internet Explorer to Firefox. I decided to try Firefox one day. I installed and used it a little bit and I didn't like it, so I uninstalled and continued using Internet Explorer. I don't remember exactly when I gave it a second chance, but the fact is that I gave and this time I think it is forever. After playing with it a little bit more I saw how customizable it was through add-ons, etc (e.g.: the tweak I did with the toolbars isn't possible with Internet Explorer).

After the changing I don't want to go back to IE. Firefox is much superior in quality and velocity. You navigate the internet really faster.

Remember one thing though: you should have a version of Internet Explorer in case you need it for a site that doesn't work well with Firefox. Yes, there are sites and forms that don't play well with Firefox yet. If you ever be in such a situation, just open IE and try with it. That's what I do and it has worked fine.

How to get Firefox
If you don't know, Firefox runs on various versions of Microsoft Windows, Mac OS X, Linux, and many other Unix-like operating systems.

You have three options and on the date of this post the Firefox versions you can download are:

The current stable release version
Firefox 2.0.0.14. You can get it at the page Firefox web browser | International versions: Get Firefox in your language.

Beta version
The beta version is Firefox 3 beta 5. You can get it at the page Firefox web browser (beta) | International versions: Get Firefox in your language.

Nightly builds
Nightly builds are the ones I use. Each day we have a new one. You can get it at the latest trunk page.

Final notes
It's impossible to comment on every part of Firefox that I like. I hope that you could have gotten the notion of a few of the capabilities of this splendid piece of software. I think of Firefox as a work of art. In truth it's what it is: a work of art.

Firefox's source code is free software.

The open source community must be recognized as a powerful and expressive one. They are on the scene. Congratulations to everyone that helps make Firefox such an work of art. It's amazing to realize how a collaborative work has evolved impressively during the last years.

Folks, keep up the great job and continue innovating all the time. That's what makes someone like me write a post like this.

References
Mozilla Firefox article at Wikipedia
http://en.wikipedia.org/wiki/Mozilla_Firefox

The History of Mozilla Firefox article at Wikipedia
http://en.wikipedia.org/wiki/History_of_Mozilla_Firefox

mozilla - Developer Central
http://www.mozilla.org/developer/

Complete list of Firefox 2.0.0.14 features
http://www.mozilla.com/en-US/firefox/features.html

Recommended Add-ons :: Firefox Add-ons
https://addons.mozilla.org/en-US/firefox/recommended

Firefox 3 for Developers - Mozilla Developer Center (MDC)
http://developer.mozilla.org/en/docs/Firefox_3_for_developers