Asp.net 向excel文件添加标题

Asp.net 向excel文件添加标题,asp.net,Asp.net,我正在使用datatable作为函数的输入创建一个excel文件。 虽然在excel文件中创建,但它不会添加标题。有人能给代码吗 下面是excel文件的代码和行/列结构 private void ExporttoExcel(DataTable table) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Resp

我正在使用datatable作为函数的输入创建一个excel文件。 虽然在excel文件中创建,但它不会添加标题。有人能给代码吗 下面是excel文件的代码和行/列结构

private void ExporttoExcel(DataTable table)
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.Buffer = true;
    HttpContext.Current.Response.ContentType = "application/ms-excel";
    //HttpContext.Current.Response.ContentType = "application/ms-word";
    HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");
    // HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.doc");
    // HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xlsx");
    HttpContext.Current.Response.Write("head");
    HttpContext.Current.Response.Write("tail");
    // excell_app.createHeaders(5, 2, "Total of Products", "B5", "D5", 2, "YELLOW", true, 10, "n");

    HttpContext.Current.Response.Charset = "utf-8";
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
    HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
    HttpContext.Current.Response.Write("<BR><BR><BR>");
    HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
    int columnscount = GridView1.Columns.Count;

    for (int j = 0; j < columnscount; j++)
    {


        HttpContext.Current.Response.Write("<Td>");
        HttpContext.Current.Response.Write("<B>");
        //HttpContext.Current.Response.Write(GridView1.Columns[j].HeaderText.ToString());
    }

    HttpContext.Current.Response.Write("</TR>");
    foreach (DataRow row in table.Rows)
    {
        HttpContext.Current.Response.Write("<TR>");
        for (int i = 0; i < table.Columns.Count; i++)
        {
            HttpContext.Current.Response.Write("<Td>");
            HttpContext.Current.Response.Write(row[i].ToString());
            HttpContext.Current.Response.Write("</Td>");
        }

        HttpContext.Current.Response.Write("</TR>");
    }
    HttpContext.Current.Response.Write("</Table>");
    HttpContext.Current.Response.Write("</font>");
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();
}
private void ExporttoExcel(数据表)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer=true;
HttpContext.Current.Response.ContentType=“应用程序/ms excel”;
//HttpContext.Current.Response.ContentType=“应用程序/ms word”;
HttpContext.Current.Response.Write(@“”);
HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=Reports.xls”);
//HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=Reports.doc”);
//HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=Reports.xlsx”);
HttpContext.Current.Response.Write(“head”);
HttpContext.Current.Response.Write(“tail”);
//excell_应用程序createHeaders(5,2,“产品总数”,“B5”,“D5”,2,“黄色”,真实,10,“n”);
HttpContext.Current.Response.Charset=“utf-8”;
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding(“windows-1250”);
HttpContext.Current.Response.Write(“”);
HttpContext.Current.Response.Write(“


”); HttpContext.Current.Response.Write(“”); int columnsunt=GridView1.Columns.Count; 对于(int j=0;j
城市联系人EMAILID姓名ID号,如

private void ExporttoExcel(数据表)
 private void ExporttoExcel(DataTable table)
 {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        //HttpContext.Current.Response.ContentType = "application/ms-word";
        HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
       HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");
       // HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.doc");
        HttpContext.Current.Response.Charset = "utf-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
        HttpContext.Current.Response.Write("<BR><BR><BR>");
        HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
        int columnscount = GridView_Result.Columns.Count;

        for (int j = 0; j < columnscount; j++)
        {
            HttpContext.Current.Response.Write("<Td>");
            HttpContext.Current.Response.Write("<B>");
            HttpContext.Current.Response.Write(GridView_Result.Columns[j].HeaderText.ToString());
            HttpContext.Current.Response.Write("</B>");
            HttpContext.Current.Response.Write("</Td>");
        }
        HttpContext.Current.Response.Write("</TR>");
        foreach (DataRow row in table.Rows)
        {
            HttpContext.Current.Response.Write("<TR>");
            for (int i = 0; i < table.Columns.Count; i++)
            {
                HttpContext.Current.Response.Write("<Td>");
                HttpContext.Current.Response.Write(row[i].ToString());
                HttpContext.Current.Response.Write("</Td>");
            }

            HttpContext.Current.Response.Write("</TR>");
        }
        HttpContext.Current.Response.Write("</Table>");
        HttpContext.Current.Response.Write("</font>");
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }



    protected void Btn_Export_Click(object sender, EventArgs e)
    {
        ExporttoExcel(dt);

    }
}
{ HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Buffer=true; HttpContext.Current.Response.ContentType=“应用程序/ms excel”; //HttpContext.Current.Response.ContentType=“应用程序/ms word”; HttpContext.Current.Response.Write(@“”); HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=Reports.xls”); //HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=Reports.doc”); HttpContext.Current.Response.Charset=“utf-8”; HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding(“windows-1250”); HttpContext.Current.Response.Write(“”); HttpContext.Current.Response.Write(“


”); HttpContext.Current.Response.Write(“”); int columnsunt=GridView\u Result.Columns.Count; 对于(int j=0;j
打印标题的行已被注释掉。很抱歉,您无法指出哪一行代码,因为如果我在两个代码下面注释,则会出现错误//HttpContext.Current.Response.AddHeader(“内容处置”,“附件;文件名=Reports.doc”)//HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=Reports.xlsx”)<代码>//HttpContext.Current.Response.Write(GridView1.Columns[j].HeaderText.ToString())否仍不起作用选择Story.City、Story.Contact、Story.UserEmailid、Story.UserName、Story.StoryId、COUNT(Likes.StoryId)作为NumberOfOrders从Likes左到Likes.StoryId=Story.StoryId和liked在'12/10/2013'和'12/11/2013'之间按Story.StoryId分组,Story.UserName,Story.UserEmailid,Story.Contact,Story.City order by NumberOfOrders DESC我正在使用下面的问题来填充数据表