剑灵仙光珠,册子设计,草绳编织机
在这个示例中,我们使用的是微软给我们提供的数据库,也就是家喻户晓的northwind数据库。要下载microsoft的免费样本northwind数据库,您需要访问以下url。在页面上,您将找到下载按钮,如以下屏幕截图所示。
如果因为数据库版本问题或其他原因,附加不上,那你就用那几个脚本文件。
这玩腻更新的还是比较快的,所以可用性还是比较大的。现在我们创建一个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>
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论