Sunday, March 26, 2017

ClosedXML – Read/Write Excel File Using C#.NET

Many of Windows/Web applications having requirement that, get the data from database and import into Excel file. There are several ways for developers to write data into Excel using C#.NET.

See the following options
1. Microsoft Excel Interop
2. Microsoft Open XML SDK
3. ClosedXML

Microsoft Excel Interop:

1. Office Interop needs Excel driver to be installed on the machine which is generating excel file
2. Excel installed version must match with the interop assembly version
3. Interop calls are expensive in terms of performance

Microsoft Open XML SDK:


1. Open XML is developed by Microsoft
2. No need to install Excel driver on the machine
3. It is a document format consists of XML parts stored in ZIP package
4. It is the default format for MS Office 2007/2010

ClosedXML:

1. It is wrapper on Open XML specification
2. No need to install Excel driver on the machine
3. It makes easier for developers to create Excel 2007/2010/2013 files
4. It provides Object oriented way to manipulate files without dealing with the hassles of XML documents
5. Writing no.of lines of code is very less compare to Open XML
6. It supports both C#.NET and VB.NET
7. It supports wide range of excel operations like merge cells, group cells, sort cells, header, footer, tables, formulas, images, hyperlinks and etc.
8. It supports .NET framework 3.5, 4.0 , 4.5+
9. Install from nuget using PM> Install-Package ClosedXML

Check wide range of examples @ https://github.com/closedxml/closedxml/wiki
Example:

1. Create Simple excel file

var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
workbook.SaveAs("HelloWorld.xlsx");


2. Export DataTable to excel

DataTable dtTable = new DataTable();
table.Columns.Add("FirstName", typeof(string));
table.Columns.Add("LastName", typeof(string));
table.Rows.Add(“Rama”,”M”);
table.Rows.Add(“Siva”,”S”);
using (XLWorkbook wb = new XLWorkbook())
            {
                wb.Worksheets.Add(dtTable);
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;filename=User.xlsx");
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.End();
                }
            }

Happy Coding :)

Saturday, March 25, 2017

Visual Studio – Productivity Power Tools

Productivity power tool is an outstanding visual studio extension from Microsoft to improve developer productivity since VS 2010.
Recently Microsoft made it as open-source in github https://github.com/Microsoft/VS-PPT
VS Productivity Power Tool team has shipped an extension for every major visual studio version includes VS 2012, 2013, 2015 and 2017.

It is completely free to integrate into any Visual Studio Version.

See the following features which you get it from VS power tools
1. Solution Error Visualizer
2. Match Margin
3. Copy as HTML
4. Ctrl+Click Go to Definition
5. Peek F1
6. Fix Mixed Tabs
7. Align Assignments
8. Middle Click Scroll
9. Syntactic Line Compression
10. Timestamp margin
11. Scroll bar Marker
12. Color Printing


You can download few samples from the url : Download

Happy Coding :)


Tuesday, March 21, 2017

Visual Studio – Performance analysis tool

Diagnosing an application performance issues are crucial before blowing in production.

Usually to investigate performance problems, developer uses a simple stopwatch (System.Diagnostics.Stopwatch) measurement in debugging mode to analyse how long particular code block executed. But, adding stopwatch in every code block is a pain and time consuming action for developers.

To avoid adding stopwatch measurement in code block, Microsoft has introduced a Performance memory analysis tool since Visual Studio 2013.

Steps to use Performance analysis tool

1. Run the application in debugging mode
2. Set Breakpoint in required line of code
3. On Step over of each line, Visual Studio shows elapsed time at end of each line
4. Find more details from Debug Menu --Show Diagnostic Tools

Example

1. As per above screen, line number 56 took 71ms to run includes I/O time and CPU execution time ( which is highlighted with red pen)
2. To get accurate time, execute same code block multiple times using Set Next Statement ( Ctrl+Shift+F10 )
3. To get more details, click on elapsed time and see the following screen


Events window: It shows each line execution time after break point hit
Memory Usage window: It allows us to take snapshot and compare performance
CPU Usage window: It shows complete call-tree with Total CPU time


Happy Coding :)

Sunday, March 12, 2017

C#.NET: Send SMS/SMS Integration using Twilio Rest Client


The Twilio C#/.NET SDK makes it easy to interact with the Twilio API from your .NET application.

The Twilio C# SDK supports .NET applications written in C#, VB.Net, and F# that utilize the .NET Framework version 3.5 or higher and .net core

Steps to configure Twilio

1. Install Twilio DLL references from Nuget package
     Install-Package Twilio
2. Register with Twilio using
     https://www.twilio.com/try-twilio
3. Get the Account SID and Auth Token from Twilio Dashboard
4. Add Account SID and Auth Token into Web.config Appsettings
5. Add funds to the twilio account
6. Register SENDER short code in Twilio
7. Set SENDER short code in web.config Appsettings
8. Activate Country code using
    https://www.twilio.com/console/sms/settings/geo-permissions
9. Add Country code prior to the phone number
10. Create SMSResponse DB Table for logging Twilio responses

You can download SMS helper code from the URL : Download
You can download DBScript from the URL : Download 

Happy Coding :)

Wednesday, March 1, 2017

PACE.JS: Automatic Page Load Progress Bar


Any Web Application requires Page Loading Progress Bar for the best end user experience.

Pace.JS could be the best option to implement page loading progress bar to any web application without any external coding. It is fully automatic page load progress bar.

Pace.JS is simple javascript library and will inspect automatically all AJAX requests, document ready state and event loop log.

Pace.JS works even without JQuery library and it has wide range of in-built progress bar themes.

Just Include following two CDN libraries to work.