2013-06-13 50 views
-1

我最近开始在代码Visual Studio 2010从视觉 工作室  2005.我需要代码从数据网格导出到Excel。在视觉 工作室  2005年使用以下代码。Datagrid在ASP.NET C#中的Excel - Visual Studio 2010

Response.Clear(); 
    Response.AddHeader("content-disposition", "attachment;filename=dgd.xls"); 
    Response.Charset = ""; 
    Response.ContentType = "application/vnd.xls"; 
    System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
    dgd.Visible = true; 
    dgd.RenderControl(htmlWrite); 
    Response.Write(stringWrite.ToString()); 
    Response.End(); 

这并不产生相同的结果在Visual Studio中头没有对齐的列。数据网格中的图片不会在Excel中获取,并且数据网格中的链接无法正确显示。什么会更好的代码?

回答

0
Add following code after htmlwriter line 


`if (dtDetails.Rows.Count > 0) 
      { 
       for (int i = 0; i < gvProduction.HeaderRow.Cells.Count; i++) 
       { 
        gvProduction.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1"); 
       } 
       int j = 1; 
       //This loop is used to apply stlye to cells based on particular row 
       foreach (GridViewRow gvrow in gvProduction.Rows) 
       { 
        gvrow.BackColor = Color.White; 
        if (j <= gvProduction.Rows.Count) 
        { 
         if (j % 2 != 0) 
         { 
          for (int k = 0; k < gvrow.Cells.Count; k++) 
          { 
           gvrow.Cells[k].Style.Add("background-color", "#EFF3FB"); 
          } 
         } 
        } 
        j++; 
       } 
       gvProduction.RenderControl(hw); 
       Response.Write(sw.ToString()); 
       Response.End(); 
      }` 
0

您可以使用以下代码。

导出按钮点击:

FileInfo FI = new FileInfo(Path); 
StringWriter stringWriter = new StringWriter(); 
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter); 
DataGrid DataGrd = new DataGrid(); 
DataGrd.DataSource = dt1; 
DataGrd.DataBind(); 

DataGrd.RenderControl(htmlWrite); 
string directory = Path.Substring(0, Path.LastIndexOf("\\")); // GetDirectory(Path); 
if (!Directory.Exists(directory)) 
{ 
    Directory.CreateDirectory(directory); 
} 

System.IO.StreamWriter vw = new System.IO.StreamWriter(Path, true); 
stringWriter.ToString().Normalize(); 
vw.Write(stringWriter.ToString()); 
vw.Flush(); 
vw.Close(); 
WriteAttachment(FI.Name, "application/vnd.ms-excel", stringWriter.ToString()); 

代码编写附件:

public static void WriteAttachment(string FileName, string FileType, string content) 
{ 
    HttpResponse Response = System.Web.HttpContext.Current.Response; 
    Response.ClearHeaders(); 
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName); 
    Response.ContentType = FileType; 
    Response.Write(content); 
    Response.End(); 
} 

更多参考资料,见Export Gridview Data to Excel in ASP.NET

0

以下代码可能会帮助您使用gridview编写Excel工作表。

// Function to export datagridview to excel sheet 
// excel_file contains the path to the excel file. 
public void export_to_excel(DataGridView dgv, string excel_file) 
{ 
    int cols; 

    //Open file 
    StreamWriter wr = new StreamWriter(excel_file); 

    //Determine the number of columns and write columns to file 
    cols = dgv.Columns.Count; 
    for (int i = 0; i < cols; i++) 
    { 
     wr.Write(dgv.Columns[i].HeaderText.ToString().ToUpper() + "\t"); 
    } 
    wr.WriteLine(); 

    //Write rows to the Excel file 
    for (int i = 0; i < (dgv.Rows.Count - 1); i++) 
    { 
     for (int j = 0; j < cols; j++) 
     { 
      if (dgv.Rows[i].Cells[j].Value != null) 
       wr.Write(dgv.Rows[i].Cells[j].Value + "\t"); 
      else 
      { 
       wr.Write("\t"); 
      } 
     } 
     wr.WriteLine(); 
    } 

    //Close file 
    wr.Close(); 
} 

此外,你可以通过博客文章How to Export Data to Excel from an ASP.NET Application + Avoid the File Format Differ Prompt

0

我在我们的项目中使用相同的。

private void ExportToExcel(DataTable dt) 
    { 
     if (dt.Rows.Count > 0) 
     { 
      string filename = "DownloadReport.xls"; 
      System.IO.StringWriter tw = new System.IO.StringWriter(); 
      System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); 
      DataGrid dgGrid = new DataGrid(); 
      dgGrid.DataSource = dt; 
      dgGrid.DataBind(); 

      //Get the HTML for the control. 
      dgGrid.RenderControl(hw); 
      //Write the HTML back to the browser. 
      Response.ContentType = "application/vnd.ms-excel"; 
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ""); 
      this.EnableViewState = false; 
      Response.Write(tw.ToString()); 
      Response.End(); 
     } 
    } 

希望它可以帮助你