将数据导出到Excel C#

时间:2017-08-15 22:18:57

标签: c# excel

我想用某些列自定义Excel输出结果。现在我导出excel工作正常,但它显示太多列。我想只导出某些列。知道怎么做吗?

  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);
        }
    }

和模型

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; }
}

我想从Excel中删除四列,如SSN,导入日期,Arch Date和Archived列。任何的想法?谢谢!

1 个答案:

答案 0 :(得分: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();
相关问题