C # merge Excel Cells

Source: Internet
Author: User

This article is based on the example in C # programming typical instance analysis.

Excel worksheet before merge:

After merging the Excel Worksheet:

The sorting functions are as follows:

 

Code

1 ///   <Summary>
2 /// Merge cells in a specified Excel File
3 ///   </Summary>
4 ///   <Param name = "mysheet"> Specified Excel worksheet </Param>
5 ///   <Param name = "Startline"> Start line </Param>
6 ///   <Param name = "reccount"> Total number of rows </Param>
7 ///   <Param name = "col"> Columns to be merged </Param>
8 Private   Void Mergecell_second ( Ref Worksheet mysheet, Int Startline, Int Reccount, String COL)
9 {
10 String Qy1 = Mysheet. get_range (COL + Startline. tostring (), Col + Startline. tostring (). Text. tostring (); // Obtains the filling content of cells in the merged column of the starting row.
11
12 Range rg1;
13 String Strtemp =   "" ;
14 Bool Endcycle =   False ;
15
16 // Loop from starting row to ending row
17 For ( Int I = Startline; I <= Reccount + Startline -   1 &&! Endcycle ;)
18 {
19 For ( Int J = I +   1 ; J <= Reccount + Startline -   1 ; J ++ )
20 {
21 Rg1. = Mysheet. get_range (COL + J. tostring (), Col + J. tostring ()); // Get the filling content of the next row
22 Strtemp = Rg1.text. tostring (). Trim ();
23
24 If (Strtemp. Trim () = Qy1.trim ()) // Content Equals initial content
25 {
26 Rg1. = Mysheet. get_range (COL + I. tostring (), Col + J. tostring ()); // Select the merge location of the last row and the merge region of the current row.
27 Rg1.clearcontents (); // Clear the region to be merged
28 Rg1.mergecells =   True ;
29 If (COL =   " A " )
30 Mysheet. cells [I, 1 ] = Qy1;
31 Else   If (COL =   " B " )
32 Mysheet. cells [I, 2 ] = Qy1;
33
34 If (J = Reccount + Startline -   1 )
35 {
36 Endcycle =   True ;
37 }
38 }
39 Else // Content is not equal to initial content
40 {
41 I = J; // I get new value
42 Qy1 = Mysheet. get_range (COL + J. tostring (), Col + J. tostring (). Text. tostring ();
43 Break ;
44 }
45 }
46 }
47 }

 

 

The call method is as follows:

 

1 Mergecell_second ( Ref Mysheet, 3 , Recordcount, " A " ); // Call the function to merge column
2 Mergecell_second ( Ref Mysheet, 3 , Recordcount, " B " ); // Use a function to merge columns B

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.