Wednesday, March 14, 2012

Export data table to Excel

To Export Data table to excel

following code can be useful


SPContext context = SPContext.Current;
System.Web.HttpResponse currentResponse = this.Response;
foreach (DataColumn column in table.Columns)
{
currentResponse.Write(column.ColumnName + "\t");
}
currentResponse.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++) { currentResponse.Write(row[i].ToString().Replace(";", string.Empty) + "\t"); } currentResponse.Write(Environment.NewLine); } currentResponse.ContentType = "text/xls"; currentResponse.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".xls"); currentResponse.End(); currentResponse.Close();
HOPE THIS HELPS SOMEONE

Export Nested SPGridViews to Excel

I spend entire day to resolve bugs for rendering HtmlTextWriter to excel in a visual web part, for each solution you need to modify either web.config or @page directive or override VerifyRenderingInServerForm method on page... It started to spinning my head, then I thought to do something by my own...

1) On ClientClick of link/image/button find GirdView's innerHTML
2) Assign that innerHTML to a hidden field
Add following code to your serverside event

System.Web.HttpContext curContext = System.Web.HttpContext.Current;
curContext.Response.Clear();
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
curContext.Response.Clear();
curContext.Response.Buffer = true;
curContext.Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("YOURFILE", System.Text.Encoding.UTF8) + ".xls");
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.Write('>meta http-equiv=Content-Type content=text/html;charset=UTF-8<');
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
htmlWriter.Write(">TABLE Gridline=\"Both\"<");
htmlWriter.Write(hdnGridInnerHTML.Value.ToString());
htmlWriter.Write(">/TABLE<");
curContext.Response.Write(strWriter.ToString());
curContext.Response.Flush();
curContext.Response.End();

THERE is one Problem still I am facing... If any body can help.
If you open this document it won't show you correctly. BUT if you Save it locally and then open it opens correctly...

HOPE THIS MIGHT HELP SOMEONE

Limit Sharepoint Site Search with Current SPSite Object

Previously I tried a lot to limit my site search within the local site but could not succeeded.

If you want to create a simple site search utility in your web part page.

I found useful link at HERE, Thanks to Josh...

I created a visual web part and added following code


<script language="javascript" type="text/javascript">
function RedirectToSearchResultsPage() {
var searchVal = document.getElementById('SearchTerm').value;
window.location = "searchresults.aspx?k=" + searchVal + "&cs=This Site" + "&u=<%= SPContext.Current.Site.Url %>"; } </script> <div class="listDiv"> <table width="100%"> <tr> <td> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="left" class="listTitle-tms"> Search Site </td> </tr> <tr> <td> <input id="SearchTerm" style="width: 70%" /> <input type="button" onclick="RedirectToSearchResultsPage()" value="Search" /> </td> </tr> </table> </td> </tr> </table> </div>


(REMOVE br Tags)
Hope you find it useful.