2014-11-17 54 views
3

我想導出一個數據集到Excel 2007中,我不能使用正常代碼導出使用內容類型中的MIME類型,如「Response.ContentType =」application/ms- EXCEL 「;」 如果我使用MIME類型爲xls我得到一個警告,當ai嘗試導出,我不能有這個錯誤,因爲客戶端,所以我開始使用EPPlus,但現在我有期待erros,像「ArgumentNullException未處理由用戶代碼」。當我正在debbuging我注意到,在btnExportClick方法變量DS是空的,我認爲在誤差修改是,但我不能明白的地方,這裏是全碼:導出數據集爲Excel 2007 EPPlus

namespace PortalFornecedores 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!this.IsPostBack) 
      { 
       this.BindGrid(); 
      } 
     } 


     public void BindGrid() 
     { 
      using (DataSet ds = new DataSet()) 
      { 
       ds.ReadXml(Server.MapPath("~/Customers.xml")); 
       GridFornecedor.DataSource = ds; 
       GridFornecedor.DataBind(); 
      } 
     } 




     public void btnExportClick(object sender, EventArgs e) 
     { 
      DataTable ds = GridFornecedor.DataSource as DataTable; 
      ExportExcel(ds); 


     } 


     public void ExportExcel(DataTable ds) 
     { 

      using (ExcelPackage pck = new ExcelPackage()) 
      { 
       //Create the worksheet 
       ExcelWorksheet ws = pck.Workbook.Worksheets.Add("SearchReport"); 

       //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1 
       ws.Cells["A1"].LoadFromDataTable(ds, true); 

       //prepare the range for the column headers 
       string cellRange = "A1:" + Convert.ToChar('A' + ds.Columns.Count - 1) + 1; 

       //Format the header for columns 
       using (ExcelRange rng = ws.Cells[cellRange]) 
       { 
        rng.Style.WrapText = false; 
        rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left; 
        rng.Style.Font.Bold = true; 
        rng.Style.Fill.PatternType = ExcelFillStyle.Solid; //Set Pattern for the background to Solid 
        rng.Style.Fill.BackgroundColor.SetColor(Color.Gray); 
        rng.Style.Font.Color.SetColor(Color.White); 
       } 

       //prepare the range for the rows 
       string rowsCellRange = "A2:" + Convert.ToChar('A' + ds.Columns.Count - 1) + ds.Rows.Count * ds.Columns.Count; 

       //Format the rows 
       using (ExcelRange rng = ws.Cells[rowsCellRange]) 
       { 
        rng.Style.WrapText = false; 
        rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left; 
       } 

       //Read the Excel file in a byte array 
       Byte[] fileBytes = pck.GetAsByteArray(); 

       //Clear the response 
       Response.Clear(); 
       Response.ClearContent(); 
       Response.ClearHeaders(); 
       Response.Cookies.Clear(); 

       //Add the header & other information 
       Response.Cache.SetCacheability(HttpCacheability.Private); 
       Response.CacheControl = "private"; 
       Response.Charset = System.Text.UTF8Encoding.UTF8.WebName; 
       Response.ContentEncoding = System.Text.UTF8Encoding.UTF8; 
       Response.AppendHeader("Content-Length", fileBytes.Length.ToString()); 
       Response.AppendHeader("Pragma", "cache"); 
       Response.AppendHeader("Expires", "60"); 
       Response.AppendHeader("Content-Disposition", 
       "attachment; " + 
       "filename=\"ExcelReport.xlsx\"; " + 
       "size=" + fileBytes.Length.ToString() + "; " + 
       "creation-date=" + DateTime.Now.ToString("R") + "; " + 
       "modification-date=" + DateTime.Now.ToString("R") + "; " + 
       "read-date=" + DateTime.Now.ToString("R")); 
       Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 

       //Write it back to the client 
       Response.BinaryWrite(fileBytes); 
       Response.End(); 
      } 
     } 

     public override void VerifyRenderingInServerForm(Control control) 
     { 
      /* Confirms that an HtmlForm control is rendered for the specified ASP.NET 
       server control at run time. */`enter code here` 
     } 
    } 

回答

2

夫婦的事情。這不是真正的問題,更普遍的網絡。

首先,你是網格數據源設置爲數據集在這裏:

using (DataSet ds = new DataSet()) 
{ 
    ds.ReadXml(Server.MapPath("~/Customers.xml")); 
    GridFornecedor.DataSource = ds; 

但後來鑄造到DataTable這裏:

DataTable ds = GridFornecedor.DataSource as DataTable; 

時,你應該先轉換爲數據集,然後得到它的表集合的第一個表。

但是,這仍然不能解決問題,因爲你有一個類別級別的對象,它不會在回傳中存在。你需要使用像這樣的會話或視圖狀態變量:

public void BindGrid() 
{ 
    using (DataSet ds = new DataSet()) 
    { 
     ds.ReadXml(Server.MapPath("~/Customers.xml")); 
     GridFornecedor.DataSource = ds; 
     GridFornecedor.DataBind(); 
     ViewState["GridDataSource"] = ds; 
    } 
} 


public void btnExportClick(object sender, EventArgs e) 
{ 
    //DataTable ds = GridFornecedor.DataSource as DataTable; 
    var ds = ViewState["GridDataSource"] as DataSet; 
    var dt = ds.Tables[0]; 
    ExportExcel(dt); 
} 
+0

工作完美,謝謝 –