gmsahmod says:
private
void
button1_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:\MyExcelsiorData\test2.xltx"
;
xlWorkBook = xlApp.Workbooks.Add(filePath);
string
datestr = DateTime.Now.ToShortDateString();
string
datestr1 = DateTime.Now.ToShortTimeString();
string
fileName =
@"C:\MyQuotes\Quote_"
+ txtName.Text.Replace(
" "
,
""
) +
" "
+ datestr.Replace(
'/'
,
'_'
)+
" "
+ datestr1.Replace(
':'
,
'_'
) +
".xlsx"
;
string
quote = txtName.Text.Replace(
" "
,
""
) +
" "
+ datestr.Replace(
'/'
,
'_'
) +
" "
+ datestr1.Replace(
':'
,
'_'
);
string
salesman = txtEmail.Text.ToUpper();
string
customer = txtName.Text.ToUpper();
string
contact = txtContact.Text.ToUpper();
string
address2 = txtDel2.Text.ToUpper();
string
address3 = txtDel3.Text.ToUpper();
string
note = txtNote.Text.ToUpper();
string
trans = comboBox1.SelectedItem.ToString();
int
currentSheet = 1;
int
cell = 0;
int
rowsToDisplay = 28;
int
rowsToStart = 20;
for
(
int
row = 0; row < dataGridView1.Rows.Count-1; 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(xlSheets[xlWorkBook.Sheets.Count], misValue, misValue, misValue);
xlWorkSheet.Name =
"Sheet"
+ currentSheet.ToString();
}
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;
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].Value.ToString();
}
else
{
xlWorkSheet.Cells[cell + rowsToStart, column + 1] = dataGridView1.Rows[row].Cells[column].Value.ToString();
}
}
if
((row + 1) % rowsToDisplay == 0)
{
xlWorkSheet.get_Range(
"F49"
,
"F49"
).Value2 =
"total"
;
xlWorkSheet.get_Range(
"G49"
,
"G49"
).Value2 =
"=SUM(G20:G48)/1.15"
;
currentSheet++;
cell = 0;
}
else
if
(row + 1 == dataGridView1.Rows.Count)
{
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)"
;
}
else
{
cell++;
}
}
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(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show(
"Excel file created , you can find the file c:\\MyQuotes"
+ fileName);
}
Replace with below code.