This bit for generating Excel file and saving it on the server side. You have to load the database data into the dataset first.
[ C# ]
DataSet DS = somevalueDataSet();
XmlDataDocument xdd = new XmlDataDocument(DS);
XslTransform xt = new XslTransform();
string workingdir = AppDomain.CurrentDomain.BaseDirectory + "/" + "xslSheet.xsl";
xt.Load(workingdir);
string dirPath = AppDomain.CurrentDomain.BaseDirectory + "/" + "GeneratedFR";
string filePath = AppDomain.CurrentDomain.BaseDirectory + "/" + "GeneratedFR" + "/" + "Test1.xls";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
File.Delete(filePath);
FileStream fs = new FileStream(filePath, FileMode.Create);
using(fs)
{
xt.Transform(xdd,null,fs);
fs.Close();
}
This bit for sending email. You can bypass your Outlook. It will attach a file from the server side. That can be your Excel file that you generated and saved on the server side.
[ C# ]
MailMessage mail = new MailMessage();
mail.To = "
[email protected]'';
mail.From = "
[email protected]";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body.";
MailAttachment attachment = new MailAttachment( Server.MapPath( "test.txt" ) ); //create the attachment
mail.Attachments.Add( attachment ); //add the attachment
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send( mail );
[ VB.NET ]
Dim mail As New MailMessage()
mail.To = "
[email protected]''
mail.From = "
[email protected]"
mail.Subject = "this is a test email."
mail.Body = "this is my test email body."
Dim attachment As New MailAttachment(Server.MapPath(''test.txt'')) 'create the attachment
mail.Attachments.Add(attachment) 'add the attachment
SmtpMail.SmtpServer = "localhost" 'your real server goes here
SmtpMail.Send(mail)