Click here to Skip to main content
15,850,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to export Repeater control in excel as it is looking in aspx pages, means with CSS and background images and same formatting.

I some one know the same kindly reply ASAP.

Thanks to all of you.
Posted

1 solution

I don't know of a solution where you could just convert your repeater to excel but I just wanted to show you how I exported my repeater to excel in a recent project...

My repeater contains a gridview in my ItemTemplate then I just loop through my tables and add the styling as I go... Hope this will help you...

List<DataTable> data = GetMyTables();

        using (ExcelPackage pck = new ExcelPackage())
        {
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Sessions");

            int CurrentRow = 1;

            foreach (DataTable dt in data)
            {
                ExcelRange rand = ws.Cells[CurrentRow, 1, CurrentRow, dt.Columns.Count];
                rand.Merge = true;
                ws.Cells["A" + CurrentRow.ToString()].Value = dt.TableName;
                ws.Cells["A" + CurrentRow.ToString()].Style.Font.Bold = true;
                CurrentRow += 2;
                ws.Cells["A" + CurrentRow].LoadFromDataTable(dt, true);

                using (ExcelRange range = ws.Cells[CurrentRow, 1, CurrentRow, dt.Columns.Count])
                {
                    range.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                    range.Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#498af3"));
                    range.Style.Font.Color.SetColor(Color.White);
                }

                using (ExcelRange range = ws.Cells[CurrentRow, 1, CurrentRow + dt.Rows.Count, dt.Columns.Count])
                {
                    range.Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
                    range.Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
                    range.Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
                    range.Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
                }

                CurrentRow += dt.Rows.Count + 2;
            }

            ws.Cells[1, 1, CurrentRow - 1, data[0].Columns.Count].AutoFitColumns();

            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment; filename=file.xlsx");
            Response.BinaryWrite(pck.GetAsByteArray());
            Response.End();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900