How to read huge Excel file using C#

Reading a Excel file is easy using the SpreadsheetDocument and SheetData classes but this method uses a lot of memory if the Excel file is big.
This is due to the fact that to get the sheet data the framework loads the entire file into memory.

To read huge files it is better to use the OpenXmlReader.

Here’s an example usage of the OpenXmlReader (it also consider the SharedString)

To loop the Excel’s rows

The OpenXmlReader uses less memory, but it is slow.
For faster reading you can use the ExcelDataReader, it is available also as a NuGet package.

How to use the BASS audio library in UWP development

The BASS audio library is a solid library to decode and play many audio formats, supports gapless playback (using the mixer, not used in this example) and equalizer (not used in this example).

The library is available also for Windows Store development.

To reference the BASS audio library in your UWP project remember to add conditional references based on platform in the csproj file

Let’s see how we can use the library in a background audio player.

First we need the background task. Here we initialize the BASS audio library, and free it when the task ends. Since we are going to use BASS only to decode the audio we can use the “no sound” device.

Now we need to play the file when the foreground app asks for it. Communication between the foreground app and the background task is made through messages.

When the background task receives a “file” request message it opens the file using BASS (we’ll see later how) and sets the media source of the bacgkround media player.

The BassMediaSource class implements IMediaSource and is the one responsible of opening and playing the file.

The code is quite simple: we need to create the decode stream for the BASS audio library and listen to the MediaSource events (Closed, Starting, SampleRequested).

The SampleRequested event occurs when then audio player needs a decoded sample to play. Here we just need to decode the requested data and return it to the player.

The application lets you select a file from your music library (files outside of your music library won’t work because the application does not have access to external folders).

Here’s the full source code of the test program (remember to set your BASS registration info)

Download source code

Have fun! šŸ˜‰

Convert IANA timezones to Windows

The IANA timezones database is probably the most used standard to reference a timezone by name (a lot of libraries use it).

The problem comes when you need to use these timezones informations in a C# program: Windows timezones have different names and there’s no standard method to convert a IANA timezone to a Windows one.

Informations about the relation between IANA and Windows timezonesĀ  are accessible here:
http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml

Looking at the data is clear that you can convert from a IANA timezone to a Windows one, but a Windows timezone cannot be converted to a single IANA timezone. This is because Windows groups more than one timezone under the same name.

I made a test project to demonstrate how to convert timezones: just deserialize the data and look for the corresponding timezone.

You can download the test project here: IANATimezonesConverter.zip