Hi use below code
Add reference below in your project
Microsoft office 12.0 controls library
add namespace
using Microsoft.Office.Interop.Excel;
DataSet dset = new DataSet();
dset.Tables.Add();
for (int i = 0; i < grdBothPrintXLFull.Columns.Count; i++)
{
dset.Tables[0].Columns.Add(grdBothPrintXLFull.Columns[i].HeaderText);
}
System.Data.DataRow dr1;
for (int i = 0; i < grdBothPrintXLFull.Rows.Count; i++)
{
dr1 = dset.Tables[0].NewRow();
System.Web.UI.WebControls.Label lblCCName = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblCCName");
System.Web.UI.WebControls.Label lblItemName = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblItemName");
System.Web.UI.WebControls.Label lblItemCode = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblItemCode");
System.Web.UI.WebControls.Label lblItemDesc = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblItemDesc");
System.Web.UI.WebControls.Label lblBINNo = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblBINNo");
System.Web.UI.WebControls.Label lblItemNature = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblItemNature");
System.Web.UI.WebControls.Label lblUOM = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblUOM");
System.Web.UI.WebControls.Label lblOPBAL = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblOPBAL");
System.Web.UI.WebControls.Label lblInQty = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblInQty");
System.Web.UI.WebControls.Label lblOutQty = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblOutQty");
System.Web.UI.WebControls.Label lblBal = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblBal");
System.Web.UI.WebControls.Label lblMinStkLvl = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblMinStkLvl");
System.Web.UI.WebControls.Label lblSecUOM = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblSecUOM");
System.Web.UI.WebControls.Label lblSecBal = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblSecBal");
System.Web.UI.WebControls.Label lblRate = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblRate");
System.Web.UI.WebControls.Label lblStockValue = (System.Web.UI.WebControls.Label)grdBothPrintXLFull.Rows[i].Cells[0].FindControl("lblStockValue");
dr1[0] = lblCCName.Text.ToString();
dr1[1] = lblItemName.Text.ToString();
dr1[2] = lblItemCode.Text.ToString();
dr1[3] = lblItemDesc.Text.ToString();
dr1[4] = lblBINNo.Text.ToString();
dr1[5] = lblItemNature.Text.ToString();
dr1[6] = lblUOM.Text.ToString();
dr1[7] = lblOPBAL.Text.ToString();
dr1[8] = lblInQty.Text.ToString();
dr1[9] = lblOutQty.Text.ToString();
dr1[10] = lblBal.Text.ToString();
dr1[11] = lblMinStkLvl.Text.ToString();
dr1[12] = lblSecUOM.Text.ToString();
dr1[13] = lblSecBal.Text.ToString();
dr1[14] = lblRate.Text.ToString();
dr1[15] = lblStockValue.Text.ToString();
dset.Tables[0].Rows.Add(dr1);
}
dr1 = dset.Tables[0].NewRow();
dr1[0] = "";
dr1[1] = "";
dr1[2] = "";
dr1[3] = "";
dr1[4] = "";
dr1[5] = "";
dr1[6] = "";
dr1[7] = "";
dr1[8] = "";
dr1[9] = "";
dr1[10] = "";
dr1[11] = "";
dr1[12] = "";
dr1[13] = "";
dr1[14] = "Tot Val (INR):";
dr1[15] = ((System.Web.UI.WebControls.Label)grdBothPrintXLFull.FooterRow.FindControl("lblGrandTotal")).Text;
dset.Tables[0].Rows.Add(dr1);
ApplicationClass excel = new ApplicationClass();
Workbook wBook;
Worksheet wSheet;
wBook = excel.Workbooks.Add(System.Reflection.Missing.Value);
wSheet = (Worksheet)wBook.ActiveSheet;
System.Data.DataTable dt = dset.Tables[0];
System.Data.DataColumn dc = new DataColumn();
int colIndex = 0;
int rowIndex = 4;
foreach (DataColumn dcol in dt.Columns)
{
colIndex = colIndex + 1;
excel.Cells[5, colIndex] = dcol.ColumnName;
}
foreach (DataRow drow in dt.Rows)
{
rowIndex = rowIndex + 1;
colIndex = 0;
foreach (DataColumn dcol in dt.Columns)
{
colIndex = colIndex + 1;
excel.Cells[rowIndex + 1, colIndex] = drow[dcol.ColumnName];
}
}
wSheet.Columns.AutoFit();
String strFileName = Server.MapPath("~\\Images\\StockStatement.xls");
Boolean blnFileOpen = false;
try
{
System.IO.FileStream fileTemp = File.OpenWrite(strFileName);
fileTemp.Close();
}
catch
{
blnFileOpen = false;
}
if (System.IO.File.Exists(strFileName))
{
System.IO.File.Delete(strFileName);
}
Range oRng;
wSheet.Cells[1, 2] = lblOffice1.Text;
wSheet.Cells[3, 2] = lblCostCenter1.Text;
wSheet.Cells[4, 1] = lblOfficeName1.Text;
wSheet.get_Range("B1", "B1").Font.Bold = true;
wSheet.get_Range("B1", "B1").Font.ColorIndex = 55;
wSheet.get_Range("B3", "B3").Font.ColorIndex = 55;
wSheet.get_Range("A4", "A4").Font.ColorIndex = 55;
wSheet.get_Range("B1", "E1").Merge(Type.Missing);
wSheet.get_Range("B3", "E3").Merge(Type.Missing);
wSheet.get_Range("B1", "B1").HorizontalAlignment = Constants.xlCenter;
wSheet.get_Range("B3", "B3").HorizontalAlignment = Constants.xlCenter;
wSheet.get_Range("B3", "B3").Font.Bold = true;
wSheet.get_Range("A4", "A4").Font.Bold = true;
wSheet.get_Range("A4", "A4").HorizontalAlignment = Constants.xlLeft;
wSheet.get_Range("A5", "P5").Font.Bold = true;
wSheet.get_Range("A5", "P5").Interior.ColorIndex = 43;
wSheet.Name = "Stock Statement";
oRng = wSheet.get_Range("A1", "P1");
oRng.EntireColumn.AutoFit();
wBook.SaveAs(strFileName, XlFileFormat.xlExcel12, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false, XlSaveAsAccessMode.xlShared, XlSaveConflictResolution.xlLocalSessionChanges, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false);