C# 如何使用microsoft.office.interop.excel在c中只获取一次excel文件的合并单元格

C# 如何使用microsoft.office.interop.excel在c中只获取一次excel文件的合并单元格,c#,asp.net,asp.net-mvc-4,office-interop,excel-interop,C#,Asp.net,Asp.net Mvc 4,Office Interop,Excel Interop,我正在使用microsoft.office.interop.excel阅读C语言中的excel文件。当文件包含合并的单元格时,它通过在合并的单元格中包含单元格的数量来循环,而我希望立即合并区域 这是我的方法 public void ImportXLX() { Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

我正在使用microsoft.office.interop.excel阅读C语言中的excel文件。当文件包含合并的单元格时,它通过在合并的单元格中包含单元格的数量来循环,而我希望立即合并区域

这是我的方法

public void ImportXLX()
    {
        Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

        Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(@"C:\Users\Vipin\Desktop\Sheets\MyXL6.xlsx", 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);

        int workSheetCounts = wb.Worksheets.Count;

        for (int sheetCounter = 1; sheetCounter <= workSheetCounts; sheetCounter++)
        {
            Microsoft.Office.Interop.Excel.Worksheet workSheet = wb.Sheets[sheetCounter];

            Range excelRange = workSheet.UsedRange;
            Range objRange = null;

            int rowCount = 0;
            float totalRowWidth = 0;
            foreach (Microsoft.Office.Interop.Excel.Range row in excelRange.Rows)
            {
                rowCount++;
                totalRowWidth = row.Width;
                int colCount = 0;

                foreach (Microsoft.Office.Interop.Excel.Range c in row.Cells)
                {
                    colCount++;
                    objRange = workSheet.Cells[rowCount, colCount];

                    double width = 0;
                    double height = 0;
                    string colVal = null;

                    if (objRange.MergeCells)
                    {
                        colVal = Convert.ToString(((Range)objRange.MergeArea[1, 1]).Text).Trim();
                        width = objRange.MergeArea.Width;
                        height = objRange.MergeArea.Height;


                    }
                    else
                    {
                        colVal = Convert.ToString(objRange.Text).Trim();
                        width = objRange.Width;
                        height = objRange.Height;
                    }
                    Debug.Write("objRange = " + objRange + " rowCount = " + rowCount + " Width = " + width + " height = " + height + "TotalColumnWidth = " + totalRowWidth + "TotalRowHeight = " + totalColHeight + " \n ");
                }
            }
        }
        app.Quit();
    }
这是excel文件的屏幕截图

在此我希望[3,1][4,1][5,1][6,1][7,1][8,1][9,1]单元以其完整的宽度和高度在循环中只运行一次

Microsoft目前不建议也不支持从任何无人值守、非交互式客户端应用程序或组件(包括ASP、ASP.NET、DCOM和NT服务)自动化Microsoft Office应用程序,因为在这种环境下运行Office时,Office可能会表现出不稳定的行为和/或死锁

如果您正在构建一个在服务器端上下文中运行的解决方案,那么您应该尝试使用安全的组件来无人值守地执行。或者,您应该尝试找到至少允许部分代码在客户端运行的替代方案。如果使用服务器端解决方案中的Office应用程序,该应用程序将缺少许多成功运行所需的功能。此外,您将面临整体解决方案稳定性方面的风险


你可以在这篇文章中了解更多。您可以考虑使用开放式XML SDK或为服务器端执行而设计的任何其他第三方组件。有关更多信息,请参阅。

但我正在获取合并单元格,我只需要它们的单元格。无论您做什么,这都不支持完成工作的方式。全部完成,先生。。。反正是thanx