CSharp: word,excel,powerpoint convert to pdf,hrml etc using Aspose.Office

news/2025/10/24 18:10:20/文章来源:https://www.cnblogs.com/geovindu/p/19163993

 

    /// <summary>/// 目标文件类型/// Aspose是商业软件需要购买/// </summary>public enum FileFormat{None,Pdf,Html,Text,Doc,Docx,Xls,Xlsx,CSV,PPT,PPTx,SVG,Webp,Rtf,Dwg,Psd}/// <summary>/// 文件转换/// geovindu,Geovin Du,涂聚文,塗聚文/// 底部有水印,是商业软件需要购买/// 有时间慢慢优化,不断完善/// 20251024/// </summary>public class AsposeOfficeHelper{/// <summary>/// 文件转换/// 其他文件类型自我优化/// /// </summary>/// <param name="inpFile">源文件</param>/// <param name="outFile">目标文件</param>/// <param name="fileFormat">目标转换文件类型</param>/// <returns></returns>public static bool DuConvert(string inpFile, string outFile, FileFormat fileFormat){int i = outFile.LastIndexOf("\\");int j = outFile.LastIndexOf(".");string FileName = outFile.Substring(i + 1, j - (i + 1));string ExtendName = outFile.Substring(j + 1);//考虑扩展名bool isNeedConvert = false;if (File.Exists(inpFile)){switch (fileFormat){case FileFormat.Pdf:isNeedConvert = ConvertPdf(inpFile, outFile);break;case FileFormat.Html:isNeedConvert = ConvertHtml(inpFile, outFile);break;case FileFormat.Text:isNeedConvert = ConvertText(inpFile, outFile);break;case FileFormat.Xls:isNeedConvert = ConvertXls(inpFile, outFile);break;case FileFormat.Xlsx:isNeedConvert = ConvertXlsx(inpFile, outFile);break;case FileFormat.CSV:isNeedConvert = ConvertCSV(inpFile, outFile);break;case FileFormat.Webp:isNeedConvert = ConvertWebp(inpFile, outFile);break;case FileFormat.Doc:isNeedConvert = ConvertDoc(inpFile, outFile);break;case FileFormat.Docx:isNeedConvert = ConvertDocx(inpFile, outFile);break;case FileFormat.SVG:isNeedConvert = ConvertSvg(inpFile, outFile);break;case FileFormat.Rtf:isNeedConvert = ConvertRtf(inpFile, outFile);break;default:isNeedConvert = false;break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPdf(string inpFile, string outFile){bool isNeedConvert = false;if (File.Exists(inpFile)){FileInfo fileInfo = new FileInfo(inpFile); //获上传源文件的扩展名switch (fileInfo.Extension.ToLower()){case ".doc":isNeedConvert = ConvertDocToPdf(inpFile, outFile);break;case ".docx":isNeedConvert = ConvertDocxToPdf(inpFile, outFile);break;case ".ppt":isNeedConvert = ConvertPPTToPdf(inpFile, outFile);break;case ".pptx":isNeedConvert = ConvertPPTxToPdf(inpFile, outFile);break;case ".xls":isNeedConvert = ConvertXlsToPdf(inpFile, outFile);break;case ".xlsx":isNeedConvert = ConvertXlsxToPdf(inpFile, outFile);break;case ".html":isNeedConvert = ConvertHtmlToPdf(inpFile, outFile);break;case ".htm":isNeedConvert = ConvertHtmlToPdf(inpFile, outFile);break;case ".txt":isNeedConvert = ConvertTextToPdf(inpFile, outFile);break;case ".jpg":isNeedConvert = ConvertJpgToPdf(inpFile, outFile);break;case ".png":isNeedConvert = ConvertPngToPdf(inpFile, outFile);break;case ".gif":isNeedConvert = ConvertGifToPdf(inpFile, outFile);break;case ".svg":isNeedConvert = ConvertSvgToPdf(inpFile, outFile);break;case ".rtf":isNeedConvert = ConvertRtfToPdf(inpFile, outFile);break;case ".psd":isNeedConvert = ConvertPsdToPdf(inpFile, outFile);break;case ".dwg":isNeedConvert = ConvertDwgToPdf(inpFile, outFile);break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertHtml(string inpFile, string outFile){bool isNeedConvert = false;if (File.Exists(inpFile)){FileInfo fileInfo = new FileInfo(inpFile); //获上传源文件的扩展名switch (fileInfo.Extension.ToLower()){case ".doc":isNeedConvert = ConvertDocToHtml(inpFile, outFile);break;case ".docx":isNeedConvert = ConvertDocxToHtml(inpFile, outFile);break;case ".ppt":isNeedConvert =  ConvertPPTToHtml(inpFile, outFile);break;case ".pptx":isNeedConvert = ConvertPPTxToHtml(inpFile, outFile);break;case ".xls":isNeedConvert = ConvertXlsToHtml(inpFile, outFile);break;case ".xlsx":isNeedConvert =ConvertXlsxToHtml(inpFile, outFile);break;case ".html":isNeedConvert = false;break;case ".htm":isNeedConvert = false;break;case ".pdf":isNeedConvert = ConvertPdfToHtml(inpFile,outFile);break;case ".rtf":isNeedConvert = ConvertRtfToHtml(inpFile, outFile);break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertText(string inpFile, string outFile){bool isNeedConvert = false;if (File.Exists(inpFile)){FileInfo fileInfo = new FileInfo(inpFile); //获上传源文件的扩展名switch (fileInfo.Extension.ToLower()){case ".doc":isNeedConvert = ConvertDocToText(inpFile, outFile);break;case ".docx":isNeedConvert = ConvertDocxToText(inpFile, outFile);break;case ".ppt":isNeedConvert = false;// ConvertPPTToText(inpFile, outFile);break;case ".pptx":isNeedConvert = false;// ConvertPPTXToText(inpFile, outFile);break;case ".xls":isNeedConvert = ConvertXlsToText(inpFile, outFile);break;case ".xlsx":isNeedConvert = ConvertXlsxToText(inpFile, outFile);break;case ".html":isNeedConvert = false;//ConvertHtmlToText(inpFile, outFile);break;case ".htm":isNeedConvert = false;//ConvertHtmlToText(inpFile, outFile);break;case ".pdf":isNeedConvert = ConvertPdfToText(inpFile, outFile);break;case ".rtf":isNeedConvert = false;//ConvertRtfToText(inpFile, outFile);break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDoc(string inpFile, string outFile){bool isNeedConvert = false;if (File.Exists(inpFile)){FileInfo fileInfo = new FileInfo(inpFile); //获上传源文件的扩展名switch (fileInfo.Extension.ToLower()){case ".txt":isNeedConvert = false;// ConvertTextToDoc(inpFile, outFile);break;case ".pdf":isNeedConvert = ConvertPdfToDoc(inpFile, outFile);break;case ".html":isNeedConvert = ConvertHtmlToDoc(inpFile, outFile);break;case ".Docx":isNeedConvert = ConvertDocxToDoc(inpFile, outFile);break;case ".rtf":isNeedConvert = false;// ConvertRtfToDoc(inpFile, outFile);break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDocx(string inpFile, string outFile){bool isNeedConvert = false;if (File.Exists(inpFile)){FileInfo fileInfo = new FileInfo(inpFile); //获上传源文件的扩展名switch (fileInfo.Extension.ToLower()){case ".doc":isNeedConvert = ConvertDocToDocx(inpFile, outFile);break;case ".xsl":isNeedConvert = ConvertXlsToDocx(inpFile, outFile);break;case ".xslx":isNeedConvert = ConvertXlsxToDocx(inpFile, outFile);break;case ".txt":isNeedConvert = false;// ConvertTextToDocx(inpFile, outFile);break;case ".pdf":isNeedConvert = ConvertPdfToDocx(inpFile, outFile);break;case ".html":isNeedConvert = ConvertHtmlToDocx(inpFile, outFile);break;case ".rtf":isNeedConvert = false;//ConvertRtfToDocx(inpFile, outFile);break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertCSV(string inpFile, string outFile){bool isNeedConvert = false;if (File.Exists(inpFile)){FileInfo fileInfo = new FileInfo(inpFile); //获上传源文件的扩展名switch (fileInfo.Extension.ToLower()){case ".xls":isNeedConvert = ConvertXlsToCsv(inpFile, outFile);break;case ".xlsx":isNeedConvert = ConvertXlsxToCsv(inpFile, outFile);break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXls(string inpFile, string outFile){bool isNeedConvert = false;if (File.Exists(inpFile)){FileInfo fileInfo = new FileInfo(inpFile); //获上传源文件的扩展名switch (fileInfo.Extension.ToLower()){case ".csv":isNeedConvert = ConvertCsvToXls(inpFile, outFile);break;case ".xlsx":isNeedConvert = ConvertXlsxToXls(inpFile, outFile);break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsx(string inpFile, string outFile){bool isNeedConvert = false;if (File.Exists(inpFile)){FileInfo fileInfo = new FileInfo(inpFile); //获上传源文件的扩展名switch (fileInfo.Extension.ToLower()){case ".xls":isNeedConvert = ConvertXlsToXlsx(inpFile, outFile);break;case ".csv":isNeedConvert =ConvertCsvToXlsx(inpFile, outFile);break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertRtf(string inpFile, string outFile){bool isNeedConvert = false;if (File.Exists(inpFile)){FileInfo fileInfo = new FileInfo(inpFile); //获上传源文件的扩展名switch (fileInfo.Extension.ToLower()){case ".doc":isNeedConvert = ConvertDocToRtf(inpFile, outFile);break;case ".docx":isNeedConvert = ConvertDocxToRtf(inpFile, outFile);break;case ".ppt":isNeedConvert = false;// ConvertPPTToRtf(inpFile, outFile);break;case ".pptx":isNeedConvert = false;//ConvertPPTxToRtf(inpFile, outFile);break;case ".xls":isNeedConvert = false;//ConvertXlsToRtf(inpFile, outFile);break;case ".xlsx":isNeedConvert = false;//ConvertXlsxToRtf(inpFile, outFile);break;case ".html":isNeedConvert = false;//ConvertHtmlToRtf(inpFile, outFile);break;case ".htm":isNeedConvert = false;//ConvertHtmlToRtf(inpFile, outFile);break;case ".pdf":isNeedConvert = false;// ConvertPdfToRtf(inpFile, outFile);break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertWebp(string inpFile, string outFile){bool isNeedConvert = false;if (File.Exists(inpFile)){FileInfo fileInfo = new FileInfo(inpFile); //获上传源文件的扩展名switch (fileInfo.Extension.ToLower()){case ".jpg":isNeedConvert = ConvertImageToWebp(inpFile, outFile);break;case ".png":isNeedConvert = ConvertImageToWebp(inpFile, outFile);break;case ".gif":isNeedConvert =  ConvertImageToWebp(inpFile, outFile);break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertSvg(string inpFile, string outFile){bool isNeedConvert = false;if (File.Exists(inpFile)){FileInfo fileInfo = new FileInfo(inpFile); //获上传源文件的扩展名switch (fileInfo.Extension.ToLower()){case ".jpg":isNeedConvert = ConvertImagedToSvg(inpFile, outFile);// ConvertJpgToSvg(inpFile, outFile);break;case ".png":isNeedConvert = ConvertPngToSvg(inpFile, outFile);break;case ".gif":isNeedConvert = ConvertImagedToSvg(inpFile, outFile);// ConvertGifToSvg(inpFile, outFile);break;}}return isNeedConvert;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertHtmlToPdf(string inpFile, string outFile){bool ok = false;try{// Load the HTML file into a document using HtmlLoadOptionsvar options = new Aspose.Pdf.HtmlLoadOptions();// Open HTML documentusing (var document = new Aspose.Pdf.Document(inpFile, options)){// Save PDF documentdocument.Save(outFile);}ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertHtmlToPdfTwo(string inpFile, string outFile){bool ok = false;try{// Load the HTML file into a document using HtmlLoadOptionsvar document = new Aspose.Html.HTMLDocument(inpFile);var options = new Aspose.Html.Saving.PdfSaveOptions();Aspose.Html.Converters.Converter.ConvertHTML(document, options, outFile);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertHtmlToDocx(string inpFile, string outFile){bool ok = false;try{// Load the HTML file into a document using HtmlLoadOptionsvar document = new Aspose.Html.HTMLDocument(inpFile);var options = new Aspose.Html.Saving.DocSaveOptions();Aspose.Html.Converters.Converter.ConvertHTML(document, options, outFile);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertHtmlToDoc(string inpFile, string outFile){bool ok = false;try{// Load the HTML file into a document using HtmlLoadOptionsvar document = new Aspose.Html.HTMLDocument(inpFile);var options = new Aspose.Html.Saving.DocSaveOptions();Aspose.Html.Converters.Converter.ConvertHTML(document, options, outFile);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertTextToPdf(string inpFile, string outFile){bool ok = false;try{TextReader tr = new StreamReader(inpFile);Aspose.Pdf.Document doc = new Aspose.Pdf.Document();Aspose.Pdf.Page page = doc.Pages.Add();TextFragment text = new TextFragment(tr.ReadToEnd());page.Paragraphs.Add(text);doc.Save(outFile);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// ConvertRtfToPdf/// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDocxToPdf(string inpFile,string outFile){bool ok = false;try{Aspose.Words.Document doc = new Aspose.Words.Document(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Words.SaveFormat.Pdf);ok = true;}catch(Exception ex){ Console.WriteLine(ex.Message.ToString()); }return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDocToDocx(string inpFile, string outFile){bool ok = false;try{Aspose.Words.Document doc = new Aspose.Words.Document(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Words.SaveFormat.Docx);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDocxToDoc(string inpFile, string outFile){bool ok = false;try{Aspose.Words.Document doc = new Aspose.Words.Document(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Words.SaveFormat.Doc);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertRtfToPdf(string inpFile, string outFile){bool ok = false;try{//多个文件//var fileNames = new List<string> { "Input1.rtf", "Input2.rtf" };//var output = new Document();//// 在追加之前从目标文档中删除所有内容。//output.RemoveAllChildren();//foreach (string fileName in fileNames)//{//    var input = new Document(fileName);//    // 将源文档附加到目标文档的末尾。//    output.AppendDocument(input, ImportFormatMode.KeepSourceFormatting);//}//output.Save("Output.pdf");var input = new Aspose.Words.Document(inpFile);input.Save(outFile);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertRtfToHtml(string inpFile, string outFile){bool ok = false;try{var input = new Aspose.Words.Document(inpFile);//Aspose.Words.Saving.HtmlSaveOptions options = new Aspose.Words.Saving.HtmlSaveOptions()//{//    HtmlVersion = HtmlVersion.XHtml,//    ExportXhtmlTransitional = true,//    PrettyFormat = true//};Aspose.Words.Saving.HtmlFixedSaveOptions options = new Aspose.Words.Saving.HtmlFixedSaveOptions(){ExportEmbeddedImages = true,ExportEmbeddedCss = true,PrettyFormat = true};input.Save(outFile);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDocToPdf(string inpFile, string outFile){bool ok = false;try{Aspose.Words.Document doc = new Aspose.Words.Document(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Words.SaveFormat.Pdf);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDocxToHtml(string inpFile, string outFile){bool ok = false;try{Aspose.Words.Document doc = new Aspose.Words.Document(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Words.SaveFormat.Html);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDocToHtml(string inpFile, string outFile){bool ok = false;try{Aspose.Words.Document doc = new Aspose.Words.Document(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Words.SaveFormat.Html);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDocToText(string inpFile, string outFile){bool ok = false;try{Aspose.Words.Document doc = new Aspose.Words.Document(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Words.SaveFormat.Text);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDocxToText(string inpFile, string outFile){bool ok = false;try{Aspose.Words.Document doc = new Aspose.Words.Document(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Words.SaveFormat.Text);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDocToRtf(string inpFile, string outFile){bool ok = false;try{Aspose.Words.Document doc = new Aspose.Words.Document(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Words.SaveFormat.Rtf);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDocxToRtf(string inpFile, string outFile){bool ok = false;try{Aspose.Words.Document doc = new Aspose.Words.Document(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Words.SaveFormat.Rtf);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsToPdf(string inpFile, string outFile){bool ok = false;try{Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Cells.SaveFormat.Pdf);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsToHtml(string inpFile, string outFile){bool ok = false;try{Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Cells.SaveFormat.Html);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsToDocx(string inpFile, string outFile){bool ok = false;try{Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Cells.SaveFormat.Docx);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsxToDocx(string inpFile, string outFile){bool ok = false;try{Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Cells.SaveFormat.Docx);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsToJson(string inpFile, string outFile){bool ok = false;try{Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Cells.SaveFormat.Json);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsToText(string inpFile, string outFile){bool ok = false;try{//Load your source workbookAspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(inpFile);//0-byte arraybyte[] workbookData = new byte[0];//Text save options. You can use any type of separatorAspose.Cells.TxtSaveOptions opts = new Aspose.Cells.TxtSaveOptions();opts.Separator = '\t';//Copy each worksheet data in text format inside workbook data arrayfor (int idx = 0; idx < workbook.Worksheets.Count; idx++){//Save the active worksheet into text formatMemoryStream ms = new MemoryStream();workbook.Worksheets.ActiveSheetIndex = idx;workbook.Save(ms, opts);//Save the worksheet data into sheet data arrayms.Position = 0;byte[] sheetData = ms.ToArray();//Combine this worksheet data into workbook data arraybyte[] combinedArray = new byte[workbookData.Length + sheetData.Length];Array.Copy(workbookData, 0, combinedArray, 0, workbookData.Length);Array.Copy(sheetData, 0, combinedArray, workbookData.Length, sheetData.Length);workbookData = combinedArray;}//Save entire workbook data into fileFile.WriteAllBytes(outFile, workbookData);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsxToText(string inpFile, string outFile){bool ok = false;try{//Load your source workbookAspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(inpFile);//0-byte arraybyte[] workbookData = new byte[0];//Text save options. You can use any type of separatorAspose.Cells.TxtSaveOptions opts = new Aspose.Cells.TxtSaveOptions();opts.Separator = '\t';//Copy each worksheet data in text format inside workbook data arrayfor (int idx = 0; idx < workbook.Worksheets.Count; idx++){//Save the active worksheet into text formatMemoryStream ms = new MemoryStream();workbook.Worksheets.ActiveSheetIndex = idx;workbook.Save(ms, opts);//Save the worksheet data into sheet data arrayms.Position = 0;byte[] sheetData = ms.ToArray();//Combine this worksheet data into workbook data arraybyte[] combinedArray = new byte[workbookData.Length + sheetData.Length];Array.Copy(workbookData, 0, combinedArray, 0, workbookData.Length);Array.Copy(sheetData, 0, combinedArray, workbookData.Length, sheetData.Length);workbookData = combinedArray;}//Save entire workbook data into fileFile.WriteAllBytes(outFile, workbookData);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsxToPdf(string inpFile, string outFile){bool ok = false;try{Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Cells.SaveFormat.Pdf);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// ConvertXlsxToXls/// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsToXlsx(string inpFile, string outFile){bool ok = false;try{Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Cells.SaveFormat.Xlsx);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsxToHtml(string inpFile, string outFile){bool ok = false;try{Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Cells.SaveFormat.Html);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsToCsv(string inpFile, string outFile){bool ok = false;try{Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Cells.SaveFormat.Csv);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsxToCsv(string inpFile, string outFile){bool ok = false;try{Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Cells.SaveFormat.Csv);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertXlsxToXls(string inpFile, string outFile){bool ok = false;try{Aspose.Cells.Workbook doc = new Aspose.Cells.Workbook(inpFile);// Save the document in HTML format.doc.Save(outFile);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPPTToPdf(string inpFile, string outFile){bool ok = false;try{Aspose.Slides.Presentation doc = new Aspose.Slides.Presentation(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Slides.Export.SaveFormat.Pdf);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPPTToHtml(string inpFile, string outFile){bool ok = false;try{Aspose.Slides.Presentation doc = new Aspose.Slides.Presentation(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Slides.Export.SaveFormat.Html5);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPPTToJpg(string inpFile, string outFile){bool ok = false;try{using (Aspose.Slides.Presentation presentation = new Aspose.Slides.Presentation(inpFile)){for (int i = 0; i < presentation.Slides.Count; i++){// Generate thumbnail for each slideusing (var bitmap = presentation.Slides[i].GetThumbnail(1f, 1f)){// Save each slide as a separate imagebitmap.Save($"Slide_{i + 1}.jpg"); //, System.Drawing.Imaging.ImageFormat.Jpeg}}}ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPPTxToPdf(string inpFile, string outFile){bool ok = false;try{Aspose.Slides.Presentation doc = new Aspose.Slides.Presentation(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Slides.Export.SaveFormat.Pdf);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPPTxToHtml(string inpFile, string outFile){bool ok = false;try{Aspose.Slides.Presentation doc = new Aspose.Slides.Presentation(inpFile);// Save the document in HTML format.doc.Save(outFile, Aspose.Slides.Export.SaveFormat.Html5);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPdfToHtml(string inpFile, string outFile){bool ok = false;try{// Load the source PDF documentAspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(inpFile);// Save the PDF as a Word document (DOCX format)pdfDocument.Save(outFile, Aspose.Pdf.SaveFormat.Html);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPdfToText(string inpFile, string outFile){bool ok = false;try{//string[] pdfFiles = { "sample1.pdf", "sample2.pdf" };//var extractor = new TextExtractor();//var options = new TextExtractorOptions(TextExtractorOptions.TextFormattingMode.Raw);//foreach (var file in pdfFiles)//{//    options.AddInput(new FileDataSource(file));//}//var resultContainer = extractor.Process(options);//for (int i = 0; i < resultContainer.ResultCollection.Count; i++)//{//    string text = resultContainer.ResultCollection[i].ToString();//    Console.WriteLine(text);//}//2//var extractor = new TextExtractor();//var options = new TextExtractorOptions(TextExtractorOptions.TextFormattingMode.Pure);//options.AddInput(new FileDataSource(inpFile));//var resultContainer = extractor.Process(options);//string extractedText = resultContainer.ResultCollection[0].ToString();           //// Save the extracted text to a file//File.WriteAllText(outFile, extractedText);//3// Load the PDF documentAspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(inpFile);// StringBuilder to hold the extracted textStringBuilder builder = new StringBuilder();// Loop through all the pages in the PDFforeach (Aspose.Pdf.Page pdfPage in pdfDocument.Pages){// Process each page for text extractionusing (MemoryStream textStream = new MemoryStream()){// Create a text device for text extractionTextDevice textDevice = new TextDevice();// Set text extraction options to 'Pure' modeTextExtractionOptions textExtOptions = new TextExtractionOptions(TextExtractionOptions.TextFormattingMode.Pure);textDevice.ExtractionOptions = textExtOptions;// Extract text from the current page and save it to the memory streamtextDevice.Process(pdfPage, textStream);// Convert memory stream to textstring extractedText = Encoding.Unicode.GetString(textStream.ToArray());// Append the extracted text to the StringBuilderbuilder.Append(extractedText);}}// Save the extracted text to a fileFile.WriteAllText(outFile, builder.ToString());ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPdfToDoc(string inpFile, string outFile){bool ok = false;try{// Load the source PDF documentAspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(inpFile);// Save the PDF as a Word document (DOCX format)pdfDocument.Save(outFile, Aspose.Pdf.SaveFormat.Doc);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPdfToDocx(string inpFile, string outFile){bool ok = false;try{// Load the source PDF documentAspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(inpFile);// Save the PDF as a Word document (DOCX format)pdfDocument.Save(outFile, Aspose.Pdf.SaveFormat.DocX);ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertJpgToPdf(string inpFile, string outFile){bool ok = false;try{// Create PDF documentusing (var document = new Aspose.Pdf.Document()){// Add pagevar page = document.Pages.Add();var image = new Aspose.Pdf.Image();// Load JPEG fileimage.File = inpFile;// Read Height of input imagepage.PageInfo.Height = image.BitmapSize.Height;// Read Width of input imagepage.PageInfo.Width = image.BitmapSize.Width;page.PageInfo.Margin.Bottom = 0;page.PageInfo.Margin.Top = 0;page.PageInfo.Margin.Right = 0;page.PageInfo.Margin.Left = 0;page.Paragraphs.Add(image);// Save PDF documentdocument.Save(outFile);}ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertGifToPdf(string inpFile, string outFile){bool ok = false;try{// Create PDF documentusing (var document = new Aspose.Pdf.Document()){// Add pagevar page = document.Pages.Add();var image = new Aspose.Pdf.Image();// Load sample GIF image fileimage.File = inpFile;page.Paragraphs.Add(image);// Save PDF documentdocument.Save(outFile);}ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPngToPdf(string inpFile, string outFile){bool ok = false;try{// Create PDF documentusing (var document = new Aspose.Pdf.Document()){// Add pagevar page = document.Pages.Add();var image = new Aspose.Pdf.Image();// Load PNG fileimage.File = inpFile;// Read Height of input imagepage.PageInfo.Height = image.BitmapSize.Height;// Read Width of input imagepage.PageInfo.Width = image.BitmapSize.Width;page.PageInfo.Margin.Bottom = 0;page.PageInfo.Margin.Top = 0;page.PageInfo.Margin.Right = 0;page.PageInfo.Margin.Left = 0;page.Paragraphs.Add(image);// Save PDF documentdocument.Save(outFile);}ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertPsdToPdf(string inpFile, string outFile){bool ok = false;try{var loadOptions = new PsdLoadOptions() { LoadEffectsResource = true };PsdImage image = (PsdImage)Aspose.PSD.Image.Load(inpFile, loadOptions);var pdfOptions = new Aspose.PSD.ImageOptions.PdfOptions();// Customize pdfOptions as needed (compression, encryption, etc.)image.Save(outFile, pdfOptions);image.Dispose();ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertDwgToPdf(string inpFile, string outFile){bool ok = false;try{// Load a CAD drawing in an instance of CadImageusing (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(inpFile)){// Create an instance of PdfOptionsAspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();// Create an instance of CadRasterizationOptions and set its various propertiesCadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();cadRasterizationOptions.PageWidth = 1600;cadRasterizationOptions.PageHeight = 1600;// cadRasterizationOptions.CenterDrawing = true;//cadRasterizationOptions.ScaleMethod = ScaleType.Center;// Set the VectorRasterizationOptions propertypdfOptions.VectorRasterizationOptions = cadRasterizationOptions;// Export the CAD drawing to PDF formatcadImage.Save(outFile, pdfOptions);}}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertImageToWebp(string inpFile, string outFile){bool ok = false;try{// Load the image using ImageSharpusing (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(inpFile)){// Save the image in WebP formatimage.Save(outFile, new WebpEncoder());ok = true;}}catch (Exception ex){ex.Message.ToString();}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertImageToWebpTwo(string inpFile, string outFile){bool ok = false;try{// Load the image using Aspose.Imagingusing (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(inpFile)){// Save the image in WebP formatimage.Save(outFile, new Aspose.Imaging.ImageOptions.WebPOptions());ok = true;}}catch (Exception ex){ex.Message.ToString();}return ok;}/// <summary>/// /// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertSvgToPdf(string inpFile, string outFile){bool ok = false;try{// 加载 SVG 文件var document = new SVGDocument("Sample.svg");// 配置保存选项var options = new Aspose.Svg.Saving.PdfSaveOptions();// 转换并保存为 PDF 文件Aspose.Svg.Converters.Converter.ConvertSVG(document, options, "Result.pdf");ok = true;}catch (Exception ex){Console.WriteLine(ex.Message.ToString());}return ok;}/// <summary>/// /// </summary>/// <param name="fileOriginPath"></param>/// <param name="fileSavePath"></param>/// <returns></returns>static bool ConvertPngToSvg(string fileOriginPath, string fileSavePath){bool ok = true;try{using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(fileOriginPath)){Aspose.Imaging.ImageOptions.SvgOptions svgOptions = new Aspose.Imaging.ImageOptions.SvgOptions();Aspose.Imaging.ImageOptions.SvgRasterizationOptions svgRasterizationOptions = new Aspose.Imaging.ImageOptions.SvgRasterizationOptions{PageWidth = image.Width,PageHeight = image.Height};svgOptions.VectorRasterizationOptions = svgRasterizationOptions;image.Save(fileSavePath, svgOptions);ok = true;}}catch (Exception ex){ex.Message.ToString();}return ok;}/// <summary>/// /// </summary>/// <param name="fileOriginPath"></param>/// <param name="fileSavePath"></param>/// <returns></returns>static bool ConvertImagedToSvg(string fileOriginPath, string fileSavePath){bool ok = true;try{using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(fileOriginPath)){Aspose.Imaging.ImageOptions.SvgOptions svgOptions = new Aspose.Imaging.ImageOptions.SvgOptions();Aspose.Imaging.ImageOptions.SvgRasterizationOptions svgRasterizationOptions = new Aspose.Imaging.ImageOptions.SvgRasterizationOptions{PageWidth = image.Width,PageHeight = image.Height};svgOptions.VectorRasterizationOptions = svgRasterizationOptions;image.Save(fileSavePath, svgOptions);ok = true;}}catch (Exception ex){ex.Message.ToString();}return ok;}/// <summary>/// /// </summary>/// <param name="csvFilePath"></param>/// <param name="xlsxFilePath"></param>/// <returns></returns>public static bool ConvertCsvToXlsx(string inpFile, string outFile){bool ok = false;try{// Read CSV linesvar lines = File.ReadAllLines(inpFile);// Create Excel documentusing (SpreadsheetDocument document = SpreadsheetDocument.Create(outFile, SpreadsheetDocumentType.Workbook)){WorkbookPart workbookPart = document.AddWorkbookPart();workbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();worksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(new SheetData());Sheets sheets = document.WorkbookPart.Workbook.AppendChild(new Sheets());Sheet sheet = new Sheet(){Id = document.WorkbookPart.GetIdOfPart(worksheetPart),SheetId = 1,Name = "Sheet1"};sheets.Append(sheet);SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();// Populate data from CSVforeach (var line in lines){DocumentFormat.OpenXml.Spreadsheet.Row row = new DocumentFormat.OpenXml.Spreadsheet.Row();var cells = line.Split(',');foreach (var cellValue in cells){DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell(){DataType = CellValues.String,CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(cellValue)};row.Append(cell);}sheetData.Append(row);}workbookPart.Workbook.Save();ok = true;}}catch (Exception ex){ex.Message.ToString();}return ok;}/// <summary>////// </summary>/// <param name="inpFile"></param>/// <param name="outFile"></param>/// <returns></returns>static bool ConvertCsvToXls(string inpFile, string outFile){bool ok = false;Encoding encoding = Encoding.UTF8;char delimiter = ',';try{DataTable dataTable = ReadCsv(inpFile, encoding, delimiter);ok = ConvertDataTableToXls(dataTable, outFile);}catch (Exception ex){ex.Message.ToString();}return ok;}/// <summary>/// Reads a CSV file and returns its contents as a DataTable/// </summary>/// <param name="filePath"></param>/// <param name="encoding"></param>/// <param name="delimiter"></param>/// <returns></returns>/// <exception cref="Exception"></exception>static DataTable ReadCsv(string filePath, Encoding encoding, char delimiter){try{using (var reader = new StreamReader(filePath, encoding))using (var csv = new CsvReader(reader, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture){Delimiter = delimiter.ToString(),HasHeaderRecord = true,IgnoreBlankLines = true,TrimOptions = CsvHelper.Configuration.TrimOptions.Trim})){using (var dr = new CsvDataReader(csv)){var dt = new DataTable();dt.Load(dr);return dt;}}}catch (Exception ex){throw new Exception($"Failed to read CSV file: {ex.Message}", ex);}}/// <summary>////// </summary>/// <param name="dataTable"></param>/// <param name="outputPath"></param>/// <returns></returns>/// <exception cref="Exception"></exception>static bool ConvertDataTableToXls(DataTable dataTable, string outputPath){bool ok = false;try{// Create a new spreadsheet documentusing (SpreadsheetDocument document = SpreadsheetDocument.Create(outputPath, SpreadsheetDocumentType.Workbook)){// Add a WorkbookPart to the documentWorkbookPart workbookPart = document.AddWorkbookPart();workbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();// Add a WorksheetPart to the WorkbookPartWorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();worksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(new SheetData());// Add Sheets to the WorkbookSheets sheets = workbookPart.Workbook.AppendChild(new Sheets());// Append a new worksheet and associate it with the workbookSheet sheet = new Sheet(){Id = workbookPart.GetIdOfPart(worksheetPart),SheetId = 1,Name = "Sheet1"};sheets.Append(sheet);// Get the SheetData objectSheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();// Add header rowDocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();foreach (DataColumn column in dataTable.Columns){DocumentFormat.OpenXml.Spreadsheet.Cell cell = CreateCell(column.ColumnName, CellValues.String);headerRow.AppendChild(cell);}sheetData.AppendChild(headerRow);// Add data rowsforeach (DataRow row in dataTable.Rows){DocumentFormat.OpenXml.Spreadsheet.Row dataRow = new DocumentFormat.OpenXml.Spreadsheet.Row();foreach (var item in row.ItemArray){CellValues cellType = GetCellValueType(item);string cellValue = GetCellValueAsString(item, cellType);DocumentFormat.OpenXml.Spreadsheet.Cell cell = CreateCell(cellValue, cellType);dataRow.AppendChild(cell);}sheetData.AppendChild(dataRow);}// Save the workbookworkbookPart.Workbook.Save();ok = true;}}catch (Exception ex){throw new Exception($"Failed to create XLS file: {ex.Message}", ex);}return ok;}/// <summary>/// Creates a new Excel cell with the specified value and type/// </summary>/// <param name="value"></param>/// <param name="cellType"></param>/// <returns></returns>private static DocumentFormat.OpenXml.Spreadsheet.Cell CreateCell(string value, CellValues cellType){DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();cell.DataType = new EnumValue<CellValues>(cellType);cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(value);return cell;}/// <summary>///  Determines the cell value type based on the object type/// </summary>/// <param name="value"></param>/// <returns></returns>private static DocumentFormat.OpenXml.Spreadsheet.CellValues GetCellValueType(object value){if (value == DBNull.Value)return DocumentFormat.OpenXml.Spreadsheet.CellValues.String;Type type = value.GetType();if (type == typeof(int) || type == typeof(long) || type == typeof(short) || type == typeof(byte))return DocumentFormat.OpenXml.Spreadsheet.CellValues.Number;else if (type == typeof(float) || type == typeof(double) || type == typeof(decimal))return DocumentFormat.OpenXml.Spreadsheet.CellValues.Number;else if (type == typeof(DateTime))return DocumentFormat.OpenXml.Spreadsheet.CellValues.Date;else if (type == typeof(bool))return DocumentFormat.OpenXml.Spreadsheet.CellValues.Boolean;elsereturn DocumentFormat.OpenXml.Spreadsheet.CellValues.String;}/// <summary>////// </summary>/// <param name="value"></param>/// <param name="cellType"></param>/// <returns></returns>private static string GetCellValueAsString(object value, DocumentFormat.OpenXml.Spreadsheet.CellValues cellType){if (value == DBNull.Value || value == null)return string.Empty;if (cellType == DocumentFormat.OpenXml.Spreadsheet.CellValues.Boolean){return (value is bool boolVal && boolVal) ? "1" : "0";}else if (cellType == DocumentFormat.OpenXml.Spreadsheet.CellValues.Number){if (value is DateTime dateVal){return dateVal.ToOADate().ToString(CultureInfo.InvariantCulture);}return Convert.ToString(value, CultureInfo.InvariantCulture);}else if (cellType == DocumentFormat.OpenXml.Spreadsheet.CellValues.String ||cellType == DocumentFormat.OpenXml.Spreadsheet.CellValues.SharedString){return Convert.ToString(value) ?? string.Empty;}else{return string.Empty;}}}

  

using Aspose.CAD.FileFormats.Cad;
using Aspose.CAD.ImageOptions;
using Aspose.Cells;
using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
using Aspose.Pdf;
using Aspose.Pdf.Devices;
using Aspose.Pdf.Operators;
using Aspose.Pdf.Plugins;
using Aspose.Pdf.Text;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.ImageLoadOptions;
using Aspose.Slides;
using Aspose.Slides.Export.Web;
using Aspose.Svg;
using Aspose.Words;
using Aspose.Words.Saving;
using CsvHelper;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
using iTextSharp.text;
using iTextSharp.text.pdf;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Webp;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Web;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/945573.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Offsec Nibbles CTF 实战解析:PostgreSQL漏洞利用与权限提升

本文详细记录了Offsec Nibbles CTF挑战的完整解题过程,重点分析了PostgreSQL CVE-2022-1552漏洞的利用方法,通过Python脚本实现远程代码执行,并分享了权限提升的实战经验,最后讨论了系统加固建议。Offsec Nibbles …

Linux基础——iptables常规操作

Linux基础——iptables常规操作一、iptables概述iptables 的核心是四表五链,四表:raw, connecting tracking, mangle, nat 五链:PREOUTING, INPUT, FORWAED, OUTPUT, POSTROUTING 每种表可以包含的链种类TablesPRER…

MySQLdump 常用参数说明 - 实践

MySQLdump 常用参数说明 - 实践2025-10-24 18:04 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !importa…

2025 10 24日报

上午 写了周作业的F题,一开始以为很难,后面发现字符串的长度小于等于100,于是就可以直接设 dp[i,j,k], 表示 [1∼i] 里面有 j 个 1,总贡献为 k 的方案中与 s [1∼i] 最小的不同个数,然后转移就很明显了dp[i,j,k]…

Linux手动安装最新版 CMake

目录🔽 步骤 1:进入临时目录并下载 CMake 二进制包🔧 步骤 2:解压安装包🧩 步骤 3:将 CMake 添加到系统 PATH方法 A:全局生效(需 root 权限)方法 B:仅对当前用户生效(无 root 权限也可用)✅ 步骤 4:验…

2025年新疆喀纳斯旅游服务权威推荐榜单:新疆/阿勒泰/禾木深度游旅行社综合评测

碧蓝的喀纳斯湖倒映着雪山林海,三湾的晨雾缭绕如仙境,图瓦村落的木屋错落于林间,这片北疆雪域明珠正吸引着越来越多游客的目光。 据新疆文旅厅数据显示,2024年新疆接待游客总量突破2.5亿人次,但随之而来的是投诉量…

一天一款实用的AI工具,第9期,AI转黏土风格

一键将图片转成黏土风格,呈现立体质感与柔和色彩的可爱画面。工具介绍 本期推荐这款【AI转黏土风格】工具,它能将任何照片瞬间变成软萌圆润的黏土动画风! 它完美模拟了真实黏土的柔和质感与磨砂纹理,让人物如同定制…

题解:P8930 「TERRA-OI R1」神,不惧死亡

$O(m \sqrt[3]{\frac{n^2}{m}log^2{n}})$P8930 「TERRA-OI R1」神,不惧死亡 大水紫 首先发现是神秘区间问题,考虑支持区间操作的数据结构。 发现数据范围是 \(1e5\) ,而且维护的东西很神秘,直接考虑分块或莫队。 如…

生产环节最容易出问题的三个点,老板必须盯紧

之前我们企业在做大之前,生产靠的是人盯人。订单少的时候还能勉强跑得动,一旦订单量起来,问题就暴露出来了:计划排不准,执行乱套,质量失控。 在企业规模升级的那几年,我学到了很多,总结起来,生产环节最容易出…

CS50ai: week2 Uncertainty我的笔记A版 - 实践

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025 OSCAR丨与创新者同频!Apache RocketMQ 邀您共赴开源之约

10 月 28 日,北京将迎来开源行业的一场重磅盛会——2025 OSCAR 开源产业大会。 本次大会汇聚全球开源领域的思想领袖、技术专家、企业高管以及创新实践者,围绕开源技术的未来、产业创新及生态协同展开深度讨论。 大会…

2025年PSA制氮设备厂家权威推荐榜单:电解水制氢设备/氦气纯化系统/氘气回收纯化源头厂家精选

在现代工业生产中,氮气作为重要的保护性气体,广泛应用于电子、化工、食品、医药等多个领域。PSA(Pressure Swing Adsorption,变压吸附)制氮设备因其高效节能、操作简便的特点,已成为工业气体分离技术中的重要一环…

解决git clone只有master分支的问题

解决git clone只有master分支的问题clone code: git clone git@gitlab.xxx.yy:zzz/IPM_Semantic.git 在使用 git clone + 远程仓库地址将项目下载下来之后,倘若远程仓库有多个分支,会发现,使用git branch查看本地分…

一文读懂循环神经网络(RNN):原理、局限与LSTM解决方案 - 指南

一文读懂循环神经网络(RNN):原理、局限与LSTM解决方案 - 指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "C…

2025年搬家纸箱权威推荐榜单:物流包装/电商纸箱/平口纸箱源头厂家精选

在物流搬运、家庭搬迁及电商包装领域,纸箱作为不可或缺的包装材料,其质量直接关系到物品的安全与搬运效率。面对市场上琳琅满目的纸箱产品,选择一家可靠的供应商成为许多企业与个人消费者的关键任务。本文基于实地考…

2025年北京cppm认证培训公司权威推荐榜单:cppm考前培训/cppm证书培训/cppm课程培训源头公司精选

面对市场对专业采购人才持续攀升的需求,一项权威调研显示,优质CPPM认证培训机构的学员绩效平均提升达45%以上。 在当今全球化的商业环境中,采购与供应链管理已成为企业核心竞争力的重要组成部分。CPPM(注册职业采购…

详细介绍:记一次达梦数据库的查询异常

详细介绍:记一次达梦数据库的查询异常2025-10-24 17:52 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !…

从价值直觉到价值理性:AI元人文演进路径解读

从价值直觉到价值理性:AI元人文的演进之路 当前人工智能发展面临一个关键挑战:大语言模型虽展现出卓越能力,但其价值响应模式仍建立在简单的参数化基础之上。本文旨在系统阐述AI元人文构想如何引领人工智能实现从价…

【LTDC】在 RGBLCD 屏上实现任意位置画点和读点

前言 此篇文章仅作笔记分享,内容来源为:【正点原子】全是干货 | 手把手教你学STM32的LTDC这篇文章的目的就是熟悉在 RGBLCD 上的绘图操作,了解如何往显存中对应的位置写数据。 绘图实质由于我们的屏幕是 ATK 4.3 80…