My data is stored in a data set from which I create an excel using a predefined excel template.
I want to send that excel as an attachement then.
This is my code which creates a new excel,however I want to use a predefined one..
public bool generateExcel(DataSet Report,string ReportExt)
{
try
{
bool success = false;
string FileName = "C:\\Documents and Settings\\Desktop\\Report_" + ReportExt + "_" + DateTime.Now.ToLongDateString() + ".xls";
Application ExcelApp = new Application();
Workbook ExcelWorkBook = null;
Worksheet ExcelWorkSheet = null;
ExcelApp.Visible = false;
ExcelWorkBook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
List<string> SheetNames = new List<string>();
SheetNames.Add("Work");
for (int i = 1; i < Report.Tables.Count; i++)
ExcelWorkBook.Worksheets.Add();
for (int i = 0; i < Report.Tables.Count; i++)
{
int r = 1;
ExcelWorkSheet = ExcelWorkBook.Worksheets[i + 1];
for (int col = 1; col < Report.Tables[i].Columns.Count; col++)
ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Columns[col - 1].ColumnName;
r++;
for (int row = 0; row < Report.Tables[i].Rows.Count; row++) {
for (int col = 1; col < Report.Tables[i].Columns.Count; col++)
ExcelWorkSheet.Cells[r, col] = Report.Tables[i].Rows[row][col - 1].ToString();
r++;
}
ExcelWorkSheet.Name = SheetNames[i];
}
ExcelWorkBook.SaveAs(FileName);
ExcelWorkBook.Close();
ExcelApp.Quit();
Marshal.ReleaseComObject(ExcelWorkSheet);
Marshal.ReleaseComObject(ExcelWorkBook);
Marshal.ReleaseComObject(ExcelApp);
daily.Close();
success = true;
return success;
}
catch(IOException IOex)
{
throw new ReportException(IOex.ToString());
}
catch (Exception ex)
{
throw new ReportException(ex.ToString());
}
}
I want to update the above code so that I use a predefined and then upload the data in it.
Next I want to send that excel in email as attachment...This is what my current code is..
public void SendEmail()
{
try
{
string mailbody = " Hi, ";
mailbody += "Reports have been generated in the folder below;";
mailbody += "C:\\Documents and Settings\\Desktop\\Reports\\";
System.Net.Mail.SmtpClient mailobj = new System.Net.Mail.SmtpClient();
System.Net.Mail.MailAddress MailFrom = new System.Net.Mail.MailAddress("test@yahoo.com", "test");
System.Net.Mail.MailAddress MailTo = new System.Net.Mail.MailAddress("test@yahoo.com", "test");
System.Net.Mail.MailMessage mailmsg = new System.Net.Mail.MailMessage(MailFrom, MailTo);
mailmsg.IsBodyHtml = true;
mailmsg.Subject = "Reports";
mailmsg.Body = mailbody;
mailobj.Host = "specified host here";
mailobj.Send(mailmsg);
}
catch (Exception ex)
{
throw new ReportException(ex.ToString());
}
}
Can anyone please help me with this,how should I update my code :)