-2

我正在开发一个硒应用程序。Selenium 访问我的共享点站点,并从那里下载一张 Excel 表格。现在,忘记硒。

我希望 c# 处理打开的 excel 表。从某种意义上说,我想做一些过滤列并将所有值(过滤后)存储到临时变量中?这可能吗?

我怎样才能做到这一点?

任何评论都会非常有帮助。

4

4 回答 4

1

There have already been a few solutions out there how to handle Word/Excel/Powerpoint documents with C#. I'm actually just going to repeat what they say, but I'll provide some more code. I had to develop a software that allows automatization and the first question you need to ask yourself, what is the time constraint. That's why I'm going for, because it is pretty fast in implementation. You can find something about it here:

http://msdn.microsoft.com/fr-fr/library/microsoft.office.interop.excel.application%28v=office.11%29.aspx

Due to some limitation with objects, you might take a look at NetOffice, which actually does the same functionalities than Microsoft Office offers, but it helps you to handle objects easier.

http://netoffice.codeplex.com/

To be honest, if you post a question on Microsoft or NetOffice forum, any programming that involves Interop with Microsoft, will be a paid service and you won't have that fast a reply. In that case, you'll have to look up by yourself. Now for the OpenXML: http://openxmldeveloper.org/ I won't be able to tell you much about it, because it requires more time and more knowledge to use it, but you'll find a lot of videos how to work with it and how to handle objects/replacements of objects. Also a pro for OpenXML it became a standard and recommanded by the Admininistrator of Microsoft forums. Con of Microsoft Office Interop -> Server management and automatization won't work, depending on the server configuration. Also you'll probably face a lot of issues with COM setups, rights, which will lead to a lot of hours/days of debugging. I wish, I could tell you more about the OpenXML, but didn't have the time to work more in dept with it.

Now about your issue in C#, if you plan to do it with Microsoft.Office.Interop, you might first of all download/integrate the library into your Visual Studio. Now, following code will help you to open a file, go through worksheets and go through rows/columns and to save it.

Code:

using Excel = NetOffice.ExcelApi;
Excel.Application xlsApp = new Excel.Application();

or:

Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook xls = null;
excel = new Microsoft.Office.Interop.Excel.Application();
            object missing = Type.Missing;
            object trueObject = true;
            excel.Visible = false;
            excel.DisplayAlerts = false;
            xls = excel.Workbooks.Open(excelFile, missing, trueObject, missing,

                    missing, missing, missing, missing, missing, missing, missing, missing,

                    missing, missing, missing);

You can choose between both. But I would go for the Microsoft one:

try
{
                // Must be surrounded by try catch to work.
                // http://naimishpandya.wordpress.com/2010/12/31/hide-power-point-application-window-in-net-office-automation/
                xlsApp.Visible = true;
                xlsApp.DisplayAlerts = false;
}
catch (Exception e)
{
                Console.WriteLine("-------Error hiding the application-------");
                Console.WriteLine("Occured error might be: " + e.StackTrace);
}
Excel.Workbook workbook;
workbook = xlsApp.Workbooks.Open(configuration.XLSExportedFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                            Type.Missing, Type.Missing);

Now, you want to move between worksheets or maybe shapes in that worksheet:

foreach (Excel.Worksheet sheet in workbook.Sheets)
{
     workbook.Activate();
     sheet.Activate();
     foreach (Excel.Shape shape in sheet.Shapes)
     {
     }
}

Now if you want to paste some values into a specific range, here you go:

sheet.Range(cellmapp).PasteSpecial();

Where cellmapp, you a range from A1:B2 for example. For autofiting values to their cell size, you can do following code:

sheet.Columns.AutoFit();
sheet.Rows.AutoFit();

Now, once you finished all your modification process, very important with COM, always close them.

workbook.SaveAs(File);
workbook.Close();
xlsApp.Quit(); 
xlsApp.Dispose();

In case you are afraid some Excel processes are stuck, go for

private void ManageXLSProcesses()
{
        Process[] processes = System.Diagnostics.Process.GetProcessesByName("Excel");
        foreach (Process p in processes)
            p.Kill();
}

But you should avoid doing it in the harsh way. It's up to you, to see which way to go if you work with COM or OpenXML, but the future will be OpenXML, because Microsoft Office Interop are easy to write, but hard to handle during runtime. This is supposed to give you an overview of the coming code that youll write. Make your own choices of implementation. Good advice, spend more time into OpenXML since it's a standard, because COM management is going to be harsh in the future with newest version of Microsoft Office 2013 ++.

于 2013-07-17T12:56:12.233 回答
1

处理 Office 文档的官方方法通常是使用 Microsoft 自己的 Open XML SDK 来处理 Office XML 文件(docx、xlsx 等)。您无需在计算机上安装 Excel 即可使用 SDK。您可以在“使用 Open XML 和 Excel ”中找到使用 Excel 文件的文档和示例

使用互操作是一个可怕的想法,因为您必须为每个文档创建一个新的 Excel 实例,并且您必须确保它在完成时关闭。对于多个线程可能同时尝试处理 Excel 文件的服务器应用程序(例如网站)来说,这是一种难以管理的情况。Excel Interop 是一种快速关闭服务器的方法。

于 2013-07-17T11:50:07.383 回答
1

对的,这是可能的。容易找到但很难做到的解决方案是 Office Interop:例如打开 excel 并强制它执行您的程序。这可行,但是很麻烦,速度慢且难以调试。我会推荐 EPPLUS - 这可以轻松打开和修改 xlsx 文件,并且与 excle interop 相比速度快得不得了。它是免费的http://epplus.codeplex.com/

于 2013-07-17T11:31:50.817 回答
0

可以使用 Interop,查看此链接 http://www.dotnetperls.com/excel。您可以通过 excel 进行过滤,也可以将所有列加载到自己的对象中,然后使用 C# 进行过滤,无论您喜欢什么。

于 2013-07-17T11:32:54.227 回答