Range merge exception: Already merged!

Hi.


Well, I’m not really trying to merge cells which are already merged, but Aspose.Cells tells me I’m doing so! This is a part of code facing error:

ws.Cells(cri, 0).Value = als(j).QGroup.DisplayOrder
rng = ws.Cells.CreateRange(cri, 0, als(j).QGroup.Questions.Count, 1)
rng.Merge()

And this is output error message:

Cells in range A9:A11 cannot be merged because cells in range A10:H10 have already been merged.

at ’’..Merge(Cells , Int32 , Int32 , Int32 , Int32 )
at Aspose.Cells.Cells.Merge(Int32 firstRow, Int32 firstColumn, Int32 totalRows, Int32 totalColumns)
at Aspose.Cells.Range.Merge()
at PAWeb.GAPersonalResultsCentral.DoGenerateReport(Object sender, EventArgs e) in C:\Users\Administrator\documents\visual studio 2013\Projects\Performance Appraizal\Performance Appraizal Web\Level05\GeneralAppraisal\GAPersonalResultsCentral.aspx.vb:line 665

When I remove merge command (line 665, as displayed above), I receive attached excel file. I couldn’t figure out cell A10 (which seems to be the cause of error) is merged with which of the other cells. It seems to be completely alone! And more strange, A10:H10 is not merged at all! (C10:H10 is merged, however) Some lines forward, I have another merge command which tries to merge B9:B11 and I receive the same error because A10:H10 is already merged! Does anyone have any idea about this?

EDIT: I was working on color styles and found that the above code block (which contains color formatting after merge) does strange things. I put a message box to find out which cell range every rng variable refers to, and reached A9:A11, A12:A15, A16:A17, A18 (alone) and so on. This is what I need, but when I apply colors and other formatting, merged ranges A37:H37, A38:L38, A:39, B39 and some other cells are also affected! Complete code:

ws.Cells(cri, 0).Value = als(j).QGroup.DisplayOrder
rng = ws.Cells.CreateRange(cri, 0, als(j).QGroup.Questions.Count, 1)
'rng.Merge()
st = wb.CreateStyle
st.Font.Name = “B Nazanin”
st.Font.Size = 11
st.HorizontalAlignment = Cells.TextAlignmentType.Center
st.VerticalAlignment = Cells.TextAlignmentType.Center
st.Borders.SetColor(System.Drawing.Color.Black)
st.Borders.SetStyle(Cells.CellBorderType.Thin)
st.Borders.DiagonalStyle = Cells.CellBorderType.None
rng.SetStyle(st)
ws.Cells(cri, 1).Value = als(j).QGroup.Name
rng = ws.Cells.CreateRange(cri, 1, als(j).QGroup.Questions.Count, 1)
'rng.Merge()
st = wb.CreateStyle
st.Font.Name = “B Nazanin”
st.Font.Size = 11
st.VerticalAlignment = Cells.TextAlignmentType.Center
st.Borders.SetColor(System.Drawing.Color.Black)
st.Borders.SetStyle(Cells.CellBorderType.Thin)
st.Borders.DiagonalStyle = Cells.CellBorderType.None
rng.SetStyle(st)

And program output excel file besides expected one is attached.

Hi,


Thanks for the template files.

Please try our latest version/ fix: Aspose.Cells for .NET v8.6.0.5

I have evaluated your scenario/ case(s) a bit.
1) Regarding merging cells issue, I used your template file with simplest code mentioned below using v8.6.0.5, it works fine and I do not get the exception. The output file is fine tuned. I have also attached the output file for your reference here.
e.g
Sample code:

Dim workbook As New Workbook(“e:\test2\Sample.xlsx”)
Dim worksheet As Worksheet = workbook.Worksheets(0)

’ Merge the ranges
Dim range As Range = worksheet.Cells.CreateRange(“A9:A11”)
range.Merge()
Dim range2 As Range = worksheet.Cells.CreateRange(“B9:B11”)
range2.Merge()
workbook.Save(“e:\test2\out1Sample1.xlsx”)


If you still find the issue with v8.6.0.5, kindly provide us runnable sample code as above to reproduce the issue on our end, we will check it soon.

2) Regarding color styles/formatting issue, I cannot evaluate your code for I am not sure about some variables/ objects and their values. We appreciate if you could create a simple console application (runnable), zip it and post us here with all the template files, we will check it soon. Also provide some screen shot(s) to highlight the problematic areas, this will surely help us to evaluate your issue precisely to consequently figure it out soon.

Thank you.

Many thanks for your reply, Amjad.


Tested latest version, no change.
In the same code mentioned above, I am using merge command without any problem(s), but in a little less complex worksheet structure. Unfortunately, because of application complexity, I cannot write a sample code in this case which can represent real case scenario and because this is a web-based application, transporting the entire code to your side is absolutely impossible. I can only tell you that all variables have their correct values (traced) and all ranges (according to RefersTo property) point to exactly expected ranges (traced too). I tested manually addressing A9:A11 for merge as you did, no success (exactly the same error). This new version also didn’t solve coloring problem.

Regards.

Hi,


I am afraid, how could we figure out the issue without reproducing it on our end. So, we would need your simulation application (runnable) to evaluate your issue properly, I still suspect the issue might be due to your own code you have used in your application, anyways, kindly do the needful and provide a sample project/ application, zip it and post us here to show the issue one end, we will check it soon.

Thank you.

After an hour of work, I finally created a windows application with real-case scenario. A RAR file is attached containing a database and a solution containing two projects. One is a class library containing all classes I wrote myself, and another is the real code running in my ASP.NET page as a Windows forms application. Database should be copied to C:\ (or wherever you prefer, but remember to change path in Windows application). Result will be generated as C:\Output.xlsx (or whatever you want through changing code). I hope this helps you completely re-produce the issue and figure it out. You may need this driver (ACE OLEDB 12.0) from Microsoft to connect to database via OLEDB.


Regards.

I figured it out. The problem was in my code. I was doing it badly wrong. Sorry to take your time.


Regards.

Hi Mohammad,

Thanks for your posting and using Aspose.Cells.

It is a pleasure to know that you were able to sort out this issue yourself. We appreciate your time an efforts in figuring out this issue. Let us know if you encounter any other issue, we will be glad to look into it and help you further.