Note: The DLL method for office is not called.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;usingUtility;usingSystem.IO;namespacemytool{ Public classSaveasexcel {/// <summary> ///Save As Excel (parameter is passed over the DataGridView table)/// </summary> /// <param name= "DGV" ></param> Public Static voidDatagridviewtoexcel (DataGridView dgv) {SaveFileDialog Dlg=NewSaveFileDialog (); Dlg. Filter="execl Files (*.xls) |*.xls"; Dlg. FilterIndex=0; Dlg. Restoredirectory=true; Dlg. Createprompt=true; Dlg. Title="Save As Excel file"; if(DLG. ShowDialog () = =DialogResult.OK) {Stream mystream; MyStream=dlg. OpenFile (); StreamWriter SW=NewStreamWriter (MyStream, System.Text.Encoding.GetEncoding (-0)); stringColumntitle =""; Try { //Write column headings for(inti =0; I < DGV. ColumnCount; i++) { if(I >0) {Columntitle+="\ t"; } columntitle+=DGV. Columns[i]. HeaderText; } SW. WriteLine (Columntitle); //Write column Contents for(intj =0; J < DGV. Rows.Count; J + +) { stringColumnvalue =""; for(intK =0; K < DGV. Columns.count; k++) { if(k >0) {Columnvalue+="\ t"; } if(DGV. ROWS[J]. CELLS[K]. Value = =NULL) Columnvalue+=""; ElseColumnvalue+=DGV. ROWS[J]. CELLS[K]. Value.tostring (). Trim (); } SW. WriteLine (Columnvalue); } SW. Close (); Mystream.close (); } Catch(Exception e) {MessageBox.Show (e.tostring ()); } finally{SW. Close (); Mystream.close (); } } } }}