解决使用aspose将excel转化成pdf时没有表格线,没有表格

解决方案:需要设置每个单元格的样式,然后遍历每个sheet中每个单元格,将该样式应用到每个单元格
以下是代码:

using Aspose.Cells;
using Aspose.Words;
using Aspose.Words.Fonts;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BorderType = Aspose.Cells.BorderType;

namespace manager.Controllers
{
    public class topdfController : ApiController
    {
    //这个是破解钥匙
        public const string Key =
            "PExpY2Vuc2U+DQogIDxEYXRhPg0KICAgIDxMaWNlbnNlZFRvPkFzcG9zZSBTY290bGFuZCB" +
            "UZWFtPC9MaWNlbnNlZFRvPg0KICAgIDxFbWFpbFRvPmJpbGx5Lmx1bmRpZUBhc3Bvc2UuY2" +
            "9tPC9FbWFpbFRvPg0KICAgIDxMaWNlbnNlVHlwZT5EZXZlbG9wZXIgT0VNPC9MaWNlbnNlV" +
            "HlwZT4NCiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1p" +
            "dGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+DQogICAgPE9yZGVySUQ+MTQ" +
            "wNDA4MDUyMzI0PC9PcmRlcklEPg0KICAgIDxVc2VySUQ+OTQyMzY8L1VzZXJJRD4NCiAgIC" +
            "A8T0VNPlRoaXMgaXMgYSByZWRpc3RyaWJ1dGFibGUgbGljZW5zZTwvT0VNPg0KICAgIDxQc" +
            "m9kdWN0cz4NCiAgICAgIDxQcm9kdWN0PkFzcG9zZS5Ub3RhbCBmb3IgLk5FVDwvUHJvZHVj" +
            "dD4NCiAgICA8L1Byb2R1Y3RzPg0KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl" +
            "0aW9uVHlwZT4NCiAgICA8U2VyaWFsTnVtYmVyPjlhNTk1NDdjLTQxZjAtNDI4Yi1iYTcyLT" +
            "djNDM2OGYxNTFkNzwvU2VyaWFsTnVtYmVyPg0KICAgIDxTdWJzY3JpcHRpb25FeHBpcnk+M" +
            "jAxNTEyMzE8L1N1YnNjcmlwdGlvbkV4cGlyeT4NCiAgICA8TGljZW5zZVZlcnNpb24+My4w" +
            "PC9MaWNlbnNlVmVyc2lvbj4NCiAgICA8TGljZW5zZUluc3RydWN0aW9ucz5odHRwOi8vd3d" +
            "3LmFzcG9zZS5jb20vY29ycG9yYXRlL3B1cmNoYXNlL2xpY2Vuc2UtaW5zdHJ1Y3Rpb25zLm" +
            "FzcHg8L0xpY2Vuc2VJbnN0cnVjdGlvbnM+DQogIDwvRGF0YT4NCiAgPFNpZ25hdHVyZT5GT" +
            "zNQSHNibGdEdDhGNTlzTVQxbDFhbXlpOXFrMlY2RThkUWtJUDdMZFRKU3hEaWJORUZ1MXpP" +
            "aW5RYnFGZkt2L3J1dHR2Y3hvUk9rYzF0VWUwRHRPNmNQMVpmNkowVmVtZ1NZOGkvTFpFQ1R" +
            "Hc3pScUpWUVJaME1vVm5CaHVQQUprNWVsaTdmaFZjRjhoV2QzRTRYUTNMemZtSkN1YWoyTk" +
            "V0ZVJpNUhyZmc9PC9TaWduYXR1cmU+DQo8L0xpY2Vuc2U+";
        #region excel转化成pdf
        [HttpGet]
        public string exceltopdf(string ss)
        {

            string path = System.Web.Hosting.HostingEnvironment.MapPath(ss);

            //return path;
            try
            {
                string inpdf = path;

                string outpdf = @"/filedata/1111.pdf";

                 new Aspose.Cells.License().SetLicense(new MemoryStream(Convert.FromBase64String(Key)));

                string outtopdf = System.Web.Hosting.HostingEnvironment.MapPath(outpdf);


                Aspose.Cells.Workbook excel = new Workbook(inpdf);

         

                WorksheetCollection worksheets = excel.Worksheets;      //获取sheet集合


                //风格的配置

                //Aspose.Cells.Style style = hg.GetStyle(); 
                Aspose.Cells.Style style = excel.Styles[excel.Styles.Add()];
                //Setting the line style of the top border
                style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;

                //Setting the color of the top border
                style.Borders[BorderType.TopBorder].Color = Color.Black;

                //Setting the line style of the bottom border
                style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

                //Setting the color of the bottom border
                style.Borders[BorderType.BottomBorder].Color = Color.Black;

                //Setting the line style of the left border
                style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;

                //Setting the color of the left border
                style.Borders[BorderType.LeftBorder].Color = Color.Black;

                //Setting the line style of the right border
                style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

                //Setting the color of the right border
                style.Borders[BorderType.RightBorder].Color = Color.Black;

                style.HorizontalAlignment = TextAlignmentType.Center;//文字居中

                for (int i = 0; i < worksheets.Count; i++)     //每一sheet
                {
                    //worksheets[i].Cells.SetColumnWidth(1, 30);//设置列宽
                    for (int j = 0; j < worksheets[i].Cells.Count; j++)  //sheet中的每一个单元格都采用这种风格
                    {
                        worksheets[i].Cells.SetRowHeight(j, 20);//设置行高

                        Cell hg = worksheets[i].Cells[j];
                        hg.SetStyle(style);

                    }

                }
                excel.Save(outtopdf, Aspose.Cells.SaveFormat.Pdf);


                return outpdf;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }
        #endregion
    }
}

解决使用aspose将excel转化成pdf时没有表格线,没有表格_第1张图片

如果其他转化的话请看我其他博客
https://blog.csdn.net/asdfghjkl110292/article/details/94551204

你可能感兴趣的:(笔记)