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

I am tring to import the gridview data to excelsheet but the imported data losses the formatting done in the gridview. How can I solve this problem?

My code is:
C#
public partial class AddingGridDataInExcel : System.Web.UI.Page
   {
       string fileLoc;
       string result;
       protected void Page_Load(object sender, EventArgs e)
       {
           DataSet ds = new DataSet();
           OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\zid120\Timesheet\eTimeTrackLite1.mdb;");
           OleDbDataAdapter oda = new OleDbDataAdapter("select distinct e.EmployeeName,a.AttendanceDate,a.PunchRecords from Employees as e inner join AttendanceLogs as a on e.EmployeeId=a.EmployeeId where e.RecordStatus=1 and e.EmployeeName not like '%[0-9]%' and (a.AttendanceDate >= #1/1/2013#) and (a.AttendanceDate <= #7/30/2013#)", oconn);
           oda.Fill(ds);
           System.Web.UI.WebControls.Style myStyle = new System.Web.UI.WebControls.Style();
           myStyle.ForeColor = System.Drawing.Color.Red;
           myStyle.BackColor = System.Drawing.Color.Yellow;
           MyTable.ApplyStyle(myStyle);
           MyTable.DataSource = ds;
           MyTable.DataBind();
       }

       protected void btnAdd_Click(object sender, EventArgs e)
       {
           Application xlApp = new Application();
           Workbook newWorkbook = xlApp.Application.Workbooks.Add();
           Worksheet newWorksheet = newWorkbook.Sheets.Add();
           newWorksheet.Name = "19Aug2013";        //***** Same as workbook name

           System.Data.DataTable dt = (System.Data.DataTable)((DataSet)MyTable.DataSource).Tables[0];

           int columnCount = dt.Columns.Count;
           int rowCount = dt.Rows.Count;
           int excelRowPosition = 0;
           int excelColumnPosition = 0;
           for (int row = 0; row < rowCount; row++)
           {
               for (int column = 0; column < columnCount; column++)
               {
                   string valueToInsert = dt.Rows[row][column].ToString();
                   excelRowPosition = row + 1; excelColumnPosition = column + 1;

                   newWorksheet.Cells[excelRowPosition, excelColumnPosition] = valueToInsert;

               }
           }
           newWorkbook.SaveAs(@"E:\Shweta.xlsx");
           newWorkbook.Close(true);
           xlApp.Quit();
       }
Posted
Updated 2-Sep-13 20:39pm
v2

1 solution

write the styles that you used for gridview like below to Response.Write(style);
C#
 string style = @"<style>.ui-widget-header {
	border: 1px solid #4297d7;
	background-color: #3B5998;
	color: #ffffff;
	font-weight: bold;
}
.ui-dialog-content {
	position: relative;
	border: 0;
	padding: .5em 1em;
	background: none;
	overflow: auto;
background:#ffffff;
}</style>
";
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment;filename=DataCleaning.xls");
        Response.Charset = "";
        this.EnableViewState = false;

        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

        grddataCleaning.RenderControl(htw);
        
        Response.Write(style);
        Response.Write(sw.ToString());
        Response.End();
 
Share this answer
 
v2

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