简体   繁体   English

如何从 C# 加速创建 excel Excel 电子表格?

[英]How can I speed-up creating excel Excel spreadsheets from C#?

I am currently writing around 200 Excel spreadsheets using Excel 2007 with C# COM Interop.我目前正在编写大约 200 个 Excel 电子表格,使用 Excel 2007 和 C# ZD47C174ED277BDF70CC Interactive

Unfortunately I need about 4 minutes to write these 200 sheets - each sheet has around 1000 rows with 8- 10 columns.不幸的是,我需要大约 4 分钟来写这 200 张纸——每张纸大约有 1000 行,8-10 列。

How can I speed up things?我怎样才能加快速度?

My code looks like this basically:我的代码基本上是这样的:

var xlApp = new Excel.Application();
xlApp.DisplayAlerts=false;
foreach (x in z) {
   var wb = xlApp.Workbooks.add(XLWBATemplae.xlWBATWorksheet);
   var ws = (Worksheet)wb.Worksheets[1];
   //fill data into sheet
   wb.SaveAs(fileName);
   wbClose();
}

clarification:I am filling every cell individually right now澄清:我现在正在单独填充每个单元格

I will try out some of the suggestions from you and will then accept the best solution.我将尝试您的一些建议,然后接受最佳解决方案。

You can get rid of COM Interop altogether, by using EPPlus (here) .您可以使用 EPPlus (here)完全摆脱 COM Interop。 This library creates Excel files by using the 2007 XML format, not the Excel COM interface, and is a lot quicker as a result. This library creates Excel files by using the 2007 XML format, not the Excel COM interface, and is a lot quicker as a result.

You can speed this up by treating the 1000 rows with 8 to 10 columns as an array and copy your data into this array in one go instead of filling each one of those 8000 to 10000 cells on its own.您可以通过将具有 8 到 10 列的 1000 行视为一个数组并将您的数据复制到一个 go 中的该数组中,而不是单独填充这 8000 到 10000 个单元格中的每一个来加快这一速度。 See here for more info: Write Array to Excel Range有关更多信息,请参见此处:将数组写入 Excel 范围

Have you considered using C# tools such NPOI .您是否考虑过使用NPOI等 C# 工具。
It works very well and it doesn't require you to use COM Interop.它运行良好,不需要您使用 COM Interop。
Scott Mitchell wrote about it recently .斯科特米切尔最近写了一篇关于它的文章。

I wouldn't use an Excel app to do this, instead write it directly from c# using a library: Create Excel (.XLS and.XLSX) file from C# I wouldn't use an Excel app to do this, instead write it directly from c# using a library: Create Excel (.XLS and.XLSX) file from C#

MUCH faster.快多了。

Rgds GJ Rgds GJ

Sometimes it's sufficient to save your files as a text file in csv format.有时将文件保存为csv格式的文本文件就足够了。 It can be opened by Excel like a regular Excel sheet.它可以像普通的 Excel 表一样由 Excel 打开。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM