樣本:
在這個示例中,我們使用的是微軟給我們提供的數據庫,也就是家喻戶曉的Northwind數據庫。要下載Microsoft的免費樣本Northwind數據庫,您需要訪問以下URL。下載Northwind數據庫在頁面上,您將找到下載按鈕,如以下屏幕截圖所示。

第2步:安裝Microsoft的免費樣本Northwind數據庫
一個安裝程序文件(.msi)將被下載。您可以將其保存在桌面上,因為下載完成后您需要執行它。文件下載完成后,您可以通過雙擊安裝文件或右鍵單擊然后單擊上下文菜單中的安裝選項來開始安裝。
安裝完成后,您可以在以下位置檢查已安裝的數據庫文件,您將在安裝文件夾中找到Northwind數據庫。
步驟3:使用Management Studio將Northwind MDF文件附加到SQL Server數據庫
現在,你需要啟動SQL Server Management Studio中,然后用鼠標右鍵單擊數據庫文件夾在對象資源管理器。在上下文菜單中,單擊Attach選項,如下所示
此選項將在SQL Server中打開文件瀏覽器,您需要導航並選擇NORTHWIND.MDF文件並按OK按鈕。
這就是你將看到數據庫現在可以在SQL Server中與其他數據庫一起使用。
如果因為數據庫版本問題或其他原因,附加不上,那你就用那幾個腳本文件。
安裝圖表:
這玩膩更新的還是比較快的,所以可用性還是比較大的。現在我們創建一個model(用於綁定圖標值)
public class OrderModel
{
public string ShipCity { get; set; } public int TotalOrders { get; set; } }
我們現在肯定是要去創建我們的控制器了,定義如下。
using System;
using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; using WebApplication1.Models; using System.Web.Helpers; namespace WebApplication1.Controllers { public class PdfController : Controller { public static PdfContext pdfcontextoBJ = new PdfContext(); // GET: Pdf public ActionResult Index() { byte[] bytes = PopulateChart(); ViewBag.ChartImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes, 0, bytes.Length); return View(); } [HttpPost] public FileResult Export() { byte[] bytes = PopulateChart(); ViewBag.ChartImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes, 0, bytes.Length); using (MemoryStream stream = new System.IO.MemoryStream()) { //Initialize the PDF document object. using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f)) { PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); //Add the Image file to the PDF document object. iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bytes); pdfDoc.Add(img); pdfDoc.Close(); //Download the PDF file. return File(stream.ToArray(), "application/pdf", "Chart.pdf"); } } } private static byte[] PopulateChart() { List<OrderModel> chartData = new List<OrderModel>(); //根據id統計的腳本 var objList = pdfcontextoBJ.Orders.GroupBy(u => u.ShipCity) .Select(s => new { TotalOrders = s.Key, count = s.Count() }).ToList().Take(5).ToList(); foreach (var item in objList) { chartData.Add(new OrderModel() { TotalOrders = item.count, ShipCity = item.TotalOrders }); } Chart chart = new Chart(width: 500, height: 500, theme: ChartTheme.Blue); chart.AddTitle("USA City Distribution"); chart.AddSeries("Default", chartType: "Pie", xValue: chartData, xField: "ShipCity", yValues: chartData, yFields: "TotalOrders"); return chart.GetBytes(format: "jpeg"); } } }
在view中定義如下:
@{
Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <img alt="Chart" src="@ViewBag.ChartImageUrl" style="height:300px; width:300px" /> <br /> @using (Html.BeginForm("Export", "Pdf", FormMethod.Post)) { <input type="submit" id="btnSubmit" value="Export" /> } </body> </html>
效果圖:

