C# MVC使用rdlc报表打印预览导出world,pdf,excel

 最近有个项目用到了打印小票得功能,使用了C#自带得rdlc报表插件可以实现,以下是使用C# MVC写了一个案例;

1.创建一个空得mvc项目,这部分跳过;

2.新建一个文件夹rdlc,存放rdlc要用到得数据集和数据源;

3,先创建一个数据集,它类似一个数据公用池子,里面可以又多个表;

4.数据集池子创建完成后,点击数据集,可以在里面添加多张表,表名称可以自己取,只是填充得时候,从数据库返回得表名要和自己添加得表字段名称一样;

5.添加报表;

6.打开报表,添加数据绑定,点击数据集,右键会有,一般名称可以改成对应得,数据填充要和名称一直,不然数据不显示;

7.自己可以使用控件画图,如果出现中文乱码或者变成小框框,请设置字体位宋体或者其他字体;

以上工作完成后可以写代码了,以上是测试代码,数据都是临时造得,本次测试主要是2个数据集,一个主表(dt1),一个明细表(dt),代码就不一行行解释了哈;

前端加一个就可以直接调用这个方法:    @Html.ActionLink("打印", "ExportExcelprint")

 public FileResult ExportExcelprint()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Code", typeof(string));
            dt.Columns.Add("Price", typeof(string));
            for (int i = 0; i < 20; ++i)
            {
                DataRow row = dt.NewRow();
                row["Name"] = "左炔诺孕酮炔雌醚片(长效口服避孕药)";
                row["Code"] = "1";
                row["Price"] = "10.5";
                dt.Rows.Add(row);
            }
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("Num", typeof(string));
            dt1.Columns.Add("Time", typeof(string));
            DataRow row1 = dt1.NewRow();
            row1["Num"] = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            row1["Time"] = DateTime.Now.ToString("yyyy.MM.dd");
            dt1.Rows.Add(row1);
            LocalReport localReport = new LocalReport();
            localReport.ReportPath = Server.MapPath("~/rdlc/Report1.rdlc");
            ReportDataSource reportDataSource = new ReportDataSource("DataSet1", dt);
            ReportDataSource reportDataSource1 = new ReportDataSource("DataSet2", dt1);

            localReport.DataSources.Add(reportDataSource);
            localReport.DataSources.Add(reportDataSource1);
            //string reportType = "Word";
            string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension;
           
            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;
            StringBuilder deviceInfo = new StringBuilder();
            deviceInfo.AppendLine("<DeviceInfo>");
            deviceInfo.AppendLine("<OutputFormat>PDF</OutputFormat>");
            deviceInfo.AppendLine("<PageWidth>7cm</PageWidth>");
            deviceInfo.AppendFormat("<PageHeight>{0}cm</PageHeight>", dt.Rows.Count + 4);
            deviceInfo.AppendLine("<MarginTop>0.1cm</MarginTop>");
            deviceInfo.AppendLine("<MarginLeft>0.1cm</MarginLeft>");
            deviceInfo.AppendLine("<MarginRight>0.1cm</MarginRight>");
            deviceInfo.AppendLine("<MarginBottom>0.1cm</MarginBottom>");
            deviceInfo.AppendLine("</DeviceInfo>");
            //Render the report
            renderedBytes = localReport.Render(
                reportType,
                deviceInfo.ToString(),
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);
            //string path = Server.MapPath(@"\a.pdf");
            //FileStream fs = new FileStream(path, FileMode.Create);
            //fs.Write(renderedBytes, 0, renderedBytes.Length);
            //fs.Dispose();
            return File(renderedBytes, mimeType);
        }

 页面预览效果:

大家有看不懂得地方,随时留言,我看到会回复,加我可以点击这个网址:http://www.f12.fun

猜你喜欢

转载自www.cnblogs.com/f12-liugang/p/10307672.html