Hi shabzo1,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView runat="server" ID="dataGridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<asp:Button Text="Save" runat="server" OnClick="Save" />
Namespaces
C#
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
dt.Rows.Add(1, "John Hammond", "United States");
dataGridView1.DataSource = dt;
dataGridView1.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
string sourcePath = @"C:\Users\dharmendra\Desktop\masterquote.xlsx";
string destinationPath = @"C:\Users\dharmendra\Desktop\Quote_" + "Test.xls";
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet = null;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(sourcePath);
string datestr = DateTime.Now.ToShortDateString();
string quote = "Test" + "_" + datestr;
string salesman = "txtEmail".ToUpper();
string customer = "txtName".ToUpper();
int currentSheet = 1;
int cell = 0;
int rowsToDisplay = 5;
int rowsToStart = 19;
for (int row = 0; row < dataGridView1.Rows.Count; row++)
{
if (currentSheet <= xlWorkBook.Sheets.Count)
{
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(currentSheet);
}
else
{
var xlSheets = xlWorkBook.Sheets as Excel.Sheets;
xlWorkSheet = (Excel.Worksheet)xlSheets.Add(misValue, xlSheets[xlWorkBook.Sheets.Count]);
xlWorkSheet.Name = "Sheet" + currentSheet.ToString();
}
xlWorkSheet.get_Range("C10", "E10").Value2 = salesman;
xlWorkSheet.get_Range("C12", "E12").Value2 = customer;
xlWorkSheet.get_Range("C12", "E12").Font.Bold = true;
xlWorkSheet.get_Range("C12", "E12").Font.Bold = true;
xlWorkSheet.get_Range("H12", "I12").Value2 = DateTime.Now;
xlWorkSheet.get_Range("H10", "I10").Value2 = quote;
for (int column = 0; column < dataGridView1.Columns.Count; column++)
{
if (column == 2 || column == 5)
{
xlWorkSheet.Cells[cell + rowsToStart, column + 1] = "'" + dataGridView1.Rows[row].Cells[column].Text.ToString();
}
else
{
xlWorkSheet.Cells[cell + rowsToStart, column + 1] = dataGridView1.Rows[row].Cells[column].Text.ToString();
}
}
int rowToDeleteFrom = rowsToStart + rowsToDisplay;
if ((row + 1) % rowsToDisplay == 0)
{
Excel.Range range = xlWorkSheet.get_Range("A" + rowToDeleteFrom, "A49");
Excel.Range entireRow = range.EntireRow;
entireRow.Delete(Excel.XlDirection.xlUp);
currentSheet++;
cell = 0;
}
else
{
cell++;
}
}
if (System.IO.File.Exists(destinationPath))
{
System.IO.File.Delete(destinationPath);
}
xlWorkBook.SaveAs(destinationPath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
}
It will delete the defined rows from the excel.