使用预定义的Excel模板创建excel,然后将excel作为附件邮寄。 [英] creating an excel using a predifined excel template and mailing the excel then as an attachment.

查看:161
本文介绍了使用预定义的Excel模板创建excel,然后将excel作为附件邮寄。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据存储在一个数据集中,我使用预定义的Excel模板从中创建一个excel。

我想将该excel作为附件发送。



这是我创建新excel的代码,但我想使用预定义的..



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(); //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());
            }
            

        }



我想更新上面的代码,以便我使用预定义然后上传其中的数据。



接下来我想把这封excel作为附件发送到电子邮件中...这就是我目前的代码..


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 :)

推荐答案

你确定这是C ++吗?



你只需要打开文件而不是创建一个新文件: br $> b $ b

MS Office OLE使用C ++进行自动化 [ ^ ]



什么不能使用电子邮件代码?
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 ?


这篇关于使用预定义的Excel模板创建excel,然后将excel作为附件邮寄。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆