C#读取多个Excel文件 [英] C# read multiple Excel files

查看:169
本文介绍了C#读取多个Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能作出这样的一个文件夹中读取多个Excel文件,并从中提取某些信息的应用程序?

Is it possible to make an application that reads multiple excel files from a folder and extracts some information from them?

推荐答案

是的,这里是如何使用互操作。你需要做的第一件事是Excel的互操作库添加到您的项目。您可以通过创建一个新的Visual Studio解决方案为此,右键单击引用下,选择添加参考,然后选择的Microsoft.Office.Interop.Excel 从.NET标签

Yes it is, and here is how using Interop. The first thing you need to do is add the Excel Interop library to your project. You can do this by creating a new Visual Studio solution, right clicking on References, selecting Add Reference and then selecting Microsoft.Office.Interop.Excel from the .NET tab.

然后你需要添加一个using语句为Excel,一个用于InteropServices(因为我们是一个COM对象interoping):

Then you need to add a using statement for Excel, and one for InteropServices (as we are interoping with a COM object):

using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;



随后,一个方法中,你需要创建一个应用对象:

Excel.Application application = new Excel.Application();



接下来,创建一个工作簿您想从阅读每个工作簿对象,像这样:

Next, create a Workbook object for each workbook you want to read from, like so:

Excel.Workbook workbookOne;
Excel.Workbook workbookTwo;
Excel.Workbook workbookThree;

现在使用Application对象打开每个工作簿,并加载每一个到其各自的工作簿对象:

Now use the Application object to open each workbook, and load each one into its respective Workbook object:

workbookOne = application.Workbooks.Open(@"C:\workbookOneLocation.xlsx");
workbookTwo = application.Workbooks.Open(@"C:\workbookTwoLocation.xlsx");
workbookThree = application.Workbooks.Open(@"C:\workbookThreeLocation.xlsx");

现在,你需要决定你要提取的信息。一旦你做到了这一点确定它是在其工作中的工作簿,然后通过简单地看标签和计数弄清楚的数量。在下面的例子中, Sheet2中是数字1,工作表Sheet1 是2号和表Sheet 3 是数字3:

Now you need to decide what information you want to extract. Once you have done this determine which worksheet in the workbook it is on,and then figure out the number by simply looking at the tabs and counting. In the below example, Sheet2 is number 1, Sheet1 is number 2 and Sheet3 is number 3:

创建每件的你需要像这样的信息变量(任何值类型变量将需要为可以为空):

Create a variable for each piece of information you need like so (any value type variables will need to be nullable):

string firstPieceOfInformationINeed;
string[] secondPieceOfInformationINeed;
double? thirdPieceOfInformationINeed;

让我们说的首部资料片,我们需要的是在单元格A1串上表中的一个内部工作簿之一,第二部分是单元格B2 - B4工作表上的两个内线的工作簿两个,第三件是里面的工作簿三份工作表三种细胞C5数字。我们这样做:

Let's say that the first piece of information we need is a string in Cell A1 on sheet one inside workbook one, the second piece is Cells B2 - B4 on worksheet two inside workbook two, and the third piece is a number on cell C5 on worksheet three inside workbook three. We would do this:

string firstPieceOfInformationINeed;
string[] secondPieceOfInformationINeed;
double? thirdPieceOfInformationINeed;

Excel.Worksheet worksheet;
Excel.Range range;

worksheet = workbookOne.Sheets[1];
range = worksheet.Cells["1", "1"];

firstPieceOfInformationINeed = range.Value as string;

worksheet = workbookTwo.Sheets[2];
range = worksheet.Range["B2", "B4"];

secondPieceOfInformationINeed = range.Value as string[];

worksheet = workbookThree.Sheets[3];
range = worksheet.Cells["3", "5"];

thirdPieceOfInformationINeed = range.Value as double?;



然后,我们关闭工作簿,使用一个布尔值,表明我们是否要保存更改:

Then we close the workbook, using a boolean value to indicate whether or not we want to save changes:

workbookOne.Close(true);
workbookTwo.Close(false);
workbookThree.Close(true);

现在退出应用程序:

application.Quit();

和释放COM对象:

Marshal.ReleaseComObject(application);

现在你用Excel做的,都在不同的信息,你需要存储为C#变量,你可以用这些你希望的东西做的。

Now you are done with Excel, and have all of the different pieces of information you need stored as C# variables, and you can do with these what you wish.

这篇关于C#读取多个Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆