简体   繁体   English

如何设置Visual Studio以便能够操作Excel文件?

[英]What do I need to do to set up Visual Studio to be able to manipulate an Excel File?

Let's say I want to find the value of a cell in an Excel file. 假设我想在Excel文件中找到单元格的值。 In VBA, I would do this: 在VBA中,我会这样做:

Dim varValue As Variant
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
varValue = ws.Range("A1").Value

How do I set up a Visual Studio project so I can access and modify an Excel file in C# rather than in VBA? 如何设置Visual Studio项目以便我可以在C#中而不是在VBA中访问和修改Excel文件?

What references might I need to add? 我可能需要添加哪些参考?

Does the file need to be open in order to work with it? 该文件是否需要打开才能使用它?

You can use Excel automation or a third-party library. 您可以使用Excel自动化或第三方库。

To use Excel automation, you should install the Office PIAs and add a reference to Microsoft.Office.Interop.Excel.dll . 若要使用Excel自动化,您应安装Office PIA并添加对Microsoft.Office.Interop.Excel.dll的引用。 You would then write code to open the file (using the ApplicationClass class) and manipulate it. 然后,您将编写代码来打开文件(使用ApplicationClass类)并对其进行操作。

This approach would use the same object model that you're used to in VBA. 此方法将使用您在VBA中使用的相同对象模型。 However, since C# 3 does not support optional parameters or weak typing, it will be somewhat annoying. 但是,由于C#3不支持可选参数或弱类型,因此会有些烦人。 It would be easier to do it in VB .Net or C# 4 (currently in beta) 在VB .Net或C#4(目前处于测试阶段)这样做会更容易


Excel automation is not suitable for code running in non-interactive context. Excel自动化不适用于在非交互式上下文中运行的代码。 There are a number of third-party libraries written entirely in .Net that can read and write Excel files. 有许多完全用.Net编写的第三方库可以读写Excel文件。

In addition, if you only need to manipulate table-like data, you can use OleDb to run SQL statements against Excel files using this connection string . 此外,如果您只需要操作类似表的数据,则可以使用OleDb使用此连接字符串对Excel文件运行SQL语句。

You can use OleDbCommand s to run SELECT * FROM [SheetName] , assuming that the sheet is a table. 您可以使用OleDbCommand来运行SELECT * FROM [SheetName] ,假设工作表是一个表。 You can also select from a named range. 您也可以从命名范围中进行选择。 You can get the available tables by calling oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null) and looking at the TABLE_NAME column ion the returned DataTable. 您可以通过调用oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null)并查看返回的DataTable中的TABLE_NAME列来获取可用表。

I wrote my own, fairly simple class to extract data from an Excel spreadsheet via C#. 我编写了自己的,相当简单的类,通过C#从Excel电子表格中提取数据。 You need to include: 你需要包括:

using Microsoft.Office.Interop;

Then you can do something like this to get started: 然后你可以做这样的事情来开始:

Excel.Application excel;
Excel.Workbook workbook;
Excel.Worksheet worksheet;
Excel.Sheets sheets;
Excel.Range range;

excel = new Microsoft.Office.Interop.Excel.Application();
workbook = excel.Workbooks.Open(workbookName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
sheets = workbook.Worksheets;
...

You do not have to use interop. 不必使用互操作。

You simply need a reference to... 你只需要参考......

System.Data.OleDb

Define a connection string as you would any other data source: 您可以像定义任何其他数据源一样定义连接字符串:

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourPath;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";";

And add some code to query your database: 并添加一些代码来查询您的数据库:

OleDbConnection objConnection = new OleDbConnection();
string strSQL = "SELECT * FROM [YourTable]";
objConnection = new OleDbConnection(connectionString);
objConnection.Open();
OleDbCommand cmd = new OleDbCommand(strSQL, objConnection);
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);

...Then enumerate through your data rows. ...然后枚举您的数据行。

You have to used Microsoft.Office.Interop.Excel. 您必须使用Microsoft.Office.Interop.Excel。 You'll need to use COM Interop. 您需要使用COM Interop。 Here's a detailed tutorial about how it's done. 这是一个关于它是如何完成的详细教程。

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

相关问题 在visual studio 2013 update 2中我需要使用SIMD? - What do i need to use SIMD with visual studio 2013 update 2? 如何从 Visual Studio 中删除 Excel 文件? - How do I delete Excel file from Visual Studio? 要打开一个excel文件,我需要任何特殊参考吗? - to open up an excel file, do I need any special references? 我需要从Visual Studio中获得什么才能在没有Visual Studio的情况下运行应用程序 - What do I need from visual studio to run application without visual studio Visual Studio Web,我要上传什么文件到服务器? - Visual studio web, what file do i upload to server? 我需要做什么才能使我的 C# 代码在 Visual Studio for Mac 中运行 - What do I need to do to make my C# code run in Visual Studio for Mac 在Visual Studio 2013上发布用c#asp.net创建的网站,我需要做什么? - What do I need to do to publish a website made in c# asp.net on Visual Studio 2013? 我需要为WiX提供哪些Visual Studio文件,如何获取它们? - What Visual Studio Files do I need to give to WiX, and how can I get them? 如何设置环境来解决 Visual Studio 中的 LeetCode 问题? C# - How do I set up an environment to solve LeetCode problems in Visual Studio? C# 您如何编写Visual Studio插件来操纵自己的文件类型? - How do you write a Visual Studio plugin to manipulate your own file type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM