• .net core datatable 导出 pdf 支持中文


    1、nuget  安装 iTextSharp (V4.1.6) to .NET Core.

    2、code

     public DataTable ToDataTable<T>(IEnumerable<T> collection, Dictionary<string, string> columnMaps = null)
            {
                var props = typeof(T).GetProperties();
                var dt = new DataTable();
                dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
                if (collection.Count() > 0)
                {
                    for (int i = 0; i < collection.Count(); i++)
                    {
                        ArrayList tempList = new ArrayList();
                        foreach (PropertyInfo pi in props)
                        {
                            object obj = pi.GetValue(collection.ElementAt(i), null);
                            tempList.Add(obj);
                        }
                        object[] array = tempList.ToArray();
                        dt.LoadDataRow(array, true);
                    }
                }
    
                if (columnMaps != null)
                {
                    //修改列名
                    foreach (var item in columnMaps)
                    {
                        dt.Columns[item.Key].ColumnName = item.Value;
                    }
                }
    
                return dt;
            }
    
            public bool ConvertDataTableToPDF(DataTable Data, string PDFFile, float FontSize)
            {
                try
                {
                    GenepointLog.Info($"开始导出pdf--{PDFFile}");
                    //定义页面的大小
                    Rectangle pageSize = PageSize.A4;
                    Document document = new Document(pageSize);
                    //默认页面大小
                    //Document document = new Document();
                    var stream = new FileStream(PDFFile, FileMode.OpenOrCreate);
                    PdfWriter writer = PdfWriter.GetInstance(document, stream);
                    document.Open();
                    //设置字体
                    //BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1257, BaseFont.NOT_EMBEDDED);
                    BaseFont.AddToResourceSearch("iTextAsian.dll");
                    BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
                    BaseFont bf = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                    //BaseFont bf = BaseFont.CreateFont("STSong-Light,Italic", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                    Font font = new Font(bf, FontSize);
    
                    PdfPTable table = new PdfPTable(Data.Columns.Count);
                    table.WidthPercentage = 100; // percentage
                    table.DefaultCell.Padding = 1;
                    table.DefaultCell.BorderWidth = 0.2f;
                    table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
                    //将datatable表头转换成PDFTable的表头
                    foreach (DataColumn dc in Data.Columns)
                    {
                        table.AddCell(new Phrase(dc.ColumnName.ToString(), font));
                    }
                    //插入数据
                    for (int i = 0; i < Data.Rows.Count; i++)
                    {
                        for (int j = 0; j < Data.Columns.Count; j++)
                        {
                            table.AddCell(new Phrase(Data.Rows[i][j].ToString(), font));
                        }
                    }
                    document.Add(table);
                    document.Close();
                    writer.Close();
                    stream.Dispose();
                    GenepointLog.Info("结束导出pdf");
                    return true;
                }
                catch (Exception ex)
                {
                    GenepointLog.Error($"导出pdf失败{PDFFile}", ex);
                    return false;
                }
    
            }
  • 相关阅读:
    Java中常见时间类的使用
    springboot2.0介绍1
    Element-ui-Basic
    Java开发中的23中设计模式详解(一)工厂方法模式和抽象工厂模式
    CSS3 变形、过渡、动画、关联属性浅析
    Webpack 入门教程
    ES6对象简洁语法
    如何下载ts文件
    PPT转PDF
    python实践
  • 原文地址:https://www.cnblogs.com/mojiejushi/p/16081332.html
Copyright © 2020-2023  润新知