2012-11-06 6 views
0

Я пытаюсь экспортировать свой GridView в файл Excel 2010 .xlsx. Он работает с .xls Мне было интересно, сможет ли кто-нибудь помочь мне. Вот то, что я до сих пор:Экспорт GridView в Excel

protected void Button2_Click(object sender, EventArgs e) 
    { 
     Response.Clear(); 
     Response.Buffer = true; 
     Response.AddHeader("content-disposition", 
     "attachment;filename=GridViewExport.xls"); 
     Response.Charset = ""; 
     Response.ContentType = "application/vnd.xls"; 
     StringWriter sw = new StringWriter(); 
     HtmlTextWriter hw = new HtmlTextWriter(sw); 
     GridView1.AllowPaging = false; 
     GridView1.DataBind(); 

     GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF"); 

     GridView1.HeaderRow.Cells[0].Style.Add("background-color", "#D1EEEE"); 

     GridView1.HeaderRow.Cells[1].Style.Add("background-color", "#D1EEEE"); 

     GridView1.HeaderRow.Cells[2].Style.Add("background-color", "#D1EEEE"); 

     GridView1.HeaderRow.Cells[3].Style.Add("background-color", "#D1EEEE"); 
     GridView1.HeaderRow.Cells[4].Style.Add("background-color", "#D1EEEE"); 
     GridView1.HeaderRow.Cells[5].Style.Add("background-color", "#D1EEEE"); 
     GridView1.HeaderRow.Cells[6].Style.Add("background-color", "#D1EEEE"); 
     GridView1.HeaderRow.Cells[7].Style.Add("background-color", "#D1EEEE"); 
     GridView1.HeaderRow.Cells[8].Style.Add("background-color", "#D1EEEE"); 
     for (int i = 0; i < GridView1.Rows.Count; i++) 
     { 

      GridViewRow row = GridView1.Rows[i]; 
      row.BackColor = System.Drawing.Color.White; 
      row.Attributes.Add("class", "textmode"); 
      if (i % 2 != 0) 
      { 

       row.Cells[0].Style.Add("background-color", "#FFFFFF"); 
       row.Cells[1].Style.Add("background-color", "#FFFFFF"); 
       row.Cells[2].Style.Add("background-color", "#FFFFFF"); 
       row.Cells[3].Style.Add("background-color", "#FFFFFF"); 

      } 
     } 
     GridView1.RenderControl(hw); 
     string style = @"<style> .textmode { mso-number-format:\@; } </style>"; 
     Response.Write(style); 
     Response.Output.Write(sw.ToString()); 
     Response.Flush(); 
     Response.End(); 

    } 

ответ

1

Ниже статье, использует девять методов для экспорта данных, чтобы преуспеть на asp.net, это должно помочь:

9 Solutions to Export Data to Excel for ASP.NET

+0

Можете ли вы рассказать о том, почему это поможет? – dove

+0

Можете ли вы предоставить некоторый контекст из указанной статьи? Это значительно улучшит ваш ответ. – bytebuster

+0

Спасибо, на самом деле он может экспортировать данные gridview простым кодом, используя бесплатную библиотеку. –

0

Пожалуйста, сделайте следующее:

   protected void ExportExcel(object sender, EventArgs e) 
       { 
        DataTable dt = new DataTable("GridView_Data"); 
        foreach(TableCell cell in GridView1.HeaderRow.Cells) 
        { 
         dt.Columns.Add(cell.Text); 
        } 
        foreach (GridViewRow row in GridView1.Rows) 
        { 
         dt.Rows.Add(); 
         for (int i=0; i<row.Cells.Count; i++) 
         { 
          dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text; 
         } 
        } 
        using (XLWorkbook wb = new XLWorkbook()) 
        { 
         wb.Worksheets.Add(dt); 

         Response.Clear(); 
         Response.Buffer = true; 
         Response.Charset = ""; 
         Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
         Response.AddHeader("content-disposition", "attachment;filename=GridView.xlsx"); 
         using (MemoryStream MyMemoryStream = new MemoryStream()) 
         { 
          wb.SaveAs(MyMemoryStream); 
          MyMemoryStream.WriteTo(Response.OutputStream); 
          Response.Flush(); 
          Response.End(); 
         } 
        } 
       } 

и эти ссылки показывают, шаг за шагом, как это сделать:

http://www.aspsnippets.com/Articles/Solution-ASPNet-GridView-Export-to-Excel-The-file-you-are-trying-to-open-is-in-a-different-format-than-specified-by-the-file-extension.aspx

Смежные вопросы