jjzjj

c# - Microsoft Office 互操作性能问题

coder 2024-06-01 原文

我尝试在我的 C# 4.0 Windows 应用程序中将 datagridview 数据导出到 excel 文件。

我们使用了 Microsoft.Office.Interop.Excel dll 版本 12.0.0.0。一切都很好

很好。但是当我尝试导出超过 1000 条 datagridview 记录时,它花费的时间太长了

时间。我怎样才能提高性能。

请参阅下面的 Excel 帮助程序代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using System.Runtime.InteropServices; // For COMException
using System.Reflection; // For Missing.Value and BindingFlags
using System.Diagnostics; // to ensure EXCEL process is really killed

namespace Export.Excel
{

    #region InstanceFields

    //Instance Fields
    //public delegate void ProgressHandler(object sender, ProgressEventArgs e);
    //public event ProgressHandler prg;
    private System.Data.DataView dv;
    private Style styleRows;
    private Style styleColumnHeadings;
    private Microsoft.Office.Interop.Excel.Application EXL;
    private Workbook workbook;
    private Sheets sheets;
    private Worksheet worksheet;
    private string[,] myTemplateValues;
    private int position;
    private System.Globalization.CultureInfo cl;
    private Type _ResourceType;

    #endregion

    #region Constructor

    //Constructs a new export2Excel object. The user must
    //call the createExcelDocument method once a valid export2Excel
    //object has been instantiated

    public ExportExcelFormat(string culture, Type type)
    {
        cl = new System.Globalization.CultureInfo(culture);
        _ResourceType = type;
    }

    #endregion


    #region EXCEL : ExportToExcel
    //Exports a DataView to Excel. The following steps are carried out
    //in order to export the DataView to Excel
    //Create Excel Objects
    //Create Column & Row Workbook Cell Rendering Styles
    //Fill Worksheet With DataView
    //Add Auto Shapes To Excel Worksheet
    //Select All Used Cells
    //Create Headers/Footers
    //Set Status Finished
    //Save workbook & Tidy up all objects
    //@param dv : DataView to use
    //@param path : The path to save/open the EXCEL file to/from
    //@param sheetName : The target sheet within the EXCEL file
    public void ExportToExcel(System.Data.DataView dv, string path, string sheetName, string[] UnWantedColumns)
    {
        try
        {
            //Assign Instance Fields
            this.dv = dv;

            #region NEW EXCEL DOCUMENT : Create Excel Objects

            //create new EXCEL application
            EXL = new Microsoft.Office.Interop.Excel.ApplicationClass();
            //index to hold location of the requested sheetName in the workbook sheets
            //collection
            int indexOfsheetName;

            #region FILE EXISTS
            //Does the file exist for the given path
            if (File.Exists(path))
            {

                //Yes file exists, so open the file
                workbook = EXL.Workbooks.Open(path,
                    0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
                    true, false, 0, true, false, false);

                //get the workbook sheets collection
                sheets = workbook.Sheets;

                //set the location of the requested sheetName to -1, need to find where
                //it is. It may not actually exist
                indexOfsheetName = -1;

                //loop through the sheets collection
                for (int i = 1; i <= sheets.Count; i++)
                {
                    //get the current worksheet at index (i)
                    worksheet = (Worksheet)sheets.get_Item(i);

                    //is the current worksheet the sheetName that was requested
                    if (worksheet.Name.ToString().Equals(sheetName))
                    {
                        //yes it is, so store its index
                        indexOfsheetName = i;

                        //Select all cells, and clear the contents
                        Microsoft.Office.Interop.Excel.Range myAllRange = worksheet.Cells;
                        myAllRange.Select();
                        myAllRange.CurrentRegion.Select();
                        myAllRange.ClearContents();
                    }
                }

                //At this point it is known that the sheetName that was requested
                //does not exist within the found file, so create a new sheet within the
                //sheets collection
                if (indexOfsheetName == -1)
                {
                    //Create a new sheet for the requested sheet
                    Worksheet sh = (Worksheet)workbook.Sheets.Add(
                        Type.Missing, (Worksheet)sheets.get_Item(sheets.Count),
                        Type.Missing, Type.Missing);
                    //Change its name to that requested
                    sh.Name = sheetName;
                }
            }
            #endregion

            #region FILE DOESNT EXIST
            //No the file DOES NOT exist, so create a new file
            else
            {
                //Add a new workbook to the file
                workbook = EXL.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                //get the workbook sheets collection
                sheets = workbook.Sheets;
                //get the new sheet
                worksheet = (Worksheet)sheets.get_Item(1);
                //Change its name to that requested
                worksheet.Name = sheetName;
            }
            #endregion

            #region get correct worksheet index for requested sheetName

            //get the workbook sheets collection
            sheets = workbook.Sheets;

            //set the location of the requested sheetName to -1, need to find where
            //it is. It will definately exist now as it has just been added
            indexOfsheetName = -1;

            //loop through the sheets collection
            for (int i = 1; i <= sheets.Count; i++)
            {
                //get the current worksheet at index (i)
                worksheet = (Worksheet)sheets.get_Item(i);



                //is the current worksheet the sheetName that was requested
                if (worksheet.Name.ToString().Equals(sheetName))
                {
                    //yes it is, so store its index
                    indexOfsheetName = i;
                }
            }

            //set the worksheet that the DataView should write to, to the known index of the
            //requested sheet
            worksheet = (Worksheet)sheets.get_Item(indexOfsheetName);
            #endregion

            #endregion

            // Set styles 1st
            SetUpStyles();
            //Fill EXCEL worksheet with DataView values
            fillWorksheet_WithDataView(UnWantedColumns);
            //Add the autoshapes to EXCEL
            //AddAutoShapesToExcel();
            //Select all used cells within current worksheet
            SelectAllUsedCells();

            try
            {
                workbook.Close(true, path, Type.Missing);
                EXL.UserControl = false;
                EXL.Quit();
                EXL = null;
                //kill the EXCEL process as a safety measure
                killExcel();
            }
            catch (COMException cex)
            {

            }
            catch (Exception ex)
            {

            }
        }
        catch (Exception ex)
        {

        }
    }
    #endregion

    #region EXCEL : UseTemplate
    //Exports a DataView to Excel. The following steps are carried out
    //in order to export the DataView to Excel
    //Create Excel Objects And Open Template File
    //Select All Used Cells
    //Create Headers/Footers
    //Set Status Finished
    //Save workbook & Tidy up all objects
    //@param path : The path to save/open the EXCEL file to/from
    public void UseTemplate(string path, string templatePath, string[,] myTemplateValues)
    {
        try
        {
            this.myTemplateValues = myTemplateValues;
            //create new EXCEL application
            EXL = new Microsoft.Office.Interop.Excel.ApplicationClass();
            //Yes file exists, so open the file
            workbook = EXL.Workbooks.Open(templatePath,
                0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
                true, false, 0, true, false, false);
            //get the workbook sheets collection
            sheets = workbook.Sheets;
            //get the new sheet
            worksheet = (Worksheet)sheets.get_Item(1);
            //Change its name to that requested
            worksheet.Name = "ATemplate";
            //Fills the Excel Template File Selected With A 2D Test Array
            fillTemplate_WithTestValues();
            //Select all used cells within current worksheet
            SelectAllUsedCells();

            try
            {
                workbook.Close(true, path, Type.Missing);
                EXL.UserControl = false;
                EXL.Quit();
                EXL = null;
                //kill the EXCEL process as a safety measure
                killExcel();
            }
            catch (COMException)
            {
            }
        }
        catch (Exception ex)
        {
        }
    }
    #endregion

    #region STEP 1 : Create Column & Row Workbook Cell Rendering Styles
    //Creates 2 Custom styles for the workbook These styles are
    //  styleColumnHeadings
    //  styleRows
    //These 2 styles are used when filling the individual Excel cells with the
    //DataView values. If the current cell relates to a DataView column heading
    //then the style styleColumnHeadings will be used to render the current cell.
    //If the current cell relates to a DataView row then the style styleRows will
    //be used to render the current cell.
    private void SetUpStyles()
    {
        // Style styleColumnHeadings
        try
        {
            styleColumnHeadings = workbook.Styles["styleColumnHeadings"];
        }
        // Style doesn't exist yet.
        catch
        {
            styleColumnHeadings = workbook.Styles.Add("styleColumnHeadings", Type.Missing);
            styleColumnHeadings.Font.Name = "Arial";
            styleColumnHeadings.Font.Size = 12;
            styleColumnHeadings.Font.Bold = true;
        }

        // Style styleRows
        try
        {

            styleRows = workbook.Styles["styleRows"];
        }
        // Style doesn't exist yet.
        catch
        {
            styleRows = workbook.Styles.Add("styleRows", Type.Missing);
            styleRows.Font.Name = "Verdana";
            styleRows.Font.Size = 9;
        }
    }
    #endregion

    #region STEP 2 : Fill Worksheet With DataView
    //Fills an Excel worksheet with the values contained in the DataView
    //parameter
    private void fillWorksheet_WithDataView(string[] UnWantedColumns)
    {
        position = 0;
        //Add DataView Columns To Worksheet
        int row = 1;
        int col = 1;
        // Remove unwanted columns in the loop
        int total = dv.Table.Columns.Count - UnWantedColumns.Count();
        // Loop thought the columns
        for (int i = 0; i < total; i++)
        {

            fillExcelCell(worksheet, row, col++, dv.Table.Columns[i].ToString(), styleColumnHeadings.Name, UnWantedColumns);
        }

        //Add DataView Rows To Worksheet
        row = 2;
        col = 1;

        for (int i = 0; i < dv.Table.Rows.Count; i++)
        {

            for (int j = 0; j < dv.Table.Columns.Count; j++)
            {
                fillExcelCell(worksheet, row, col++, dv[i][j].ToString(), styleRows.Name, UnWantedColumns);
            }
            col = 1;
            row++;

            position = (100 / dv.Table.Rows.Count) * row + 2;
        }
    }
    #endregion

    #region STEP 3 : Fill Individual Cell and Render Using Predefined Style
    //Formats the current cell based on the Style setting parameter name
    //provided here
    //@param worksheet : The worksheet
    //@param row : Current row
    //@param col : Current Column
    //@param Value : The value for the cell
    //@param StyleName : The style name to use
    private void fillExcelCell(Worksheet worksheet, int row, int col, Object Value, string StyleName, string[] UnWantedColumns)
    {
        if (!UnWantedColumns.Contains(Value.ToString()))
        {
            Range rng = (Range)worksheet.Cells[row, col];
            rng.NumberFormat = "@";
            rng.Select();
            rng.Value2 = Value.ToString();
            rng.Style = StyleName;
            rng.Columns.EntireColumn.AutoFit();
        }
    }
    #endregion

    #region STEP 4 : Add Auto Shapes To Excel Worksheet
    //Add some WordArt objecs to the Excel worksheet
    private void AddAutoShapesToExcel()
    {
        //Method fields
        float txtSize = 80;
        float Left = 100.0F;
        float Top = 100.0F;
        //Have 2 objects
        int[] numShapes = new int[2];
        Microsoft.Office.Interop.Excel.Shape[] myShapes = new Microsoft.Office.Interop.Excel.Shape[numShapes.Length];

        try
        {
            //loop through the object count
            for (int i = 0; i < numShapes.Length; i++)
            {

                //Add the object to Excel
                myShapes[i] = worksheet.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, "DRAFT", "Arial Black",
                    txtSize, MsoTriState.msoFalse, MsoTriState.msoFalse, (Left * (i * 3)), Top);

                //Manipulate the object settings
                myShapes[i].Rotation = 45F;
                myShapes[i].Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
                myShapes[i].Fill.Transparency = 0F;
                myShapes[i].Line.Weight = 1.75F;
                myShapes[i].Line.DashStyle = MsoLineDashStyle.msoLineSolid;
                myShapes[i].Line.Transparency = 0F;
                myShapes[i].Line.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                myShapes[i].Line.ForeColor.RGB = (0 << 16) | (0 << 8) | 0;
                myShapes[i].Line.BackColor.RGB = (255 << 16) | (255 << 8) | 255;
            }
        }
        catch (Exception ex)
        {
        }

    }
    #endregion

    #region STEP 5 : Select All Used Cells
    //Selects all used cells for the Excel worksheet
    private void SelectAllUsedCells()
    {
        Microsoft.Office.Interop.Excel.Range myAllRange = worksheet.Cells;
        myAllRange.Select();
        myAllRange.CurrentRegion.Select();
    }
    #endregion

    #region STEP 6 : Fill Template With Test Values
    //Fills the Excel Template File Selected With A 2D Test Array parameter
    private void fillTemplate_WithTestValues()
    {
        //Initilaise the correct Start Row/Column to match the Template
        int StartRow = 3;
        int StartCol = 2;

        position = 0;

        // Display the array elements within the Output window, make sure its correct before
        for (int i = 0; i <= myTemplateValues.GetUpperBound(0); i++)
        {
            //loop through array and put into EXCEL template
            for (int j = 0; j <= myTemplateValues.GetUpperBound(1); j++)
            {
                //update position in progress bar
                position = (100 / myTemplateValues.Length) * i;

                //put into EXCEL template
                Range rng = (Range)worksheet.Cells[StartRow, StartCol++];
                rng.Select();
                rng.Value2 = myTemplateValues[i, j].ToString();
                rng.Rows.EntireRow.AutoFit();
            }
            //New row, so column needs to be reset
            StartCol = 2;
            StartRow++;
        }
    }

    #endregion

    #region Kill EXCEL
    //As a safety check go through all processes and make
    //doubly sure excel is shutdown. Working with COM
    //have sometimes noticed that the EXL.Quit() call
    //does always do the job
    private void killExcel()
    {
        try
        {
            Process[] ps = Process.GetProcesses();
            foreach (Process p in ps)
            {
                if (p.ProcessName.ToLower().Equals("excel"))
                {
                    p.Kill();
                }
            }
        }
        catch (Exception ex)
        {
        }
    }
    #endregion
}

最佳答案

我有一些改进性能的建议。单独使用它们可能不会产生太大影响,但它们一起应该可以提高整体性能。

  • 隐藏 Excel(如果尚未隐藏)EXL.Visible = false;。关掉 计算(Application.Calculation = xlCalculationManual,如果它 不需要)和 ScreenUpdating
  • 使用 Excel.Workbooks.Worksheets 而不是 Sheets 集合。
  • 与其遍历所有工作表,不如尝试引用所需的工作表,使用错误处理来确定工作表是否存在:

    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets["SheetName"];
    

避免Select,它很少需要——而且很慢。替换,

//Select all cells, and clear the contents
Microsoft.Office.Interop.Excel.Range myAllRange = worksheet.Cells;
myAllRange.Select();
myAllRange.CurrentRegion.Select();
myAllRange.ClearContents();

worksheet.UsedRange.ClearContents();

您应该能够完全删除函数 SelectAllUsedCells()。如果您仍然需要选择它们,则:

worksheet.UsedRange.Select();    // but shouldn't be necessary

否则,如果您坚持循环遍历工作表,请在找到工作表后使用 break; 退出循环。

fillExcelCell() 函数中删除 rng.Select();。但是,您似乎在为每个单元格调用此函数;这个对吗?之后我会一次性完成所有格式设置。特别是,将 AutoFit 应用于整个范围。

我会创建一次形状并复制/粘贴它。 (不确定是否可以克隆?)

完成后将计算模式恢复到原来的设置。

关于c# - Microsoft Office 互操作性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17082635/

有关c# - Microsoft Office 互操作性能问题的更多相关文章

  1. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  2. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  3. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  4. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  5. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  6. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  7. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  8. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  9. ruby-on-rails - 简单的 Ruby on Rails 问题——如何将评论附加到用户和文章? - 2

    我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。

  10. 【高数】用拉格朗日中值定理解决极限问题 - 2

    首先回顾一下拉格朗日定理的内容:函数f(x)是在闭区间[a,b]上连续、开区间(a,b)上可导的函数,那么至少存在一个,使得:通过这个表达式我们可以知道,f(x)是函数的主体,a和b可以看作是主体函数f(x)中所取的两个值。那么可以有,  也就意味着我们可以用来替换 这种替换可以用在求某些多项式差的极限中。方法: 外层函数f(x)是一致的,并且h(x)和g(x)是等价无穷小。此时,利用拉格朗日定理,将原式替换为 ,再进行求解,往往会省去复合函数求极限的很多麻烦。使用要注意:1.要先找到主体函数f(x),即外层函数必须相同。2.f(x)找到后,复合部分是等价无穷小。3.要满足作差的形式。如果是加

随机推荐