简体   繁体   English

我无法打开.xlsx文件

[英]I cannot open .xlsx file

I want to open an xlsx file, I have tried the below code,but neither does it open nor does it thrown any error. 我想打开一个xlsx文件,我已经尝试了下面的代码,但它既没有打开也没有抛出任何错误。

Can anyone throw any light upon it 任何人都可以对它施加任何影响

string path = "C:\\examples\\file1.xlsx";
string connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
OleDbConnection cn = new OleDbConnection(connString);
cn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", cn);
DataTable dt = new DataTable();
adapter.Fill(dt);

In December 2010 Microsoft (finally!) published aa 64-bit OLEDB driver for CSV and XLSX files. 2010年12月,微软(最终!)发布了一个用于CSV和XLSX文件的64位OLEDB驱动程序。

You'll need the 64-bit Microsoft Access Database Engine 2010 Redistributable . 您将需要64位Microsoft Access数据库引擎2010可再发行组件 Make sure to download the 64-bit version ( AccessDatabaseEngine_X64.exe ). 确保下载64位版本( AccessDatabaseEngine_X64.exe )。 You'll need to uninstall any 32-bit Office apps (including Sharepoint Designer!) in order to install it. 您需要卸载任何32位Office应用程序(包括Sharepoint Designer!)才能安装它。

If you want CSV, you'll want the Microsoft Access Text Driver (*.txt, *.csv) driver name, but I haven't been able to find a full connection string yet using this driver from OLEDB (although if you have, leave a commend and I'll amend this answer). 如果你想要CSV,你需要Microsoft Access Text Driver (*.txt, *.csv)驱动程序名称,但我还没有找到一个完整的连接字符串,但是使用OLEDB中的这个驱动程序(尽管你有,留下一个赞扬,我会修改这个答案)。 Note that 64-bit names are different from the 32-bit versions. 请注意,64位名称与32位版本不同。

For reading XLSX files, use a connection string like this: 要读取XLSX文件,请使用如下连接字符串:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
 + FilePath
 + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";

For XLS (pre-2007 Excel) files use a connection string like this: 对于XLS(2007之前的Excel)文件,请使用如下连接字符串:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
 + FilePath
 + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

Many thanks to this blog post and this answer for pointing me in the right direction when I ran into the same problem, and for providing content I adapted to write this answer. 非常感谢这篇博文这个答案 ,当我遇到同样的问题时,我指出了正确的方向,并提供了我适合写这个答案的内容。

See what this does in your connection string: 看看它在你的连接字符串中的作用:

Extended Properties=\\"Excel 8.0;HDR=YES;\\" the rest of your connection string is correct I believe. 扩展属性= \\“Excel 8.0; HDR = YES; \\”你的连接字符串的其余部分是正确的我相信。

Are you running it on 64 bit Windows? 你是在64位Windows上运行它吗? Last time I checked the OLE drivers for Excel workbooks did not work with 64 bit Windows. 上次我检查Excel工作簿的OLE驱动程序不适用于64位Windows。

SpreadsheetGear for .NET will let you read Excel workbooks from .NET and works with .NET 2.0+ - including 64 bit Windows. SpreadsheetGear for .NET将允许您从.NET读取Excel工作簿并使用.NET 2.0+ - 包括64位Windows。

You can see live samples here and download the free trial here . 你可以看到现场的样品这里并下载免费试用这里

Disclaimer: I own SpreadsheetGear LLC 免责声明:我拥有SpreadsheetGear LLC

除了Phillip的回答,请确保将TargetPlatform设置为x86。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用NPOI保存xlsx文件后,当我打开xlsx文件时,它说文件已损坏 - After saving xlsx file using NPOI, when I open xlsx file, it says file corrupt 如何以编程方式打开 TSV 文件并将其保存为 XLSX 文件? - How do I programmatically open a TSV file and save it as an XLSX file? 使用 NPOI 损坏 .xlsx 文件的问题 - Excel 无法打开文件“file.xlsx”,因为文件格式或文件扩展名无效 - Problematic corruption of .xlsx files with NPOI - Excel cannot open the file 'file.xlsx" because the file format or file extension is not valid 如何在C#.NET中打开xlsx文件? - How will I open xlsx File in C#.NET? xlsx excel无法打开文件,因为文件格式或文件扩展名无效-C# - xlsx excel cannot open the file because the file format or file extension is not valid - c# 无法打开导出的xlsx文件 - Unable to open exported xlsx file 打开Excel xlsx文件进行查看 - Open Excel xlsx file for viewing 从模板.xlt文件打开为.xltx或xlsx - Open from template .xlt file as .xltx or xlsx 当我打开使用openXML创建的xlsx文件时,如何解决错误:“修复的记录:/xl/workbook.xml中的图纸属性? - How to fix error when I open my xlsx file creating with openXML : "repaired records : sheet properties in /xl/workbook.xml? 通过网络浏览器打开.xls / .xlsx文件,并使其对用户可编辑 - Open .xls/.xlsx file through web browser and make it editable for the user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM