C# DataTable在表中添加其他列

C# DataTable在表中添加其他列,c#,datatable,C#,Datatable,我创建了这个表,结果如下 我用于创建此表的代码如下所示。 这对我来说是正确的 DataTable dt1 = new DataTable("Table1"); dt1.Columns.Add("Num", typeof(Int32)); dt1.Columns.Add("date", typeof(string)); dt1.Columns.Add("name", typeof(string)); dt1.Columns.Add("product1a", typeof(Int32)); dt

我创建了这个表,结果如下

我用于创建此表的代码如下所示。 这对我来说是正确的

DataTable dt1 = new DataTable("Table1");

dt1.Columns.Add("Num", typeof(Int32));
dt1.Columns.Add("date", typeof(string));
dt1.Columns.Add("name", typeof(string));
dt1.Columns.Add("product1a", typeof(Int32));
dt1.Columns.Add("product1b", typeof(Int32));
dt1.Columns.Add("total1", typeof(Int32));
dt1.Columns.Add("product2a", typeof(Int32));
dt1.Columns.Add("product2b", typeof(Int32));
dt1.Columns.Add("product2c", typeof(Int32));
dt1.Columns.Add("total2", typeof(Int32));

object[] o1 = { 1, "10/10/2017", "name 1", 1, 2, 3, 1, 1, 1, 3 };
object[] o2 = { 2, "20/10/2017", "name 2", 2, 2, 4, 2, 1, 2, 5 };
object[] o3 = { 3, "15/08/2017", "name 3", 1, 3, 4, 4, 0, 0, 4 };

dt1.Rows.Add(o1);
dt1.Rows.Add(o2);
dt1.Rows.Add(o3);            

var grid = new GridView();
grid.DataSource = dt1;
grid.DataBind();

Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=ExportFetsVinculats.xls");
Response.ContentType = "application/ms-excel";

Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

grid.RenderControl(htw);

Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return Content("");
到目前为止,一切都是正确的,但您必须以以下方式实现表


是否可以再添加两列作为标题?

这是简单解决方案的代码:

DataTable dt1 = new DataTable("Table1");

dt1.Columns.Add("Num", typeof(Int32));
dt1.Columns.Add("date", typeof(string));
dt1.Columns.Add("name", typeof(string));
dt1.Columns.Add("product1a", typeof(Int32));
dt1.Columns.Add("product1b", typeof(Int32));
dt1.Columns.Add("total1", typeof(Int32));
dt1.Columns.Add("product2a", typeof(Int32));
dt1.Columns.Add("product2b", typeof(Int32));
dt1.Columns.Add("product2c", typeof(Int32));
dt1.Columns.Add("total2", typeof(Int32));

object[] o1 = { 1, "10/10/2017", "name 1", 1, 2, 3, 1, 1, 1, 3 };
object[] o2 = { 2, "20/10/2017", "name 2", 2, 2, 4, 2, 1, 2, 5 };
object[] o3 = { 3, "15/08/2017", "name 3", 1, 3, 4, 4, 0, 0, 4 };
        
dt1.Rows.Add(o1);
dt1.Rows.Add(o2);
dt1.Rows.Add(o3);

GridView grid = new GridView();                    

grid.DataSource = dt1;
grid.DataBind();

GridView HeaderGrid = grid;
GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);

TableCell HeaderCell = new TableCell();
HeaderCell.Text = "";
HeaderCell.ColumnSpan = 3;
HeaderGridRow.Cells.Add(HeaderCell);

HeaderCell = new TableCell();
HeaderCell.Text = "Head Products 1";
HeaderCell.ColumnSpan = 3;
HeaderCell.Font.Bold = true;
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderGridRow.Cells.Add(HeaderCell);

HeaderCell = new TableCell();
HeaderCell.Text = "Head Products 2";
HeaderCell.ColumnSpan = 4;
HeaderCell.Font.Bold = true;
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderGridRow.Cells.Add(HeaderCell);
        
HeaderGrid.Controls[0].Controls.AddAt(0, HeaderGridRow);

Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=ExportFetsVinculats.xls");
Response.ContentType = "application/ms-excel";

Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

grid.RenderControl(htw);

Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return Content("");
或:

public ActionResult retrona_excel()
{
数据表dt1=新数据表(“表1”);
dt1.Columns.Add(“Num”,typeof(Int32));
dt1.Columns.Add(“日期”,typeof(字符串));
dt1.Columns.Add(“名称”,typeof(字符串));
dt1.Columns.Add(“product1a”,typeof(Int32));
dt1.Columns.Add(“product1b”,typeof(Int32));
dt1.Columns.Add(“total1”,typeof(Int32));
dt1.Columns.Add(“product2a”,typeof(Int32));
dt1.Columns.Add(“product2b”,typeof(Int32));
dt1.Columns.Add(“product2c”,typeof(Int32));
dt1.Columns.Add(“total2”,typeof(Int32));
对象[]o1={1,“10/10/2017”,“名称1”,1,2,3,1,1,1,3};
对象[]o2={2,“20/10/2017”,“名称2”,2,2,4,2,1,2,5};
对象[]o3={3,“2017年8月15日”,“名称3”,1,3,4,4,0,0,4};
dt1.Rows.Add(o1);
dt1.行。添加(o2);
dt1.行。添加(o3);
GridView网格=新的GridView();
grid.DataSource=dt1;
grid.DataBind();
列表头=新列表();
list_header.Add(新元组(“”,3,false,horizontallign.Center));
list_header.Add(新元组(“Head Products 1”,3,true,horizontallign.Center));
list_header.Add(新元组(“Head Products 2”,4,true,horizontallign.Center));
获取标题表格(网格、列表标题);
Response.ClearContent();
Response.Buffer=true;
AddHeader(“内容处置”、“附件;文件名=exportfetsvingulats.xls”);
Response.ContentType=“应用程序/ms excel”;
响应。Charset=“”;
StringWriter sw=新的StringWriter();
HtmlTextWriter htw=新的HtmlTextWriter(sw);
网格渲染控制(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
返回内容(“”);
}
私有void get_header_表(GridView网格、List_header)
{
GridView头网格=网格;
GridViewRow HeaderGridRow=新的GridViewRow(0,0,DataControlRowType.Header,DataControlRowState.Insert);
TableCell HeaderCell=新的TableCell();
foreach(列表头中的元组项)
{
HeaderCell=新的TableCell();
HeaderCell.Text=item.Item1;
HeaderCell.ColumnSpan=item.Item2;
HeaderCell.Font.Bold=item.Item3;
HeaderCell.HorizontalAlign=项目4;
HeaderGridRow.Cells.Add(HeaderCell);
}
HeaderGrid.Controls[0].Controls.AddAt(0,HeaderGridRow);
}

几年前,我曾在webforms应用程序中这样做,查看gridview的rowcreated事件,然后检查它是否是正在创建的标题,然后可以创建并附加一个表单元格,并将其columnspan属性更改为要合并的列数。对不起,我手头没有密码。。第二个被接受的答案应该对你有帮助。谢谢你的帮助,你给我的链接非常有用。我以前的陈述是错误的。我附上解决方案。
public ActionResult retorna_excel()
{
     DataTable dt1 = new DataTable("Table1");

     dt1.Columns.Add("Num", typeof(Int32));
     dt1.Columns.Add("date", typeof(string));
     dt1.Columns.Add("name", typeof(string));
     dt1.Columns.Add("product1a", typeof(Int32));
     dt1.Columns.Add("product1b", typeof(Int32));
     dt1.Columns.Add("total1", typeof(Int32));
     dt1.Columns.Add("product2a", typeof(Int32));
     dt1.Columns.Add("product2b", typeof(Int32));
     dt1.Columns.Add("product2c", typeof(Int32));
     dt1.Columns.Add("total2", typeof(Int32));

     object[] o1 = { 1, "10/10/2017", "name 1", 1, 2, 3, 1, 1, 1, 3 };
     object[] o2 = { 2, "20/10/2017", "name 2", 2, 2, 4, 2, 1, 2, 5 };
     object[] o3 = { 3, "15/08/2017", "name 3", 1, 3, 4, 4, 0, 0, 4 };
        
     dt1.Rows.Add(o1);
     dt1.Rows.Add(o2);
     dt1.Rows.Add(o3);

     GridView grid = new GridView();                    

     grid.DataSource = dt1;
     grid.DataBind();

     List<Tuple<string, int, bool, HorizontalAlign>> list_header = new List<Tuple<string, int, bool, HorizontalAlign>>();
     list_header.Add(new Tuple<string, int, bool, HorizontalAlign>("", 3, false, HorizontalAlign.Center));
     list_header.Add(new Tuple<string, int, bool, HorizontalAlign>("Head Products 1", 3, true, HorizontalAlign.Center));
     list_header.Add(new Tuple<string, int, bool, HorizontalAlign>("Head Products 2", 4, true, HorizontalAlign.Center));            

     get_header_table(grid, list_header);

     Response.ClearContent();
     Response.Buffer = true;
     Response.AddHeader("content-disposition", "attachment; filename=ExportFetsVinculats.xls");
     Response.ContentType = "application/ms-excel";

     Response.Charset = "";
     StringWriter sw = new StringWriter();
     HtmlTextWriter htw = new HtmlTextWriter(sw);

     grid.RenderControl(htw);

     Response.Output.Write(sw.ToString());
     Response.Flush();
     Response.End();
     return Content("");
}

private void get_header_table(GridView grid, List<Tuple<string, int, bool, HorizontalAlign>> list_header)
{
    GridView HeaderGrid = grid;
    GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);

    TableCell HeaderCell = new TableCell();

    foreach (Tuple<string, int, bool, HorizontalAlign> item in list_header)
    {
        HeaderCell = new TableCell();
        HeaderCell.Text = item.Item1;
        HeaderCell.ColumnSpan = item.Item2;
        HeaderCell.Font.Bold = item.Item3;
        HeaderCell.HorizontalAlign = item.Item4;
        HeaderGridRow.Cells.Add(HeaderCell);
    }

    HeaderGrid.Controls[0].Controls.AddAt(0, HeaderGridRow);
}