2017-09-01 33 views
0

在將GridView數據寫入Excel之前,我試圖將文本「這是我的客戶信息」打印到Excel文件中。該文本應該在最上面,剩餘的數據應該顯示在本文下面。如何在將網格數據寫入Excel之前打印文本

任何人都可以幫助我嗎?我該怎麼做?請檢查我目前的代碼。該代碼只是將我的GridView數據寫入Excel文件。

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

       //To Export all pages 
       GridView1.AllowPaging = false; 

       Search(null, null); 

       // GridView1.HeaderRow.BackColor = Color.White; 
       foreach (TableCell cell in GridView1.HeaderRow.Cells) 
       { 
        cell.BackColor = GridView1.HeaderStyle.BackColor; 
       } 
       foreach (GridViewRow row in GridView1.Rows) 
       { 
        // row.BackColor = Color.White; 
        foreach (TableCell cell in row.Cells) 
        { 


         if (row.RowIndex % 2 == 0) 
         { 
          cell.BackColor = GridView1.AlternatingRowStyle.BackColor; 
         } 
         else 
         { 
          cell.BackColor = GridView1.RowStyle.BackColor; 
         } 
         cell.CssClass = "textmode"; 

        } 


        //table.Rows.Add(title); 

       } 
       GridView1.Columns[7].Visible = false; 
       GridView1.RenderControl(hw); 

       //style to format numbers to string 
       string style = @"<style> .textmode { } </style>"; 
       Response.Write(style); 
       Response.Output.Write(sw.ToString()); 
       Response.Flush(); 
       Response.End(); 
      } 
     } 
+0

文本添加到GridView然後把在Excel中的網格?你有一個標題行,你想在哪裏顯示文字? –

+0

這段文字應該在excel的第一行。我不想在gridview中顯示這個文本, – aniltc

+0

我給你的建議是使用Microsoft Access Redistributables(https://stackoverflow.com/questions/23041021/how-to-write-some-data-to-excel-file -xlsx)或DocumentFormat.OpenXML(https://www.nuget.org/packages/DocumentFormat.OpenXml/)或某種類型的OpenXML包裝器來編寫文檔。通過自己直接寫入文件來創建它們是在尋求麻煩。 –

回答

0

歸結起來

Response.Write("Some cool text added before the grid"); 

Response.Output.Write(sw.ToString()); 

之前。

using System; 
using System.Drawing; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.IO; 
using System.Collections.ObjectModel; 


namespace AddTextToDataGridExportExcel_46000670 
{ 
    public partial class Default : System.Web.UI.Page 
    { 
     static ObservableCollection<gridEntry> gridDataSource = new ObservableCollection<gridEntry>(); 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      fillDataSource(); 
      initializeGridView(); 
     } 

     private void fillDataSource() 
     { 
      gridDataSource.Add(new gridEntry(1)); 
      gridDataSource.Add(new gridEntry(2)); 
      gridDataSource.Add(new gridEntry(3)); 
      gridDataSource.Add(new gridEntry(4)); 
      gridDataSource.Add(new gridEntry(5)); 
     } 
     private void initializeGridView() 
     { 
      GridView1.DataSource = gridDataSource; 
      GridView1.DataBind(); 
     } 


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

       //To Export all pages 
       GridView1.AllowPaging = false; 
       addRowToGrid();//add text within the grid 
       initializeGridView(); 

       GridView1.HeaderRow.BackColor = Color.White; 
       foreach (TableCell cell in GridView1.HeaderRow.Cells) 
       { 
        cell.BackColor = GridView1.HeaderStyle.BackColor; 
       } 
       foreach (GridViewRow row in GridView1.Rows) 
       { 
        row.BackColor = Color.White; 
        foreach (TableCell cell in row.Cells) 
        { 
         if (row.RowIndex % 2 == 0) 
         { 
          cell.BackColor = GridView1.AlternatingRowStyle.BackColor; 
         } 
         else 
         { 
          cell.BackColor = GridView1.RowStyle.BackColor; 
         } 
         cell.CssClass = "textmode"; 
        } 
       } 

       GridView1.RenderControl(hw); 

       //style to format numbers to string 
       string style = @"<style> .textmode { } </style>"; 
       Response.Write(style); 
       Response.Write("blah blah blah");//add text before the grid 
       Response.Output.Write(sw.ToString()); 
       Response.Flush(); 
       Response.End(); 
      } 
     } 

     public override void VerifyRenderingInServerForm(Control control) 
     { 
      /* Verifies that the control is rendered */ 
     } 


     private void addRowToGrid() 
     { 
      gridDataSource.Insert(0, new gridEntry { col1 = "this is my added text" }); 
     } 

     private GridView addRowToGridAgain(GridView gv) 
     { 
      return gv; 
     } 

     protected void btn_ClickMe_Click(object sender, EventArgs e) 
     { 
      ExportToExcel(null, null); 
     } 
    } 

    public class gridEntry 
    { 
     public string col1 { get; set; } 
     public string col2 { get; set; } 
     public string col3 { get; set; } 
     public string col4 { get; set; } 
     public string col5 { get; set; } 
     public string col6 { get; set; } 
     public string col7 { get; set; } 
     public string col8 { get; set; } 

     public gridEntry() 
     { 
      col1 = string.Empty; 
      col2 = string.Empty; 
      col3 = string.Empty; 
      col4 = string.Empty; 
      col5 = string.Empty; 
      col6 = string.Empty; 
      col7 = string.Empty; 
      col8 = string.Empty; 
     } 

     public gridEntry(int index) 
     { 
      col1 = "col1 " + index.ToString(); 
      col2 = "col2 " + index.ToString(); 
      col3 = "col3 " + index.ToString(); 
      col4 = "col4 " + index.ToString(); 
      col5 = "col5 " + index.ToString(); 
      col6 = "col6 " + index.ToString(); 
      col7 = "col7 " + index.ToString(); 
      col8 = "col8 " + index.ToString(); 
     } 
    } 

} 

下面是您的片段,固定

  1. //GridView1.Columns[7].Visible = false;被扔的錯誤,所以我評論一下。這是由你來弄清楚,或提出新問題吧
  2. 不知道是什麼Search(null, null);呢,所以我評論它

    protected void ExportToExcelPosterWay(object sender, EventArgs e) 
    { 
        Response.Clear(); 
        Response.Buffer = true; 
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls"); 
        Response.Charset = ""; 
        Response.ContentType = "application/vnd.ms-excel"; 
        using (StringWriter sw = new StringWriter()) 
        { 
         HtmlTextWriter hw = new HtmlTextWriter(sw); 
    
         //To Export all pages 
         GridView1.AllowPaging = false; 
    
         //Search(null, null); 
         initializeGridView(); 
    
         // GridView1.HeaderRow.BackColor = Color.White; 
         foreach (TableCell cell in GridView1.HeaderRow.Cells) 
         { 
          cell.BackColor = GridView1.HeaderStyle.BackColor; 
         } 
         foreach (GridViewRow row in GridView1.Rows) 
         { 
          // row.BackColor = Color.White; 
          foreach (TableCell cell in row.Cells) 
          { 
    
    
           if (row.RowIndex % 2 == 0) 
           { 
            cell.BackColor = GridView1.AlternatingRowStyle.BackColor; 
           } 
           else 
           { 
            cell.BackColor = GridView1.RowStyle.BackColor; 
           } 
           cell.CssClass = "textmode"; 
    
          } 
    
    
          //table.Rows.Add(title); 
    
         } 
         //GridView1.Columns[7].Visible = false; 
         GridView1.RenderControl(hw); 
    
         //style to format numbers to string 
         string style = @"<style> .textmode { } </style>"; 
         Response.Write(style); 
         Response.Write("Some cool text added before the grid"); 
         Response.Output.Write(sw.ToString()); 
         Response.Flush(); 
         Response.End(); 
        } 
    }