1
我想用某些列自定義我的Excel輸出結果。現在我有出口到Excel工作好,但它顯示太多列。我想只導出某些列。任何想法如何做到這一點?將數據導出到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);
}
}
,我想模型
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,進口日期,拱門日期,並存檔列取出四列。任何想法?謝謝!
爲什麼要重新發明輪子? https://joshclose.github.io/CsvHelper/ – OrElse
讓我試試看看發生了什麼!謝謝! – user6815384
爲什麼要生成一個HTML文件並使用對應於XLSX文件的MIME類型和XLS的文件擴展名來提供它?這真是一場災難!幫你一個忙:使用EPPlus,NPOI,ClosedXML,Aspose或Office XML SDK等實際庫生成真正的Excel文件,並以正確的MIME類型和文件擴展名爲它們提供服務。 – mason