2
正在使用以下函數導出Excel文件。它工作正常。我從SQL數據庫檢索記錄到數據表,並導出Excel工作表。在導出之前添加標題Excel
public ActionResult ExporttoExcel()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Connection string here");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Exportxcel", con);
dt.Load(cmd.ExecuteReader());
int total = 0;
foreach (DataRow row in dt.Rows)
{
int salaryvalue = Convert.ToInt32(row["Salary"]);
total = salaryvalue + total;
}
dt.Rows.Add(new object[] { "", "Total", total });
if (dt.Rows.Count > 0)
{
string filename = "ExcelExport.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
dgGrid.RenderControl(hw);
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
Response.Write(tw.ToString());
Response.End();
}
return View(dt);
}
我的問題是我需要的值綁定之前增加兩個冠軍?如何做到這一點?我需要添加標題,如下面的屏幕Shot.How筆者要做到這一點?
後,這個工作....謝謝Alott –