Formatovanie taublky pri exporte do excelu   otázka

C#, WinForms

Zdravim,

uz dlsie nemozem vyriesit jeden problem.

Pri exporte do Excelu pouzivam klasicky triedu na export.

   public static void Export(string fileName, GridView gv)
    {
        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.ContentType = "application/vnd.ms-excel"; /* Excel 2003 */
        HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; /* Excel 2007 */

        // Prekodovanie        
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
        HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                // Create a form to contain the grid
                Table table = new Table();
                // pridať riadok hlavička do tabuľky
                if (gv.HeaderRow != null)
                {
                    GridViewExport.PrepareControlForExport(gv.HeaderRow);
                    gv.HeaderRow.Style.Add("background-color", "#FFFFFF");
                    //Ak chcem zmenit formatovanie hlavičky farba pozadia nezávisle jednotlivé bunky
                    for (int index = 0; index < gv.HeaderRow.Cells.Count; index++)
                    {
                        gv.HeaderRow.Cells[index].Style.Add("background-color", "#99CCFF");
                    }
                    table.Rows.Add(gv.HeaderRow);
                }
                // pridať každý z dátových riadkov
                int index2 = 1;               
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExport.PrepareControlForExport(row);
                    //Ak chcem zmenit formatovanie riadkov - každý druhý riadok ofarbiť 
                    if (index2 <= gv.Rows.Count)
                        {
                           if (index2 % 2 != 0)
                            {
                                for (int index3 = 0; index3 < row.Cells.Count; index3++)
                                {
                                    row.Cells[index3].Style.Add("background-color", "#eed0bb");
                                }
                            }
                        }
                        index2++;

                    table.Rows.Add(row);
                }
                // pridať riadok päta do tabuľky
                if (gv.FooterRow != null)
                {
                    GridViewExport.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }
                // vykresliť tabuľku do htmlwriter
                table.RenderControl(htw);
                // vykresliť htmlwriter do odpovede Respose
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

Nemám problém napr. naformátovať hlavičku ani napr. zmeniť farbu pozadia kazdemu druhemu riadku ako to je v kode hore.

Ale neviem ako zmeniť napr. farbu riadka podľa kodu (jeden znak) v nejakom stlpci, ktorý je visible false.

Na samotnej stránke to v gride viem cez RowDataBound ale pri exporte do Excelu uz nie.

Viete poradiť.

Dik za rady

nahlásit spamnahlásit spam 0 odpovědětodpovědět
                       
Nadpis:
Antispam: Komu se občas házejí perly?
Příspěvek bude publikován pod identitou   anonym.
  • Administrátoři si vyhrazují právo komentáře upravovat či mazat bez udání důvodu.
    Mazány budou zejména komentáře obsahující vulgarity nebo porušující pravidla publikování.
  • Pokud nejste zaregistrováni, Vaše IP adresa bude zveřejněna. Pokud s tímto nesouhlasíte, příspěvek neodesílejte.

přihlásit pomocí externího účtu

přihlásit pomocí jména a hesla

Uživatel:
Heslo:

zapomenuté heslo

 

založit nový uživatelský účet

zaregistrujte se

 
zavřít

Nahlásit spam

Opravdu chcete tento příspěvek nahlásit pro porušování pravidel fóra?

Nahlásit Zrušit

Chyba

zavřít

feedback