Thursday, March 20, 2014

Export GridView to excel in Visual Webpart in SharePoint

Declare GridView and Button as below

<asp:GridView ID="gridViewPermission"  runat="server" >
     </asp:GridView>

<asp:Button ID="buttonExportUsers" runat="server" Text="Export Users" onclick="ButtonExportUsers_Click" OnClientClick="window.setTimeout(function() { _spFormOnSubmitCalled = false; }, 10);"/>

Note the OnClientClick event for button

There are two approaches to export it

1. Export to csv file
2. Export to excel file



1. Export to csv file

            StringBuilder sb = new StringBuilder();
            GridViewRow grHeader = gridViewPermission.HeaderRow;
            int counter = 0;

            foreach (TableCell tc in grHeader.Cells)
            {
                sb.Append("" + tc.Text + " ,");
                counter++;
            }
            sb.AppendLine();

            foreach (GridViewRow gr in gridViewPermission.Rows)
            {
                foreach (TableCell tc in gr.Cells)
                {
                    sb.Append("" + tc.Text + " ,");
                }
                sb.AppendLine();
            }

            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            Response.AddHeader(" content-disposition", " attachment; filename=" + strSite + "_Users_" + strDate + ".csv");
            Response.ContentType = " text/csv";
         
            Response.AddHeader(" Pragma", " public");
            Response.Write(sb.ToString());
            Response.End();


2. Export to excel file

Response.ClearContent();
            Response.AddHeader("content-disposition", " attachment; filename=" + strSite + "_Users_" + strDate + ".xls");
            Response.ContentType = "application/vnd.ms-excel";
            string tab = string.Empty;
            GridViewRow grHeader = gridViewPermission.HeaderRow;
            foreach (TableCell headerCell in grHeader.Cells)
            {
               Response.Write(tab + headerCell.Text );
               tab = "\t";
            }

            Response.Write("\n");
            foreach (GridViewRow gridViewRow in gridViewPermission.Rows)
            {
                tab = string.Empty;
                foreach (TableCell dataCell in gridViewRow.Cells)
                {
                    Response.Write(tab + dataCell.Text.Replace(CommonFunctions.SpaceText,string.Empty) );
                    tab = "\t";
                }
                Response.Write("\n");
            }
            Response.End();

References - http://www.fewlines4biju.com/2011/11/export-gridview-data-to-excel-aspnet.html

Thursday, March 06, 2014

Configure ElasticSearch on Windows


2.      Download the latest java SDK if not installed on the system.

3.      Navigate to url http://www.elasticsearch.org/download/

4.      Download the zip version of elasticsearch for windows.

5.      Extract the files to a folder

6.      Set JAVA_HOME variable if not set

a.       Click on Start, right click on My Computer -> Properties
b.      Click on Advanced system settings



c.       In Advanced tab click on Environment variables



d.      If JAVA_HOME variable is not present here, click on New and enter details for Variable Name and Variable Value. Variable value will be path where java sdk is installed.



e.       Save the value

7.      Navigate to the folder where elastic search files were extracted.

8.      Navigate to bin folder and double click on elasticsearch.bat to run.



9.      Navigate to http://localhost:9200/ url and below screen should appear if successfully installed.





1.  Install Google Chrome plug-in Sense to run elasticsearch queries.