Saturday, September 3, 2011

ASP.NET DataTable To Excel


   EXPORT DATA FROM DATATABLE TO EXCEL IN ASP.NET
VB - Code

Public Sub ExportExcel(ByVal Datadt As DataTable)
        Dim dt as new datatable
        dt = Datadt       
        Dim attachment As String = "attachment; filename=" & System.DateTime.Now.[Date].ToShortDateString() & "_SalesReport.xls"
        Response.ClearContent()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/vnd.ms-excel"
        Dim tab As String = ""
        For Each dc As DataColumn In dt.Columns
            Response.Write(tab + dc.ColumnName)
            tab = vbTab
        Next
        Response.Write(vbLf)
        Dim i As Integer
        For Each dr As DataRow In dt.Rows
            tab = ""
            For i = 0 To dt.Columns.Count - 1
                Response.Write(tab & dr(i).ToString())
                tab = vbTab
            Next
            Response.Write(vbLf)
        Next
        Response.[End]()
    End Sub

C# - Code

    public void ExportExcel(DataTable Datadt)
    {
        DataTable dt = new DataTable();
        dt = Datadt;
        string attachment = "attachment; filename="+System.DateTime.Now.Date.ToShortDateString()+"_SalesReport.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/vnd.ms-excel";
        string tab = "";
        foreach (DataColumn dc in dt.Columns)
        {
            Response.Write(tab + dc.ColumnName);
            tab = "\t";
        }
        Response.Write("\n");
        int i;
        foreach (DataRow dr in dt.Rows)
        {
            tab = "";
            for (i = 0; i < dt.Columns.Count; i++)
            {
                Response.Write(tab + dr[i].ToString());
                tab = "\t";
            }
            Response.Write("\n");
        }
        Response.End();
    }

Localizing ASP.NET Application


  Localizing an ASP.NET Application

Globalization is the process of designing and developing applications that function for multiple cultures, and localization is the process of customizing your application for a given culture and locale. The topics in this section describe how to create ASP.NET Web applications that can be adapted to different languages and cultures.

Example for Localization:-

Imports System.Threading.Thread
Imports System.Threading
Public Class Globalization
    Public Sub New()
        ' Creating a Global Culture specific to our application.
        Dim cultureInfo As New System.Globalization.CultureInfo("en-US")
        ' Creating the DateTime Information specific to our application.
        Dim dateTimeInfo As New System.Globalization.DateTimeFormatInfo()
        ' Defining various date and time formats.
        dateTimeInfo.DateSeparator = "/"
        dateTimeInfo.LongDatePattern = "MM/dd/yyyy"
        dateTimeInfo.ShortDatePattern = "MM/dd/yyyy"
        ' Setting application wide date time format.
        cultureInfo.DateTimeFormat = dateTimeInfo
        ' Assigning our custom Culture to the application current thread.
        'Application.CurrentCulture = cultureInfo;
        Thread.CurrentThread.CurrentCulture = cultureInfo
        Thread.CurrentThread.CurrentUICulture = cultureInfo
    End Sub
End Class


Create an instance for the above class in the base form of your project. 
Now the datetime will be treated in the format of  MM/dd/yyyy

Expression Web 4 Add-Ins


Some Expression Web 4 Add-Ins:
Here are some Expression Web add-ins you can use:
Insert Bing Map Add-in by Mike Calvo (read his overview of this add-in)
Expression Web 4 Add-Ins by fellow MVP Ian Haynes
Other Add-Ins