Single header, multi-header export Excel

Source: Internet
Author: User

I. Single Header

1 # region Export 2 if (this. dgvinfo. rows. count> 0) 3 {4 savefiledialog = new savefiledialog (); 5 savefiledialog. defaultext = "xls"; 6 savefiledialog. filter = "Excel file (*. XLS) | *. xls "; 7 savefiledialog. filterindex = 0; 8 savefiledialog. filename = "document details"; 9 savefiledialog. restoredirectory = true; 10 savefiledialog. createprompt = true; 11 savefiledialog. title = "export to excel"; 12 savefiledialog. showdialog (); 13 if (savefiledialog. filename = "") 14 return; 15 stream mystream; 16 mystream = savefiledialog. openfile (); 17 streamwriter Sw = new streamwriter (mystream, system. text. encoding. getencoding (-0); 18 string STR = ""; 19 try20 {21 for (INT I = 0; I <this. dgvinfo. columncount; I ++) 22 {23 if (I> 0) 24 {25 STR + = "\ t"; 26} 27 STR + = dgvinfo. columns [I]. headertext; 28} 29 SW. writeline (STR); 30 for (Int J = 0; j <dgvinfo. rows. count; j ++) 31 {32 string tempstr = ""; 33 for (int K = 0; k <dgvinfo. columns. count; k ++) 34 {35 if (k> 0) 36 {37 tempstr + = "\ t"; 38} 39 if (dgvinfo. rows [J]. cells [K]. value = NULL) 40 {41 dgvinfo. rows [J]. cells [K]. value = ""; 42} 43 tempstr + = dgvinfo. rows [J]. cells [K]. value. tostring (); 44} 45 SW. writeline (tempstr); 46} 47 SW. close (); 48 mystream. close (); 49} 50 catch (exception ex) 51 {52 MessageBox. show (ex. tostring (); 53} 54 finally55 {56 SW. close (); 57 mystream. close (); 58} 59} 60 # endregion

Ii. Multiple tables

1 try 2 {3 if (treedgv. rows. count> 0) 4 {5 string sfile = system. windows. forms. application. startuppath; 6 7 // missing in system. under the reflection namespace. 8 // string sfilesave = sfile + "\" + datetime. now. tostring ("yyyymmddhhmmss") + "one-day success. xls"; 9 10 savefiledialog = new savefiledialog (); 11 savefiledialog. defaultext = "xls"; 12 savefiledialog. filter = "Excel file (*. XLS) | *. xls "; 13 savefiledialog. filterindex = 0; 14 savefiledialog. filename = datetime. now. tostring ("yyyymmddhhmmss") + "Payment Method"; 15 savefiledialog. restoredirectory = true; 16 savef Iledialog. createprompt = true; 17 savefiledialog. title = "export to excel"; 18 savefiledialog. showdialog (); 19 if (savefiledialog. filename = "") 20 return; 21 22 // you need to add Microsoft. office. interOP. excel reference 23 Microsoft. office. interOP. excel. application APP = new Microsoft. office. interOP. excel. application (); 24 25 app. visible = false; 26 app. usercontrol = true; 27 Microsoft. office. interOP. excel. workbooks workbooks = App. workbooks; 28 Microsoft. office. interOP. excel. _ workbook = workbooks. add (sfile + "\ payment method .xls"); // load template 29 Microsoft. office. interOP. excel. sheets sheets = workbook. sheets; 30 Microsoft. office. interOP. excel. _ worksheet = (Microsoft. office. interOP. excel. _ worksheet) sheets. get_item (1); // The first working thin. 31 if (worksheet = NULL) 32 return; // No worksheet. 33 34 // write data in the workbook. The Excel Index starts from 1. 35 36 for (INT I = 1; I <= treedgv. rows. count; I ++) 37 {38 int row _ = 3 + I; // the header and header rows in the Excel template occupy two rows, which need to be modified according to the actual template; 39 int dt_row = I-1; // the row of the datatable starts from 0. 40 // worksheet. cells [row _, 1] = I. tostring (); 41 for (Int J = 1; j <= treedgv. columns. count; j ++) 42 {43 If (treedgv. rows [0]. cells ["column" + J]. value. tostring ()! = "") 44 {45 worksheet. cells [row _, J] = treedgv. rows [I-1]. cells ["column" + J]. value. tostring (); 46} 47} 48 49} 50 51 52 workbook. saveas (savefiledialog. filename, missing. value, missing. value, missing. value, missing. value, missing. value, Microsoft. office. interOP. excel. xlsaveasaccessmode. xlnochange, missing. value, missing. value, missing. value, missing. value, missing. value); 53 54 55 workbook. close (null, Null, null); 56 workbooks. close (); 57 worksheet = NULL; 58 app. quit (); 59 APP = NULL; 60} 61} 62 catch (exception) 63 {64 65 MessageBox. show ("Export failed! "," Exception "); 66}

 

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.