600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > C# word excel转PDF 读取json数据

C# word excel转PDF 读取json数据

时间:2022-07-03 09:05:20

相关推荐

C# word excel转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);}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。