0

我必须编写一个可以在数据库上运行 sql 查询的控制台应用程序。然后应用程序必须获取此信息并将其编译为报告,将此报告导出为 pdf,然后通过电子邮件发送 pdf 报告。(所有这一切都必须自动发生——我将使用 Windows 调度程序在特定日期和时间运行此应用程序。)

这是我到目前为止所拥有的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mail;

namespace SqlQueryReports
{
class Program
{
    static void Main(string[] args)
    {
        SqlConnection dataConnection = new SqlConnection();
        try
        {
            dataConnection.ConnectionString ="Data Source=MY-PC\\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True;Pooling=False";
            dataConnection.Open();

            SqlCommand dataCommand = new SqlCommand();
            dataCommand.Connection = dataConnection;

            dataCommand.CommandText = "SELECT Product_id,Product_name,Product_price FROM Product";
            Console.WriteLine("About to execute: {0}\n\n", dataCommand.CommandText);

            SqlDataReader dataReader = dataCommand.ExecuteReader();

            // Compile data into Report
            // Export Report to .pdf
            // Email .pdf report

            dataReader.Close();

            Console.WriteLine("DONE");
        }
        catch(SqlException e)
        {
            Console.WriteLine(e.Message);
        }

        finally
        {
            dataConnection.Close();
        }

    }       
}
}

我只需要知道如何:

  1. 使用此信息编制报告。
  2. 将此报告导出为 pdf
  3. 通过电子邮件发送 pdf 报告。

提前致谢!

4

2 回答 2

0

如果您想在用户友好的设计器中很好地设计报告,您可以使用 DevExpress 的 XtraReports 或任何其他第三方报告引擎,它们通常允许您绑定数据源并导出为 pdf(或 excel、html、png 等)在... )。

如果您想自己做所有事情,您可以使用表格(例如)格式化一种 HTML 文档,在该表格中您可以在 dataReader 字段和列中逐字地组成网格循环,那么您应该使用任何允许您创建 pdf 文档的组件最后,您可以使用 .NET Framework 的内置 Smtp Mailing 通过电子邮件发送 pdf。

于 2011-02-07T13:08:58.860 回答
0

真的希望您对此有更多的投入,因为我刚刚获得了完全相同的任务。到目前为止,这是我发现的可能有所帮助的内容;只是一个示例 PDF 导出(来源:ASP Snippets)...

protected void ExportToPDF(object sender, EventArgs e)
{
    //Get the data from database into datatable
    string strQuery = "select CustomerID, ContactName, City, PostalCode"     +
        " from customers";
    SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);

//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
    "attachment;filename=DataTable.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End(); 
于 2015-02-05T20:36:35.560 回答