简体   繁体   English

将数据导出到Excel C#

[英]Export data into Excel C#

I want to customize my Excel output result with certain columns. 我想用某些列自定义Excel输出结果。 Right now I have the export to excel working okay, but it shows too many columns. 现在,我可以很好地导出到Excel,但是它显示了太多列。 I wanted to just export certain columns. 我只想导出某些列。 Any idea how to do it? 知道怎么做吗?

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

and the model 和模型

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

I wanted to removed the four columns from the Excel like SSN, Import Date, Arch Date, and Archived columns. 我想从Excel中删除四列,例如SSN,Import Date,Arch Date和Archived列。 Any idea? 任何想法? Thank you! 谢谢!

            // 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM