在c#中不能打开excel文件来进行不同格式的扩展错误?

时间:2022-10-24 10:07:16

can not open excel file in c# because The file you are trying to open in a different format than specified file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?

不能在c#中打开excel文件,因为您试图以不同的格式打开文件,而不是指定的文件扩展名。验证文件未被损坏,并且在打开文件之前来自可信源。您现在要打开文件吗?

  private void button1_Click(object sender, EventArgs e)
        {
            string fileName = Directory.GetCurrentDirectory();
            fileName += "\\" + textBox1.Text + ".xls";

            var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\"";
            var conn = new OleDbConnection(connectionString);
            conn.Open();

            var sheets = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            var cmd = conn.CreateCommand();

            ///////////////////////     sheet 1
            cmd.CommandText = "SELECT * FROM [" + sheets.Rows[0]["TABLE_NAME"].ToString() + "] ";
            var adapter = new OleDbDataAdapter(cmd);
            var ds = new DataSet();
            adapter.Fill(ds);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
        }

1 个解决方案

#1


0  

This is because the file is actually basically just a CSV file with an XLS extension to make it open in Excel.

这是因为该文件实际上只是一个CSV文件,带有XLS扩展名,以便在Excel中打开它。

it happens in new versions of Excel. Older ones will happily export them.

它发生在Excel的新版本中。年纪大一点的人会很乐意地把它们出口。

See this Microsoft doc for more information:

更多信息请见此微软文档:

http://support.microsoft.com/kb/948615

http://support.microsoft.com/kb/948615

One option is simply to rename the file name to .csv, and keep the user interface as saying that it is an Excel file (Excel is quite happy to read csv files). Given that Windows tends to hide the file extension, this seems like a fairly attractive option.

一种选择是将文件名重命名为.csv,并保留用户界面,称它是一个Excel文件(Excel非常乐于读取csv文件)。考虑到Windows往往隐藏文件扩展名,这似乎是一个相当有吸引力的选择。

EDIT

编辑

"External table is not in the expected format." typically occurs when trying to use an Excel 2007 file with a connection string that uses: Microsoft.Jet.OLEDB.4.0 and Extended Properties=Excel 8.0

“外部表不符合预期格式”,通常发生在尝试使用Excel 2007文件时,使用的连接字符串使用:Microsoft.Jet.OLEDB.4.0和Extended Properties= excel8.0。

Go through:

经历:

Excel "External table is not in the expected format."

Excel“外部表不符合预期格式”。

Hope its helpful.

希望它对您有所帮助。

#1


0  

This is because the file is actually basically just a CSV file with an XLS extension to make it open in Excel.

这是因为该文件实际上只是一个CSV文件,带有XLS扩展名,以便在Excel中打开它。

it happens in new versions of Excel. Older ones will happily export them.

它发生在Excel的新版本中。年纪大一点的人会很乐意地把它们出口。

See this Microsoft doc for more information:

更多信息请见此微软文档:

http://support.microsoft.com/kb/948615

http://support.microsoft.com/kb/948615

One option is simply to rename the file name to .csv, and keep the user interface as saying that it is an Excel file (Excel is quite happy to read csv files). Given that Windows tends to hide the file extension, this seems like a fairly attractive option.

一种选择是将文件名重命名为.csv,并保留用户界面,称它是一个Excel文件(Excel非常乐于读取csv文件)。考虑到Windows往往隐藏文件扩展名,这似乎是一个相当有吸引力的选择。

EDIT

编辑

"External table is not in the expected format." typically occurs when trying to use an Excel 2007 file with a connection string that uses: Microsoft.Jet.OLEDB.4.0 and Extended Properties=Excel 8.0

“外部表不符合预期格式”,通常发生在尝试使用Excel 2007文件时,使用的连接字符串使用:Microsoft.Jet.OLEDB.4.0和Extended Properties= excel8.0。

Go through:

经历:

Excel "External table is not in the expected format."

Excel“外部表不符合预期格式”。

Hope its helpful.

希望它对您有所帮助。