Merge the same content column +excel in the table merge same content column.

Source: Internet
Author: User

The method of merging the same Content column in table is good, the online code is also many, reference some to encapsulate it into jquery

Plug-in, call up still good use.

This place slightly modified, sometimes TD content is the same, but the value of the database is not the same, such as different companies, there are personnel, Finance Department,

Company A's finance department and Company B's Finance department cannot be merged, so I added a name attribute to TD. To save the ID of the department, the company department ID is different.

Invocation mode

$ (function () {

$ ("#tbdianbao"). RowSpan (1);//merge 2nd column,
$ ("#tbdianbao"). RowSpan (2);//Merge 3rd Column

});

?
12345678910111213141516171819202122232425262728293031323334353637383940414243 (function($) {    $.fn.extend({        //调用插件时的名字,传入的是列的索引0,1,2,,,        "rowspan": function(colIdx) {            var that;            var rowspan;            //遍历table中的行            $(this).find("tr").each(function(row) {            //获得该索引列的集合,并且用filter再过滤一下,取得可见的列,然后遍历它们                $(‘td:eq(‘ + colIdx + ‘)‘, this).filter(‘:visible‘).each(function(col) {                    //发现上下两列的内容一样                     if (that != null && $<span style="color: #ff0000;">(this).attr("name") == $(that).attr("name"))</span> {                        rowspan = $(that).attr("rowSpan");                        //如果是第一次进来合并两列                        if (rowspan == undefined) {                            $(that).attr("rowSpan", 1);                            rowspan = $(that).attr("rowSpan");                        }                                                rowspan = Number(rowspan) + 1;                        $(that).attr("rowSpan", rowspan);                        //将该列隐藏                        $(this).hide();                    }                    else {                        //相当保存上一列的对象                        that = this;                    }                });            });        }    });})(jQuery);

Generated tables We may also want to import into Excel, we need to merge the same columns, we do not understand the VBA child paper can not hurt Ah, so only in the generation of Excel when you judge the merge column

A column with the same company ID in the 2 column is merged from the 3rd line of Excel, and a column with the same department ID in the 3 column is merged

This.margecol (worksheet, 2, 2, Dtexcel, "gongsiid");
This.margecol (worksheet, 2, 3, Dtexcel, "Bumenid");

1  /// <summary>2         ///to merge columns in Excel3         /// </summary>4         /// <param name= "Worksheet" > Workbooks</param>5         /// <param name= "Rno" >The previous row of the workbook start line</param>6         /// <param name= "CNO" >Workbook Start Column</param>7         /// <param name= "dttemp" >Binding with Table</param>8         /// <param name= "ColName" >Merge with Columns</param>9         Private voidMargecol (Microsoft.office.interop.excel._worksheet Worksheet,intRnointCno,datatable Dttemp,stringcolname)Ten         { One             stringDcvalue =string. Empty; A  -             intSron=rno;//Start line -  the             foreach(DataRow Drinchdttemp. Rows) { -  -                 if(Dcvalue! =string. Empty && Dcvalue = =Dr[colname]. ToString ()) -                 { +Rno = Rno +1;//Row Accumulation -                 } +                 Else A                 { atDcvalue =Dr[colname]. ToString (); -  -                     //when the target line is larger than the start line, the content of the row is consistent -                     if(Rno >Sron) { - worksheet.get_range (worksheet. Cells[sron, CNO], worksheet. Cells[rno, CNO]). Merge (worksheet.get_range (worksheet. Cells[sron, CNO], worksheet. Cells[rno, CNO]). MergeCells); -                     } in                      -                     //The start line moves down one line after the merge ends toRno = Rno +1; +Sron =Rno; -                    the                 } *             } $                 Panax Notoginseng}

Merge the same content column +excel in the table merge same content column.

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.