欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET 开源导入导出库Magicodes.IE 导出Pdf教程

程序员文章站 2022-06-11 12:16:48
基础教程之导出Pdf收据 说明 本教程主要说明如何使用Magicodes.IE.Pdf完成Pdf收据导出 要点 导出PDF数据 自定义PDF模板 导出单据 如何批量导出单据 导出特性 PdfExporterAttribute + Orientation : 方向(横向或纵向) + PaperKind ......

基础教程之导出pdf收据

说明

本教程主要说明如何使用magicodes.ie.pdf完成pdf收据导出

要点

  • 导出pdf数据
  • 自定义pdf模板
  • 导出单据
  • 如何批量导出单据

导出特性

pdfexporterattribute

  • orientation: 方向(横向或纵向)
  • paperkind: 纸张类型(默认a4)
  • iswritehtml: 是否输出html模板

exporterheaderattribute

displayname: 显示名称

主要步骤

1.安装包magicodes.ie.pdf

install-package magicodes.ie.pdf

2.导出pdf数据

  • 创建dto类
        public class student
        {
            /// <summary>
            ///     姓名
            /// </summary>
            public string name { get; set; }
            /// <summary>
            ///     年龄
            /// </summary>
            public int age { get; set; }
        }
       public async task exportpdf()
        {
            var exporter = new pdfexporter();
            var result = await exporter.exportlistbytemplate("test.pdf", new list<student>()
            {
                new student
                {
                    name = "mr.a",
                    age = 18
                },
                new student
                {
                    name = "mr.b",
                    age = 19
                },
                new student
                {
                    name = "mr.b",
                    age = 20
                }
            });
        }

导出内容如下所示:

ASP.NET 开源导入导出库Magicodes.IE 导出Pdf教程

通过上述代码我们实现了一个简单的pdf文件导出,也许这样无法达到我们的要求,我们需要自定义标题,那么请看如下代码

        [pdfexporter(name = "学生信息")]
        public class student
        {
            /// <summary>
            ///     姓名
            /// </summary>
            [exporterheader(displayname = "姓名")]
            [display(name = "display姓名")]
            public string name { get; set; }
            /// <summary>
            ///     年龄
            /// </summary>
            [exporterheader(displayname = "年龄")]
            public int age { get; set; }
        }
  1. pdfexporter 通过name属性来定义文档标题
  2. exporterheader displayname用来定义属性名称
  3. display同样定义属性名称,但是优先级小于exporterheader

通过修改上述代码执行结果如下所示:

ASP.NET 开源导入导出库Magicodes.IE 导出Pdf教程

3.导出pdf收据

  • 创建导出模板
@using documentformat.openxml.emma
<!doctype html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8"/>
    <title></title>
    <style type="text/css">
        body { font-family: simsun !important; }

        p { margin: 0px; }

        footer {
            color: #333;
            font-size: 1.2rem;
            margin-bottom: 1.5rem;
            margin-right: 5%;
            margin-top: 1.2rem;
            text-align: right;
        }

        table,
        td {
            border: 1px solid #444;
            border-collapse: collapse;
            /* text-align: center; */
            height: 2rem;
            padding: 5px;
        }
    </style>
</head>

<body>
<p style="color: #000; font-size: 1.8rem; height: 32px; text-align: center;">
    @model.title
</p>
<p style="color: #333; font-size: 1.2rem; margin-left: 5%; margin-top: 1%;">
    <text>no:@model.data.code</text>
    <text style="padding-left: 52%; text-align: right;">交易时间:@model.data.tradetime.tostring("yyyy-mm-dd hh:mm:ss")</text>
</p>

<table width="90%" style="margin-left: 5%; margin-top: 1%;">
    <tr style="font-size: 1rem;">
        <td style="text-align: center; width: 11%;">交款姓名</td>
        <td colspan="2">@model.data.name</td>
        <td style="text-align: center; width: 13%;">身份证号码</td>
        <td colspan="3">@model.data.idno</td>
    </tr>
    <tr style="font-size: 1rem;">
        <td style="text-align: center">交易金额</td>
        <td colspan="6">
            <span>¥:@model.data.amount</span>
            <span style="padding: 0 2% 0 2%;">人民币(大写):</span>
            <span>@model.data.uppercaseamount</span>
        </td>
    </tr>
    <tr style="font-size: 1rem;">
        <td style="text-align: center">收款方式</td>
        <td colspan="2">@model.data.paymentmethod</td>
        <td style="text-align: center; width: 13%;">交易状态</td>
        <td colspan="3">@model.data.tradestatus</td>
    </tr>

    <tr style="font-size: 1rem;">
        <td style="text-align: center">收款事由</td>
        <td style="width: 22.3%;">@model.data.remark</td>
        <td style="text-align: center; width: 11%;">入学年级</td>
        <td style="width: 22.3%;" colspan="2">@model.data.grade</td>
        <td style="text-align: center; width: 11%;">专业</td>
        <td style="width: 22.3%;">@model.data.profession</td>
    </tr>


</table>
</body>
</html>
  • 创建dto类

        [exporter(name = "湖南心莱信息科技有限公司电子收款凭证")]
        public class receiptinfo
        {
            /// <summary>
            ///     交易时间
            /// </summary>
            public datetime tradetime { get; set; }
    
            /// <summary>
            ///     姓名
            /// </summary>
            public string name { get; set; }
    
            /// <summary>
            ///     身份证
            /// </summary>
            public string idno { get; set; }
    
            /// <summary>
            ///     金额
            /// </summary>
            public decimal amount { get; set; }
    
            /// <summary>
            ///     支付方式
            /// </summary>
            public string paymentmethod { get; set; }
    
            /// <summary>
            ///     交易状态
            /// </summary>
            public string tradestatus { get; set; }
    
            /// <summary>
            ///     备注
            /// </summary>
            public string remark { get; set; }
    
            /// <summary>
            ///     年级
            /// </summary>
            public string grade { get; set; }
    
            /// <summary>
            ///     专业
            /// </summary>
            public string profession { get; set; }
    
            /// <summary>
            ///     收款人
            /// </summary>
            public string payee { get; set; }
    
            /// <summary>
            ///     大写金额
            /// </summary>
            public string uppercaseamount { get; set; }
    
            /// <summary>
            ///     编号
            /// </summary>
            public string code { get; set; }
        }
    1. 如上代码片段通过exporter 特性去指定模板中的title,当然在我们实际开发中也可以不通过该属性去做指定,毕竟我们这一块也是完全自定义的
  • 怎么使用?

            public async task exportreceipt()
            {
                var tplpath = path.combine(directory.getcurrentdirectory(), "testfiles", "exporttemplates",
                    "receipt.cshtml");
                var tpl = file.readalltext(tplpath);
                var exporter = new pdfexporter();
                //此处使用默认模板导出
                var result = await exporter.exportbytemplate("test.pdf",
                    new receiptinfo
                    {
                        amount = 22939.43m,
                        grade = "2019秋",
                        idno = "43062619890622xxxx",
                        name = "张三",
                        payee = "湖南心莱信息科技有限公司",
                        paymentmethod = "微信支付",
                        profession = "运动训练",
                        remark = "学费",
                        tradestatus = "已完成",
                        tradetime = datetime.now,
                        uppercaseamount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                        code = "19071800001"
                    }, tpl);
            }

    通过上述代码我们需要进行指定传递我们模板路径及模板内容,最终导出效果如下:

ASP.NET 开源导入导出库Magicodes.IE 导出Pdf教程

4.批量导出pdf收据

  • 创建模板

    <!doctype html>
    
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
        <meta charset="utf-8"/>
        <title></title>
        <style type="text/css">
            body { font-family: simsun !important; }
    
            p { margin: 0px; }
    
            footer {
                color: #333;
                font-size: 1.2rem;
                margin-bottom: 1.5rem;
                margin-right: 5%;
                margin-top: 1.2rem;
                text-align: right;
            }
    
            table,
            td {
                border: 1px solid #444;
                border-collapse: collapse;
                height: 2rem;
                padding: 5px;
            }
    
            .evennum {
                margin-bottom: 8%;
                padding-top: 10%;
            }
        </style>
    </head>
    
    <body>
    @for (var i = 0; i < model.data.receiptinfoinputs.count; i++)
    {
        <div style="height: 41%; width: 100%;" class="@(i % 2 == 0 ? "" : "evennum")">
            <p style="color: #000; font-size: 1.8rem; height: 32px; text-align: center;">
                @model.data.title
            </p>
            <p style="color: #333; font-size: 1.2rem; margin-left: 5%; margin-top: 1%;">
                <text>no:@model.data.receiptinfoinputs[i].code</text>
                <text style="padding-left: 52%; text-align: right;">交易时间:@model.data.receiptinfoinputs[i].tradetime.tostring("yyyy-mm-dd hh:mm:ss")</text>
            </p>
    
            <table width="90%" style="margin-left: 5%; margin-top: 1%;">
                <tr style="font-size: 1rem;">
                    <td style="text-align: center; width: 11%;">交款姓名</td>
                    <td colspan="2">@model.data.receiptinfoinputs[i].name</td>
                    <td style="text-align: center; width: 13%;">身份证号码</td>
                    <td colspan="3">@model.data.receiptinfoinputs[i].idno</td>
                </tr>
                <tr style="font-size: 1rem;">
                    <td style="text-align: center">交易金额</td>
                    <td colspan="6">
                        <span>¥:@model.data.receiptinfoinputs[i].amount</span>
                        <span style="padding: 0 2% 0 2%;">人民币(大写):</span>
                        <span>@model.data.receiptinfoinputs[i].uppercaseamount</span>
                    </td>
                </tr>
                <tr style="font-size: 1rem;">
                    <td style="text-align: center">收款方式</td>
                    <td colspan="2">@model.data.receiptinfoinputs[i].paymentmethod</td>
                    <td style="text-align: center; width: 13%;">交易状态</td>
                    <td colspan="3">@model.data.receiptinfoinputs[i].tradestatus</td>
                </tr>
    
                <tr style="font-size: 1rem;">
                    <td style="text-align: center">收款事由</td>
                    <td style="width: 22.3%;">@model.data.receiptinfoinputs[i].remark</td>
                    <td style="text-align: center; width: 11%;">入学年级</td>
                    <td style="width: 22.3%;" colspan="2">@model.data.receiptinfoinputs[i].grade</td>
                    <td style="text-align: center; width: 11%;">专业</td>
                    <td style="width: 22.3%;">@model.data.receiptinfoinputs[i].profession</td>
                </tr>
    
    
            </table>
        </div>
    }
    </body>
    </html>
  • 创建dto类

        /// <summary>
        ///     批量导出dto
        /// </summary>
        [pdfexporter(orientation = orientation.portrait, paperkind = paperkind.a5)]
        public class batchreceiptinfodto
        {
            /// <summary>
            ///     交易时间
            /// </summary>
            public datetime tradetime { get; set; }
    
            /// <summary>
            ///     姓名
            /// </summary>
            public string name { get; set; }
    
            /// <summary>
            ///     身份证
            /// </summary>
            public string idno { get; set; }
    
            /// <summary>
            ///     金额
            /// </summary>
            public decimal amount { get; set; }
    
            /// <summary>
            ///     支付方式
            /// </summary>
            public string paymentmethod { get; set; }
    
            /// <summary>
            ///     交易状态
            /// </summary>
            public string tradestatus { get; set; }
    
            /// <summary>
            ///     备注
            /// </summary>
            public string remark { get; set; }
    
            /// <summary>
            ///     年级
            /// </summary>
            public string grade { get; set; }
    
            /// <summary>
            ///     专业
            /// </summary>
            public string profession { get; set; }
    
            /// <summary>
            ///     大写金额
            /// </summary>
            public string uppercaseamount { get; set; }
    
            /// <summary>
            ///     编号
            /// </summary>
            public string code { get; set; }
        }
  • 如何使用

            public async task bathexportreceipt()
            {
                var tplpath = path.combine(directory.getcurrentdirectory(), "testfiles", "exporttemplates",
                    "batchreceipt.cshtml");
                var tpl = file.readalltext(tplpath);
                var exporter = new pdfexporter();
    
                var input = new batchreceiptinfoinput
                {
                    payee = "湖南心莱信息科技有限公司",
                    receiptinfoinputs = new list<batchreceiptinfodto>()
                };
    
                for (var i = 0; i < 20; i++)
                    input.receiptinfoinputs.add(new batchreceiptinfodto
                    {
                        amount = 22939.43m,
                        grade = "2019秋",
                        idno = "43062619890622xxxx",
                        name = "张三",
                        paymentmethod = "微信支付",
                        profession = "运动训练",
                        remark = "学费",
                        tradestatus = "已完成",
                        tradetime = datetime.now,
                        uppercaseamount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
                        code = "1907180000" + i
                    });
    
                //此处使用默认模板导出
                var result = await exporter.exportbytemplate("test.pdf", input, tpl);
            }

通过上述代码我们需要进行指定传递我们模板路径及模板内容,最终导出效果如下:

ASP.NET 开源导入导出库Magicodes.IE 导出Pdf教程

  1. pdfexporterattribute orientation进行方向如横向或纵向
  2. paperkind 纸张类型(默认a4)

reference

https://github.com/dotnetcore/magicodes.ie