如何让Excel中的合并单元格使用的Microsoft.Office.Interop.Excel文件只一次在C# [英] how to get merged cells of excel file only one time in c# using microsoft.office.interop.excel

查看:1130
本文介绍了如何让Excel中的合并单元格使用的Microsoft.Office.Interop.Excel文件只一次在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Microsoft.Office.Interop.Excel读取Excel文件在C#。当文件具有由包含合并单元格中的细胞数合并,它是循环细胞,同时我希望一次合并后的区域

i am reading excel file in C# using microsoft.office.interop.excel. when the file having merged cells it is looping by containing number of cells in merged cell while i want the merged area at once

这里是我的方法

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文件截图

here is the excel file screenshot

在此,我想[3,1] [4,1] [5,1] [6,1] [7,1] [8,1] [9,1]细胞只有一次在其完整的循环宽度和高度

in this i want [3,1] [4,1] [5,1] [6,1] [7,1][8,1][9,1] cell just once in loop with its complete width and height

推荐答案

微软目前并不提倡,不支持,Microsoft Office应用程序的自动化,从任何无人参与的非交互式客户端应用程序或组件(包括ASP ,ASP.NET,DCOM和NT服务),因为Office可能会出现不稳定的行为和/或死锁办公室时,在此环境中运行。

如果你正在构建在服务器端上下文中运行的解决方案,你应该尝试使用已取得安全的无人参与的执行元件。或者,你应该尝试寻找替代品,允许code运行客户端至少一部分。如果您使用从服务器端解决方案的Office应用程序,该应用程序会缺乏许多必要的能力,以成功运行。此外,您将承担风险与整体解决方案的稳定性。

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

您可以在注意事项服务器 - 阅读更多有关办公室文章端自动化。您可以考虑使用Open XML SDK或专为服务器端执行任何其他第三方组件。请参见欢迎您到办公室开放XML SDK 2.5的更多信息

You can read more about that in the Considerations for server-side Automation of Office article. You may consider using the Open XML SDK or any other third-party components designed for the server-side execution. See Welcome to the Open XML SDK 2.5 for Office for more information.

这篇关于如何让Excel中的合并单元格使用的Microsoft.Office.Interop.Excel文件只一次在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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