2017-08-15 6 views
1

Ich möchte mein Excel-Ausgabeergebnis mit bestimmten Spalten anpassen. Im Moment habe ich den Export zu Excel funktioniert in Ordnung, aber es zeigt zu viele Spalten. Ich wollte nur bestimmte Spalten exportieren. Irgendeine Idee, wie es geht?Exportieren von Daten in Excel C#

public void ExportFilteredHistorydDataToExcel() 
    { 
     try 
     { 
      // getting session data from the grid 
      var claims = (List<ClaimsArchived>)Session["allclaims"]; 

      GridView gv = new GridView(); 
      gv.DataSource = claims; 
      gv.DataBind(); 

      Response.Clear(); 
      Response.Buffer = true; 
      //Response.Charset = ""; 
      //Response.ContentType = "application/vnd.ms-excel"; 

      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
      Response.AddHeader("content-disposition", "attachment;filename=filteredHistoryClaims.xls"); 

      StringWriter sw = new StringWriter(); 
      HtmlTextWriter htw = new HtmlTextWriter(sw); 

      gv.RenderControl(htw); 

      Response.Output.Write(sw.ToString()); 
      Response.Flush(); 
      Response.End(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("There is a problem exporting data to Excel Spreadsheet.", ex); 
     } 
    } 

und das Modell

public partial class ClaimsArchived 
{ 

    public int Claim_ID { get; set; } 
    public String Provider 
    { 
     get 
     { 
      if (Provider_Group == 73) 
      { 
       return "CEP"; 
      } 
      else if (Provider_Group == 72) 
      { 
       return "CASE"; 
      } 
      else 
      { 
       return "???"; 
      } 
     } 
    } 

    [Display(Name = "Provider Num")] 
    public Nullable<int> Provider_Group { get; set; } 

    public string Facility { get; set; } 
    public string Physician { get; set; } 

    [Display(Name = "Last")] 
    public string Patient_Last { get; set; } 

    [Display(Name = "First")] 
    public string Patient_First { get; set; } 

    [Display(Name = "DOB")] 
    [DataType(DataType.Date)] 
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] 
    public Nullable<System.DateTime> Patient_DOB { get; set; } 

    public int Age 
    { 
     get 
     { 
      DateTime date = DateTime.Today; 
      DateTime birthday = Convert.ToDateTime(Patient_DOB); 

      int tempage = date.Year - birthday.Year; 

      return tempage; 
     } 
    } 

    public string Funding_Type 
    { 
     get 
     { 
      DateTime date = DateTime.Today; 
      DateTime birthday = Convert.ToDateTime(Patient_DOB); 

      int compareAge = date.Year - birthday.Year; 
      if (compareAge > 20) 
      { 
       return "MADDY"; 
      } 
      else 
      { 
       return "RICHIE"; 
      } 
     } 
    } 

    [Display(Name = "Gender")] 
    public string Patient_Gender { get; set; } 

    /// <summary> 
    /// temp remove that from the output and export -- SSNs are sensitive information so that's why disabled in this case 
    /// </summary> 
    [Display(Name = "SSN")] 
    [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:###-##-####}")] 
    public Nullable<int> Patient_SSN { get; set; } 

    [Display(Name = "Zip")] 
    public string Patient_Zip { get; set; } 
    public string Diagnosis { get; set; } 

    [Display(Name = "Total Charges")] 
    public Nullable<decimal> Total_Charges { get; set; } 

    [Display(Name = "EMS Rate")] 
    public Nullable<decimal> Total_EMS { get; set; } 
    public bool Paid { get; set; } 
    //public bool Paid { get; set; } 

    [Display(Name = "Paid Date")] 
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] 
    public Nullable<System.DateTime> Paid_date { get; set; } 

    [Display(Name = "Unique ID")] 
    public Nullable<int> Unique_ID { get; set; } 

    [Display(Name = "Import Date")] 
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] 
    public Nullable<System.DateTime> ImportDate { get; set; } 

    [Display(Name = "Arh Date")] 
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)] 
    public Nullable<System.DateTime> ArchiveDate { get; set; } 

    [Display(Name = "Archived")] 
    public bool ArchiveYesNo { get; set; } 
} 

Ich wollte entfernte die vier Spalten aus der Excel wie SSN, Import Datum, Arch Datum und archivierten Spalten. Irgendeine Idee? Vielen Dank!

+0

Warum das Rad neu erfinden? https://joshclose.github.io/CsvHelper/ – OrElse

+0

Lassen Sie mich versuchen zu sehen, was passiert ist! Vielen Dank! – user6815384

+0

Warum generieren Sie eine HTML-Datei und liefern diese mit dem MIME-Typ, der einer XLSX-Datei und der Dateierweiterung von XLS entspricht? Das ist eine Katastrophe! Tun Sie sich selbst einen Gefallen: Erstellen Sie echte Excel-Dateien mit einer echten Bibliothek wie EPPlus, NPOI, ClosedXML, Aspose oder Office XML SDK, und versehen Sie sie mit dem richtigen MIME-Typ und der richtigen Dateierweiterung. – mason

Antwort

0
  // hiding the column header -- Added code start here --- 
      gv.HeaderRow.Cells[0].Visible = false; 
      // hiding the column detail 
      for (int i = 0; i < gv.Rows.Count; i++) 
      { 
       GridViewRow row = gv.Rows[i]; 
       row.Cells[0].Visible = false; 
      } 
      // ------------------ Added code ended here ------ 

      // following code are the same 
      gv.RenderControl(htw); 

      Response.Output.Write(sw.ToString()); 
      Response.Flush(); 
      Response.End(); 
Verwandte Themen