Hi gmsahmod,
Refer below code and modify as per your need to set value to specific cell.
public partial class Form5 : Form
{
public Form1(DataTable dt)
{
InitializeComponent();
this.dataGridView1.DataSource = dt;
}
private void Form5_Load(object sender, EventArgs e)
{
this.dataGridView1.Columns[0].Width = 100;
this.dataGridView1.Columns[1].Width = 150;
this.dataGridView1.Columns[2].Width = 58;
this.dataGridView1.Columns[3].Width = 60;
this.dataGridView1.Columns[4].Width = 58;
this.dataGridView1.Columns[5].Width = 50;
this.dataGridView1.Columns[5].DefaultCellStyle.Format = "R0.00";
this.dataGridView1.Columns[6].Width = 80;
this.dataGridView1.Columns[6].DefaultCellStyle.Format = "R0.00";
}
private void btnExportExcel_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
string filePath = @"C:\Users\developer4\Desktop\masterquote.xlsx";
xlWorkBook = xlApp.Workbooks.Add(filePath);
string datestr = DateTime.Now.ToShortDateString();
string datestr1 = DateTime.Now.ToShortTimeString();
string fileName = @"C:\Users\developer4\Desktop\Quote_" + datestr.Replace('/', '_') + " " + datestr1.Replace(':', '_') + ".xlsx";
string quote = datestr.Replace('/', '_') + " " + datestr1.Replace(':', '_');
string salesman = "test@gmail.com";
string customer = "test";
string contact = "test contact";
string address2 = "testAddress2".ToUpper();
string address3 = "testAddress3".ToUpper();
string note = "note".ToUpper();
string trans = "tran 1";
int rowsToDisplay = 28;
int rowsToStart = 20;
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.get_Range("E11", "E11").Value2 = salesman;
xlWorkSheet.get_Range("A12", "A12").Value2 = customer;
xlWorkSheet.get_Range("A13", "A13").Value2 = address2;
xlWorkSheet.get_Range("A14", "A14").Value2 = address3;
xlWorkSheet.get_Range("B11", "B11").Value2 = contact;
xlWorkSheet.get_Range("D12", "D12").Font.Bold = true;
xlWorkSheet.get_Range("B17", "B17").Value2 = DateTime.Now;
xlWorkSheet.get_Range("E17", "G17").Value2 = quote;
xlWorkSheet.get_Range("E13", "H13").Value2 = note;
xlWorkSheet.get_Range("C17", "C17").Value2 = trans;
int i = 0;
int j = 0;
for (int row = 0; row < dataGridView1.Rows.Count; row++)
{
if (row == 0)
{
i = rowsToStart;
}
else if ((row + 1) % rowsToDisplay == 0)
{
i++;
xlWorkSheet.get_Range("F" + i, "F" + i).Value2 = "total";
xlWorkSheet.get_Range("G" + i, "G" + i).Value2 = "=SUM(G" + rowsToStart + ":G" + (i - 1) + ")/1.15";
i++;
xlWorkSheet.get_Range("F" + i, "F" + i).Value2 = "Vat @15%";
xlWorkSheet.get_Range("G" + i, "G" + i).Value2 = "=SUM(F" + rowsToStart + ":F" + (i - 2) + ")+(SUM(F" + rowsToStart + ":F" + (i - 2) + ")*15%)";
i++;
xlWorkSheet.get_Range("F" + i, "F" + i).Value2 = "Total";
xlWorkSheet.get_Range("G" + i, "G" + i).Value2 = "=SUM(G" + (i - 1) + ":G" + (i - 2) + ")";
i++;
xlWorkSheet.get_Range("B" + i, "B" + i).Value2 = contact;
xlWorkSheet.get_Range("E" + i, "E" + i).Value2 = salesman;
i++;
xlWorkSheet.get_Range("A" + i, "A" + i).Value2 = customer;
xlWorkSheet.get_Range("D" + i, "D" + i).Font.Bold = true;
i++;
xlWorkSheet.get_Range("A" + i, "A" + i).Value2 = address2;
xlWorkSheet.get_Range("E" + i, "H" + i).Value2 = note;
i++;
xlWorkSheet.get_Range("A" + i, "A" + i).Value2 = address3;
i++;
xlWorkSheet.get_Range("B" + i, "B" + i).Value2 = DateTime.Now;
xlWorkSheet.get_Range("C" + i, "C" + i).Value2 = trans;
xlWorkSheet.get_Range("E" + i, "G" + i).Value2 = quote;
i = i + rowsToStart - 2;
j = i;
}
for (int column = 0; column < dataGridView1.Columns.Count; column++)
{
if (dataGridView1.Rows[row].Cells[column].Value == null)
{
xlWorkSheet.Cells[i, column + 1] = "";
}
else
{
xlWorkSheet.Cells[i, column + 1] = dataGridView1.Rows[row].Cells[column].Value.ToString();
}
}
i++;
}
if (dataGridView1.Rows.Count > rowsToDisplay)
{
xlWorkSheet.get_Range("F" + i, "F" + i).Value2 = "total";
xlWorkSheet.get_Range("G" + i, "G" + i).Value2 = "=SUM(G" + j + ":G" + (i - 1) + ")/1.15";
i++;
xlWorkSheet.get_Range("F" + i, "F" + i).Value2 = "Vat @15%";
xlWorkSheet.get_Range("G" + i, "G" + i).Value2 = "=SUM(F" + j + ":F" + (i - 2) + ")+(SUM(F" + j + ":F" + (i - 2) + ")*15%)";
i++;
xlWorkSheet.get_Range("F" + i, "F" + i).Value2 = "Total";
xlWorkSheet.get_Range("G" + i, "G" + i).Value2 = "=SUM(G" + (i - 1) + ":G" + (i - 2) + ")";
}
else
{
xlWorkSheet.get_Range("F49", "F49").Value2 = "total";
xlWorkSheet.get_Range("G49", "G49").Value2 = "=SUM(G20:G48)/1.15";
xlWorkSheet.get_Range("F50", "F50").Value2 = "Vat @15%";
xlWorkSheet.get_Range("G50", "G50").Value2 = "=SUM(F20:F48)+(SUM(F20:F48)*15%)";
xlWorkSheet.get_Range("F51", "F51").Value2 = "Total";
xlWorkSheet.get_Range("G51", "G51").Value2 = "=SUM(G49:G50)";
}
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}
xlWorkBook.SaveAs(fileName, Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
//releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file c:\\MyQuotes" + fileName);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}