Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 转换数据表不可逆?_C#_Asp.net_Datatable_Oledb_Xls - Fatal编程技术网

C# 转换数据表不可逆?

C# 转换数据表不可逆?,c#,asp.net,datatable,oledb,xls,C#,Asp.net,Datatable,Oledb,Xls,我正在尝试将数据表转换为XLS文件。 代码如下 片段1 此代码产生错误。它说: 不支持外部文件格式 我注意到,当您使用错误的OLEDB提供程序时,此错误与此类似。但在我的例子中,应该没有问题,因为我使用Microsoft.Jet.OLEDB.4.0 for.xls(2003)格式。但不知怎么的,事情还是发生了 这段代码在某种程度上适用于我测试的任何其他.xls文件它不仅适用于使用#Snippet1转换生成的文件。但是,假设我使用#Snippet1生成了一个文件,然后将所有单元格复制到一个新的.x

我正在尝试将数据表转换为XLS文件。 代码如下

片段1 此代码产生错误。它说:

不支持外部文件格式

我注意到,当您使用错误的OLEDB提供程序时,此错误与此类似。但在我的例子中,应该没有问题,因为我使用Microsoft.Jet.OLEDB.4.0 for.xls(2003)格式。但不知怎么的,事情还是发生了

这段代码在某种程度上适用于我测试的任何其他.xls文件它不仅适用于使用#Snippet1转换生成的文件。但是,假设我使用#Snippet1生成了一个文件,然后将所有单元格复制到一个新的.xls文件中。#Snippet2将用于此新文件。简而言之,只有直接从#Snippet1生成的文件不起作用


我想我做错了什么。因此,我希望有人能在这里帮助我。

您的Excel输出代码片段操作系统没有生成Excel文件。它生成一个html表格,并设置内容类型,以告诉计算机使用Excel打开它

您需要使用一个库来生成一个真正的excel文件,或者使用excel数据互操作(两者都有很多文章)来直接使用它


或者,您可以生成一个.csv文件,但仍然保留Excel内容类型,这样Excel会打开它,但会丢失格式。然后您可以检查扩展名,将其作为excel或纯文本阅读。

您能给我一些建议吗?如果你这样做,我会很感激的。我自己设法解决了这个问题。你的解释是对的。创建.xls文件是一种糟糕的方法。
Response.Clear();
        Response.Buffer = true;

        Response.AddHeader("content-disposition","attachment;filename=New.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        for (int i = 1; i < gvGridView.HeaderRow.Cells.Count; i++)
        {
            gvGridView.HeaderRow.Cells[i].Text = Table.Columns[i - 1].ColumnName;
        }

        for (int i = 0; i < gvGridView.Rows.Count; i++)
        {
            //Apply text style to each Row
            gvGridView.Rows[i].Attributes.Add("class", "textmode");
        }
        gvGridView.RenderControl(hw);

        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
int idx = filen.IndexOf(".");
        string tf = filen.Remove(idx, 4);

        OleDbConnection MyConnection = null;    
        DataSet DtSet = null;    
        OleDbDataAdapter MyCommand = null; 
        //Connection for MS Excel 2003 .xls format
        MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + path + "';Extended Properties=Excel 8.0;");     
        MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [table$]", MyConnection);      
        DtSet = new System.Data.DataSet();       
        MyCommand.Fill(DtSet);        
        dt = DtSet.Tables[0];        
        MyConnection.Close();            
        if (dt.Rows.Count > 0)        
        {            
            theGridView.DataSource = dt;           
            theGridView.DataBind();       
        }        
        if(System.IO.File.Exists(path))       
        {            
            System.IO.File.Delete(path);      
        }