First, use Streamwrite flow object, export Excel
1.
string _spath = generatesalarymonthlyreport (dgvsalarysum); System.Diagnostics.Process.Start (_spath);
2.
Publicstring Generatesalarymonthlyreport (DataGridView dgvdata) {string _spath= String.Format (@". \{0}.txt", Baseclass.getguid ()); String _sgoal= String.Format (@"{0}{1}.xls", system.configuration.configurationmanager.appsettings["Statistic"], Baseclass.excelfilename ("Employee monthly Salary Table")); System.IO.StreamWriter _SW=NewSystem.IO.StreamWriter (_spath,false, Encoding.default); //Table Header_SW. WriteLine ("Human Resource management system V2.0"); _SW. WriteLine ("Compensation Management Module << employee monthly salary Table >>"); _SW. WriteLine (); _SW. Write (String.Format ("{0}\t","Serial Number")); foreach(DataGridViewColumn DGVCinchdgvdata.columns) {if(DGVC. Visible) {_sw. Write (String.Format ("{0}\t", DGVC. HeaderText)); }} _sw. Write (String.Format ("{0}\t{1}\t{2}","Employee Signature","Date","Notes")); _SW. WriteLine (); inti =1; foreach(DataGridViewRow DGVRinchdgvdata.rows) {_sw. Write (String.Format ("{0}", i)); for(intj =0; J < DgvData.Columns.Count-1; J + +) { if(Dgvdata.columns[j]. Visible) {if(J = =3) {_sw. Write (String.Format ("\ t ' {0}", DGVR. CELLS[J]. Value)); } Else{_sw. Write (String.Format ("\t{0}", DGVR. CELLS[J]. Value)); }}} I++; _SW. WriteLine (); } _sw. WriteLine (); _SW. WriteLine (String.Format ("{0}\t\t{1}\t\t\t\t{2}\t\t{3}","watchmaking People","Date of Tabulation","Auditor.","Audit Date")); _SW. Close (); System.IO.File.Move (_spath, _sgoal); return_sgoal; }
C # export Excel dynamic columns