0

我需要以编程方式创建的 EXCEL 附加到应用程序中的 SMTP MAIL ......文件到我的 smtp... 我不知道如何从该方法返回创建的 excel 文件...请任何人都可以帮助我吗?

这是将数据表转换为 EXCEL 的代码:

public static void ExportToSpreadsheet(DataTable table, string name)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in table.Columns)
{
 context.Response.Write(column.ColumnName + ";");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{
 for (int i = 0; i < table.Columns.Count; i++)
 {
  context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";");
 }
 context.Response.Write(Environment.NewLine);
 }
 context.Response.ContentType = "text/csv";
 context.Response.AppendHeader
 ("Content-Disposition", "attachment; filename=" + name +".csv");
  context.Response.End();
 }
4

1 回答 1

0

以下是一些可能有帮助的重构代码:

private static Stream DataTableToStream(DataTable table)
{
    const string semiColon = ";";

    var ms = new MemoryStream();
    var sw = new StreamWriter(ms);

    foreach (DataColumn column in table.Columns)
    {
        sw.Write(column.ColumnName);
        sw.Write(semiColon);
    }
    sw.Write(Environment.NewLine);
    foreach (DataRow row in table.Rows)
    {
        for (int i = 0; i < table.Columns.Count; i++)
        {
            sw.Write(row[i].ToString().Replace(semiColon, string.Empty));
            sw.Write(semiColon);
        }
        sw.Write(Environment.NewLine);
    }
    return ms;
}

private static MailMessage CreateMail(string from, 
    string to, 
    string subject, 
    string body, 
    string attname,  
    Stream tableStream)
{
    // using System.Net.Mail
    var mailMsg = new MailMessage(from, to, subject, body);

    tableStream.Position = 0;
    mailMsg.Attachments.Add(
        new Attachment(tableStream, attname, CsvContentType));
    return mailMsg;
}

private const string CsvContentType= "text/csv";

private static void ExportToSpreadsheetInternal(Stream tableStream, string name)
{

    HttpContext context = HttpContext.Current;
    context.Response.Clear();
    context.Response.ContentType = CsvContentType ;
    context.Response.AppendHeader(
        "Content-Disposition"
        , "attachment; filename=" + name + ".csv");

    tableStream.Position = 0;
    tableStream.CopyTo(context.Response.OutputStream);

    context.Response.End();

}

public static void ExportToSpreadsheet(DataTable table, string name)
{
    var stream = DataTableToStream(table);
    var mailMsg = CreateMail("from@example.com", 
        "to@example.com", 
        "spread", 
        "the spread", 
        name, 
        stream);
    ExportToSpreadsheetInternal(stream, name);
   // send the mailMsg with SmtpClient (config in your web.config)
   var smtp = new SmtpClient();
   smtp.Send(mailMsg);
}

将此添加到您的 web.config 中,假设您使用 gmail,将其替换userpwd您的...。

  <system.net>
    <mailSettings>
        <smtp deliveryMethod="network" from="you@example.com">
          <network 
            host="smtp.gmail.com" 
            port="587"
            enableSsl="true"
            userName="user" 
            password="pwd"/>
        </smtp>
    </mailSettings>
  </system.net>
于 2013-07-06T20:45:28.940 回答