1.利用NPOI生成EXCEL

2.导出excel之后发送邮件

//导出excel

//path :保存路径

//dt:要导出的数据 

public static bool DataTableToExcel(String path, DataTable dt)
{
  bool result = false;
  IWorkbook workbook = null;
  FileStream fs = null;
  IRow row = null;
  ISheet sheet = null;
  ICell cell = null;
  try
  {
    if (dt != null && dt.Rows.Count > 0)
    {
      workbook = new HSSFWorkbook();
      sheet = workbook.CreateSheet("停台信息");//创建一个名称为Sheet0的表
      int rowCount = dt.Rows.Count;//行数
      int columnCount = dt.Columns.Count;//列数

      //设置列头
      row = sheet.CreateRow(0);//excel第一行设为列头
      for (int c = 0; c < columnCount; c++)
      {
        cell = row.CreateCell(c);
        cell.SetCellValue(dt.Columns[c].ColumnName);
      }

      //设置每行每列的单元格,
      for (int i = 0; i < rowCount; i++)
      {
        row = sheet.CreateRow(i + 1);
        for (int j = 0; j < columnCount; j++)
        {
          cell = row.CreateCell(j);//excel第二行开始写入数据
          cell.SetCellValue(dt.Rows[i][j].ToString());
        }
      }
      using (fs = File.OpenWrite(path))
      {
        workbook.Write(fs);//向打开的这个xls文件中写入数据
        result = true;
      }
    }
  return result;

  }
  catch (Exception ex)
  {
    if (fs != null)
    {
      fs.Close();
    }
    return false;
  }
}

//发送邮件功能

/// <summary>
/// C#发送邮件函数
/// </summary>
/// <param name="from">发送者邮箱</param>
/// <param name="fromer">发送人</param>
/// <param name="to">接受者邮箱</param>
/// <param name="toer">收件人</param>
/// <param name="Subject">主题</param>
/// <param name="Body">内容</param>
/// <param name="file">附件</param>
/// <param name="SMTPHost">smtp服务器</param>
/// <param name="SMTPuser">邮箱</param>
/// <param name="SMTPpass">密码</param>

/// <returns></returns>
public bool sendmail(string sfrom, string sfromer, string sto, string stoer, string sSubject, string sBody, string sfile, string sSMTPHost, string sSMTPuser, string sSMTPpass)
{
////设置from和to地址
MailAddress from = new MailAddress(sfrom, sfromer);
MailAddress to = new MailAddress(sto, stoer);

////创建一个MailMessage对象
MailMessage oMail = new MailMessage(from, to);

//// 添加附件
if (sfile != "")
{
oMail.Attachments.Add(new Attachment(sfile));
}

////邮件标题
oMail.Subject = sSubject;

////邮件内容
oMail.Body = sBody;

////邮件格式
oMail.IsBodyHtml = false;

////邮件采用的编码
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");

////设置邮件的优先级为高
oMail.Priority = MailPriority.High;

////发送邮件
SmtpClient client = new SmtpClient();
////client.UseDefaultCredentials = false;
client.Host = sSMTPHost;
client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(oMail);
return true;
}
catch (Exception err)
{
return false;
}
finally
{
////释放资源
oMail.Dispose();
}

}

//发送邮件

public String SendEmail( )
{
String vResult = "";
//校验值
bool vFlag = true;
//Sql语句
String vSql = @"
Sql语句
";

String vDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
String vPath = "D:\\ExpExcel\\";
vPath += "停台信息 " + vDate + ".xls";
try
{

DataTable dt = new DataTable();
dt = GetExpData(vSql);

vFlag = DataTableToExcel(vPath, dt);
}
catch (Exception ex)
{
vFlag = false;
vResult = "执行失败!" + ex;
}

if (vFlag)
{

String vMDate = DateTime.Now.AddDays(-1).ToString("MM月dd日");

string from = "发送人邮箱";
string fromer = "A.Link";
string to = "接收人邮箱";
string toer = "陈工";
string Subject = "停台信息 " + vDate;\\标题
string file = vPath;\\附件所在路径
string Body = @"邮件内容";
string SMTPHost = "邮箱服务器地址";
string SMTPuser = "发送人账号";
string SMTPpass = "密码";
SendEmail se = new SendEmail();

se.sendmail(from, fromer, to, toer, Subject, Body, file, SMTPHost, SMTPuser, SMTPpass);
vResult = "执行成功!";
}
return vResult;

}

如果需要源码就给我留言,但是我不经常上,不会写博客

05-27 02:07