Free.Spire.Office 发布bug解决

本地使用 nuget 下包 本地测试 word 转 pdf .doc 转 pdf 没有问题,发布到IIS 上测试程序报错
“无法找到 Spire.Xls.dll …” ,解决

在 Program.cs 文件Main 方法 添加 重新加载dll 文件地址

// An highlighted block
        public static void Main(string[] args)
        {
            #region 加载spire.office.dll 文件(服务器必须重新加载一遍,不然dll文件无法访问)
            Assembly entry = Assembly.GetEntryAssembly();
            string dir = Path.GetDirectoryName(entry.Location);//获取网站根目录,我把dll文件放在了根目录
            
            var filePath = Path.Combine(dir, "Spire.Presentation.dll");
            AssemblyLoadContext.Default.LoadFromAssemblyPath(filePath);
            var filePath2 = Path.Combine(dir, "Spire.Pdf.dll");
            AssemblyLoadContext.Default.LoadFromAssemblyPath(filePath2);
            var filePath3 = Path.Combine(dir, "Spire.Doc.dll");
            AssemblyLoadContext.Default.LoadFromAssemblyPath(filePath3);
            var filePath4 = Path.Combine(dir, "Spire.XLS.dll");
            AssemblyLoadContext.Default.LoadFromAssemblyPath(filePath4);
            var filePath5 = Path.Combine(dir, "Spire.License.dll");
            AssemblyLoadContext.Default.LoadFromAssemblyPath(filePath5);
            #endregion



            CreateHostBuilder(args).Build().Run();
        }

xls,word 转换方法 官网很详细,这里附上两个简单的

// An highlighted block
		// sourcePath 原文件地址
		// targetPath 目标文件地址
        public static string xlstoPDF(string type, string sourcePath, string targetPath)
        {
            try
            {

                Workbook workbook = new Workbook();


                //加载Excel工作簿
                workbook.LoadFromFile(sourcePath);

                workbook.ConverterSetting.SheetFitToPage = true;

                

                //将工作表保存为PDF 
                workbook.SaveToFile(targetPath, Spire.Xls.FileFormat.PDF);

                return "";

            }
            catch (Exception ex)
            {


                return ex.Message; 
            }

        }
	
		public static string doctoPDF(string type, string sourcePath, string targetPath)
        {
            try
            {

                Document doc = new Document();
                doc.LoadFromFile(sourcePath);

                ToPdfParameterList pdf = new ToPdfParameterList();

                //set DisableLink to false to preserve the hyperlinks
                pdf.DisableLink = true;

                doc.SaveToFile(targetPath, pdf);

                return "";

            }
            catch (Exception ex)
            {

                return ex.Message;
            }

        }

官网地址:https://www.e-iceblue.com/