C# wordexcel转PDF,读取json数据

Posted -王二毛-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# wordexcel转PDF,读取json数据相关的知识,希望对你有一定的参考价值。



使用json

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using MSWord = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
//using Microsoft.Office.Tools.Word;

namespace Console_WordSkill_All

    class Program
    
        static void Main(string[] args)
        
            
            string str = Readjson("filePath");
            Console.WriteLine("结果为0", str);

            string postfix = CheckTrueFileName(str);
            Console.WriteLine(postfix);

            switch (postfix)
            
                case ".doc":
                    WordToPDF(str, postfix);
                    break;
                case ".docx":
                    WordToPDF(str, postfix);
                    break;
                case ".xls":
                    ConverterToPdf(str, postfix);
                    break;
                case ".xlsx":
                    ConverterToPdf(str, postfix);
                    break;
            
            //WordToPDF(str);
        
        public static bool WordToPDF(string sourcePath, string postfix)
        
            bool result = false;
            MSWord.Application application = new MSWord.Application();
            MSWord.Document document = null;
            try
            
                application.Visible = false;
                document = application.Documents.Open(sourcePath);
                string PDFPath = sourcePath.Replace(postfix, ".pdf");//pdf存放位置
                Console.WriteLine(PDFPath);
                if (!File.Exists(@PDFPath))//存在PDF,不需要继续转换
                
                    document.ExportAsFixedFormat(PDFPath, MSWord.WdExportFormat.wdExportFormatPDF);
                
                result = true;
            
            catch (Exception e)
            
                Console.WriteLine(e.Message);
                result = false;
            
            finally
            
                document.Close();
            
            return result;
        

        private static bool ConverterToPdf(string sourcePath, string postfix)
        
            bool result = false;
            object missing = Type.Missing;
            Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
           // Microsoft.Office.Interop.Excel.ApplicationClass application = null;
            Microsoft.Office.Interop.Excel.Workbook workBook = null;
            try
            
                //application = new Microsoft.Office.Interop.Excel.ApplicationClass();
                object target =  sourcePath.Replace(postfix, ".pdf");//pdf存放位置

                Microsoft.Office.Interop.Excel.XlFixedFormatType type = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
                workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                        missing, missing, missing, missing, missing, missing, missing, missing, missing);
                将Excel中某个工作簿另存为PDF文件
                //Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets["工作簿名称或索引"];
                //worksheet.ExportAsFixedFormat(type, target, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);

                //将整个Excel另存为PDF文件
                workBook.ExportAsFixedFormat(type, target, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                result = true;
            
            catch (Exception ex)
            
                Console.WriteLine(ex.Message);
                result = false;
            
            finally
            

                if (workBook != null)
                
                    workBook.Close(false, missing, missing);
                    workBook = null;
                

                if (application != null)
                
                    application.Quit();
                    application = null;
                
                GC.Collect();

                GC.WaitForPendingFinalizers();
            

            return result;
        

        //读取json文件
        public static string Readjson(string key)
        
            string jsonfile = "C:/create.json";//JSON文件路径
            
            using (System.IO.StreamReader file = new StreamReader(jsonfile, Encoding.Default))
            
                using (JsonTextReader reader = new JsonTextReader(file))
                
                    JObject o = (JObject)JToken.ReadFrom(reader);
                    var value = o[key].ToString();
                    return value;
                
            
        

        //判断文件类型
        public static string CheckTrueFileName(string path)
        
            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
            string bx = " ";
            byte buffer;
            try
            
                buffer = r.ReadByte();
                bx = buffer.ToString();
                buffer = r.ReadByte();
                bx += buffer.ToString();
            
            catch (Exception exc)
            
                Console.WriteLine(exc.Message);
            
            r.Close();
            fs.Close();
            
            //真实的文件类型,数字
            //Console.WriteLine(bx);
            //文件名,包括后缀
            //Console.WriteLine(System.IO.Path.GetFileName(path));
            //文件名,不包括后缀
            //Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(path));
            //文件格式.doc
            //Console.WriteLine(System.IO.Path.GetExtension(path));

            return System.IO.Path.GetExtension(path);
        

    

以上是关于C# wordexcel转PDF,读取json数据的主要内容,如果未能解决你的问题,请参考以下文章

《html转pdf-----wkhtmltopdf踩坑总结》

[转]C# JSON格式的字符串读取到类中

如何用 Java 实现 wordexcel 等文档在线预览?

WinForm导出文件为WordExcel文本文件的方法(转)

纯C#,读取shp文件转json,arcgis api for js 3.x可加载

C#常用方法——通过WebServices接口读取json字符并解析示例