Dynamically merge and export data to EXCEL and dynamically export excel

Source: Internet
Author: User

Dynamically merge and export data to EXCEL and dynamically export excel

Recently, I have been dealing with the problem of dynamic data merging and exporting EXCEL. I have written a demo record and hope that Boyou who have encountered the same problem can solve it smoothly; the demo download link will be provided later.

(VS2012, ASP. NET)

I. mainly solves the following problems:

1. dynamically MERGE table cells according to business logic
2. Number rewriting after dynamic merge
3. datatable export excel
4. datatable excel special character Export
5. There may be a few bugs in the actual problem. debug them on your own.

 

 

Ii. Important code excerpt:

1. the foreground obtains the table HTML code:

1 <input type = "hidden" runat = "server" id = "lblTableHtml"/>View Code1 function getDataHtml () {2 var table = document. getElementById ("gridTable"); 3 if (table! = Null) {4 var html = table. innerHTML; 5 document. getElementById ("lblTableHtml"). value = html; 6} 7}View Code

2. Custom merged cells:

1 // 1. Associate fields to group 2 for (int I = gridTable. rows. count-1; I> 0; I --) 3 {4 HtmlTableCell oCell_previous = gridTable. rows [I-1]. cells [4]; 5 HtmlTableCell oCell = gridTable. rows [I]. cells [4]; 6 if (oCell_previous! = Null & oCell! = Null) 7 {8 if (oCell. innerText = ocell_previus.innertext) 9 {10 for (int j = 4; j <= 7; j ++) 11 {12 HtmlTableCell ocell_previusi = gridTable. rows [I-1]. cells [j]; 13 HtmlTableCell oCellI = gridTable. rows [I]. cells [j]; 14 if (ocell_previusi! = Null & oCellI! = Null) 15 {16 oCell_previousI.RowSpan = (oCell_previousI.RowSpan =-1 )? 1: oCell_previousI.RowSpan; 17 oCellI. RowSpan = (oCellI. RowSpan =-1 )? 1: oCellI. RowSpan; 18} 19 20 oCellI. Visible = false; 21 oCell_previousI.RowSpan + = oCellI. RowSpan; 22} 23} 24} 25}View Code

3. Export EXCEL logic:

1 Response. clear (); 2 Response. contentType = "application/vnd. ms-excel "; 3 string excelname = sFileName + DateTime. now. toString ("yyyyMMddHHmmss"); 4 Response. addHeader ("content-disposition", "attachment; filename =" + System. web. httpUtility. urlEncode (excelname, System. text. encoding. UTF8) + ". xls "); 5 this. enableViewState = false; 6 System. IO. stringWriter oStringWriter = new System. IO. stringWriter (); 7 System. web. UI. htmlTextWriter writer = new System. web. UI. htmlTextWriter (oStringWriter); 8 System. text. stringBuilder builder = new System. text. stringBuilder (); 9 builder. append ("<table border = '1'>"); 10 // retrieve the table 11 string sHtml = lblTableHtml. value; 12 builder. append (sHtml); 13 builder. append ("</table>"); 14 writer. write (builder. toString (); 15 writer. close (); 16 Response. write (oStringWriter. toString (); 17 Response. end ();View Code

 

 

Iii. Demo download link:

Http://files.cnblogs.com/files/zhaosx/ExcelExportDemo.zip

Related Article

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.