Subscribe:

Labels

Thursday, June 20, 2013

Export to excel from Data table in C#

 public void ExporttoExcel()
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");

            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
            //sets font
            HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
            HttpContext.Current.Response.Write("<BR><BR><BR>");
            //sets the table border, cell spacing, border color, font of the text, background, foreground, font height
            HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +
              "borderColor='#000000' cellSpacing='0' cellPadding='0' " +
              "style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
            //am getting my grid's column headers
           // int columnscount = GridView_Result.Columns.Count;
            for (int i = 0; i < grdViewYMP.Columns.Count ; i++)
            {
                HttpContext.Current.Response.Write("<Td>");
                //Get column headers  and make it as bold in excel columns
                HttpContext.Current.Response.Write("<B>");
                HttpContext.Current.Response.Write(grdViewYMP.HeaderRow.Cells[i].Text.Trim());
                HttpContext.Current.Response.Write("</B>");
                HttpContext.Current.Response.Write("</Td>");
            }
            HttpContext.Current.Response.Write("</TR>");
            DataTable dtYMP = ViewState["dtYMP"] as DataTable;
                                           
            foreach (DataRow row in dtYMP.Rows)
            {//write in new row
                HttpContext.Current.Response.Write("<TR>");
                for (int i = 0; i < dtYMP.Columns.Count; i++)
                {
                    HttpContext.Current.Response.Write("<Td>");
                    HttpContext.Current.Response.Write(row[i].ToString());
                    HttpContext.Current.Response.Write("</Td>");
                }

                HttpContext.Current.Response.Write("</TR>");
            }
            HttpContext.Current.Response.Write("</Table>");
            HttpContext.Current.Response.Write("</font>");
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }        

Export to Excel functionality Using Office.Interop.excel referenc

public void ExporExcelusingOffice()
        {
            // creating Excel Application

            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();

            // creating new WorkBook within Excel application

            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);

            // creating new Excelsheet in workbook

            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

            // see the excel sheet behind the program
            app.Visible = true;

            // get the reference of first sheet. By default its name is Sheet1.

            // store its reference to worksheet

            worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];

            worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;

            // changing the name of active sheet

            worksheet.Name = "Exported from gridview";


            // storing header part in Excel

            for (int i = 1; i < grdViewYMP.Columns.Count + 1; i++)
            {
                worksheet.Cells[1, i] = grdViewYMP.HeaderRow.Cells[i - 1].Text.Trim();
            }

            DataTable dtYMP = ViewState["dtYMP"] as DataTable;


            // storing Each row and column value to excel sheet

            for (int i = 0; i < grdViewYMP.Rows.Count - 1; i++)
            {
                for (int j = 0; j < grdViewYMP.Columns.Count; j++)
                {
                    worksheet.Cells[i + 2, j + 1] = dtYMP.Rows[i][j].ToString();
                    //worksheet.Cells[i + 2, j + 1] = grdViewYMP.Rows[i].Cells[j].Text;
                }
            }

            // save the application
            object misValue = System.Reflection.Missing.Value;
            // workbook.SaveAs("c:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            workbook.SaveAs("c:\\YMPoutput.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
            // Exit from the application

            app.Quit();

        }

Monday, June 3, 2013

Create a mysite(personal site) Using the Object Model


Below code is using object model creating the my sites dynamically.


            SPSite site = new SPSite("http://win-rgqt3bch3nk:22929/sites/Test");

            SPWeb web = site.OpenWeb();
            // SPWeb web = SPContext.Current.Web;


            web.AllowUnsafeUpdates = true;

         
            if (ApproveitemId > 0)
            {
                SPList thislist = web.Lists["Requests"];
                SPListItem item = thislist.Items.GetItemById(ApproveitemId);
                string AStatus = item["Approval_Status"].ToString();
                string strYes = "Yes";
                if (AStatus == strYes)
                {
                    string mysiteusername = item["Username"].ToString();
                     //SPUser mysiteuname = web.AllUsers[mysiteusername];

                    SPSecurity.RunWithElevatedPrivileges(delegate()
                         {
                             using (SPSite spSite = new SPSite("http://win-rgqt3bch3nk:38562"))
                             {
                                 spSite.AllowUnsafeUpdates = true;
                                 SPContext.Current.Web.AllowUnsafeUpdates = true;
                                 SPServiceContext siteContext = SPServiceContext.GetContext(spSite);
                                 //UserProfileManager up = new UserProfileManager(siteContext,true);
                                 UserProfileManager up = new UserProfileManager(siteContext);
                                 //string sAccount = "WIN-RGQT3BCH3NK\\test1";
                                 //UserProfile uprof = up.GetUserProfile(sAccount.Trim());
                                 UserProfile uprof = up.GetUserProfile(mysiteusername);
                                 uprof.CreatePersonalSite();
                             }
                         });
                }
            }
         
        }

DB Upgrade for migrating from SharePoint 2007 to SharePoint 2010

Find the following Steps which we used for the Intranet Migration

We used DB Upgrade for migrating the Intranet environment from SharePoint 2007 to SharePoint 2010.
  
  1.    Run the PreUpgrade check in SharePoint 2007
  2.    Resolve the errors
  3.    Installed the SharePoint 2010 in the new SharePoint Farm environment
  4.    Configured the SharePoint farm
  5.     Copied the DBs from SP 2007 DB to SP 2010 DB
  6.   Configured the Service Applications
  7.   Installed the customizations(wsps)
  8.  Created all the Web Applications in SP 2010 with test DB
  9.  Checked the DB upgrade errors using test-mount
  10.  Upgraded the Web App DBs using PowerShell
  11.   Upgraded the Customizations
  12.  Changed the Web Sites(Site Collections and Sub Sites) UI from SP 2007 to SP2010
  13.    Configured the Search Service app
  14.    Configured the User profile app and Synchronized the users from AD