Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

Files Count of Folder in the Excel

$
0
0

Hi,

Our requirement is to export folder names and respective files count of the folders into the excel.

These folder names are available in one of the list under the column Title.

For this i have written the code as below,now it is exporting only Folder Names,Created Date and Modified Date.

Now want to check if folder is available in the site,if it is available have to export the files count of that folder beside that.

So please share your ideas to achieve the same.

 

 private SPWeb _spWeb
        {

            get
            {

                SPWeb spWeb = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context);

                return spWeb;
            }

        }

 protected void btnExportToExcel_Click(object sender, EventArgs e)

{

SPQuery query = new SPQuery();           

query.ViewFields = "<FieldRef Name='Title'/>";           
            
            SPList employeeList = _spWeb.Lists["SPOC Mailing List"];            
            SPListItemCollection exportListItems = employeeList.GetItems(query);           
            DataTable dt = new DataTable();
            if (exportListItems.Count > 0)
            {
                dt = exportListItems.GetDataTable();
            }
            ExportToExcel(dt);

}

 public void ExportToExcel(DataTable dt)

{

if (dt.Rows.Count > 0)
            {

                //excel file name
                string filename = "WikiReport.xls";

                DataGrid dgGrid = new DataGrid();
                dgGrid.DataSource = dt;
                dgGrid.DataBind();

                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);           

                dgGrid.RenderControl(hw);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", filename));
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                //render the htmlwriter into the response  
                HttpContext.Current.Response.Write(tw.ToString());
                HttpContext.Current.Response.End();
            }

}


Thanks & Regards, Sudheer


Viewing all articles
Browse latest Browse all 11571

Trending Articles