数据到Excel

时间:2016-09-22 05:52:40

标签: vb.net excel datatable

Iam将数据从datatable导出到excel,我引用了以下链接并实现了相同的

Export DataTable to Excel File

我面临的问题是当打开Excel时,它显示您尝试打开的文件格式不同。我该怎么做才能解决此错误

我使用的代码是

 dt = city.GetAllCity();//your datatable
    string attachment = "attachment; filename=city.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/vnd.ms-excel";
    string tab = "";
    foreach (DataColumn dc in dt.Columns)
    {
        Response.Write(tab + dc.ColumnName);
        tab = "\t";
    }
    Response.Write("\n");
    int i;
    foreach (DataRow dr in dt.Rows)
    {
        tab = "";
        for (i = 0; i < dt.Columns.Count; i++)
        {
            Response.Write(tab + dr[i].ToString());
            tab = "\t";
        }
        Response.Write("\n");
    }
    Response.End();

0 个答案:

没有答案