Click here to Skip to main content
15,900,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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..

C++
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(); //Adding New sheet in Excel Workbook

                for (int i = 0; i < Report.Tables.Count; i++)
                {

                    int r = 1; // Initialize Excel Row Start Position  = 1

                    ExcelWorkSheet = ExcelWorkBook.Worksheets[i + 1];

                    //Writing Columns Name in Excel Sheet

                    for (int col = 1; col < Report.Tables[i].Columns.Count; col++)

                        ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Columns[col - 1].ColumnName;

                    r++;

                    //Writing Rows into Excel Sheet

                    for (int row = 0; row < Report.Tables[i].Rows.Count; row++) //r stands for ExcelRow and col for ExcelColumn
                    {

                        // Excel row and column start positions for writing Row=1 and Col=1

                        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];//Renaming the ExcelSheets

                }



                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..
C++
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 :)
Posted
Updated 21-Apr-14 21:06pm
v5
Comments
Sperihat 13-Apr-16 5:56am    
Here is a lot simpler approach how you can export a DataSet into an Excel file:

var ef = new ExcelFile();

foreach (DataTable table in Report.Tables)
ef.Worksheets.Add(table.TableName).InsertDataTable(table);

ef.Save(FileName);

The code uses this excel's library for C#.
Regarding the predefined excel, instead of using "new ExcelFile()" you should then use "ExcelFile.Load(path to your template workbook)".
Also note that you don't actually need to save your file on disk, you could save it into a stream and then attach that file's stream to your email, for example:

var options = new XlsSaveOptions();
var stream = new MemoryStream();
ef.Save(stream, options);

// ...

emailMessage.Attachments.Add(
new Attachment(options.ContentType, stream));

Note I'm using this email's library for C# to send an email with the attachment (you can find full sample here).

1 solution

Are you sure this is C++ ?

You simply need to open the file instead of creating a new one:

MS Office OLE Automation Using C++[^]

What is not working with email code ?
 
Share this answer
 
Comments
Member 10733096 22-Apr-14 3:09am    
Sorry, it is C#, I have updated that.
I have tried opening the file, everytime it gives me...some column 10 not found exception.
Related to email. I wanted to send th excel as attchement in it. how i can update that...
Member 10733096 22-Apr-14 3:12am    
sorry, yes it C#, I have updated that...
I have tried opening the file, but everytime it gives me some coulmn 10 not found exception.
Regarding the email, I wanted to send the excel generated as an attchemnt, how should I update that part..??..please help:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900