วิธีการแปลงไฟล์ Excel ไปเป็น PDF ใน .NET

วิธีการแปลงไฟล์ Excel ไปเป็น PDF ใน .NET

บทความนี้แสดงให้เห็นว่าวิธีการแปลงไฟล์ Excel หลายไฟล์เป็น PDF โดยใช้ Aspose.Cells LowCode PDF Converter ในแอพ .NET แปลง PDF ให้วิธีการที่สมดุลในการแปลงเอกสารโดยไม่จําเป็นต้องมีการเข้ารหัสอย่างกว้างขวางหรือความรู้ลึกเกี่ยวกับโครงสร้างภายใน Excel - เหมาะสําหรับนักวิเคราะห์ธุรกิจและนักพัฒนารายงานที่ต้องการอัตโนมัติการทํางานของข้อมูล

ปัญหาโลกจริง

ในสภาพแวดล้อมขององค์กรทีมรายงานมักต้องแปลงหลายสิบหรือแม้กระทั่งหลายร้อยหนังสือทํางานของ Excel ไปเป็นรูปแบบ PDF โดยปกติ การทําเช่นนี้ด้วยตนเองเป็นเวลาประหยัดความผิดพลาดและแยกทรัพยากรที่มีค่าจากงานวิเคราะห์ที่สําคัญยิ่งขึ้น นอกจากนี้การให้แน่ใจว่าการจัดรูปแบบที่สม่ําเสมอและการจัดระเบียบผ่านรายชื่อทั้งหมดเป็นเรื่องยากเมื่อแปลงไฟล์แต่ละครั้ง

ความคิดเห็นเกี่ยวกับโซลูชัน

ใช้ Aspose.Cells LowCode PDF Converter เราสามารถแก้ปัญหานี้ได้อย่างมีประสิทธิภาพด้วยรหัสขั้นต่ํา โซลูชันนี้เหมาะสําหรับนักวิเคราะห์ธุรกิจและนักพัฒนารายงานที่ต้องการให้กระบวนการรายชื่อของพวกเขาเรียบง่ายให้มั่นใจว่าเอกสารมีความสม่ําเสมอและมีเวลาในการทํางานที่มีมูลค่ามากขึ้น วิธีการของเราจะมุ่งเน้นไปที่การสร้างระบบประมวลผลชุดที่แข็งแกร่งซึ่งสามารถจัดการปริมาณไฟล์ Excel ขนาดใหญ่ด้วยการตั้งค่าการแปลงที่กําหนดเอง

ข้อกําหนด

ก่อนที่จะใช้โซลูชันให้แน่ใจว่าคุณมี:

  • Visual Studio 2019 หรือภายหลัง
  • .NET 6.0 หรือเร็วกว่า (เข้ากันได้กับ .Net Framework 4.6.2+)
  • Aspose.Cells สําหรับแพคเกจ .NET ที่ติดตั้งผ่าน NuGet
  • ความเข้าใจพื้นฐานของการเขียนโปรแกรม C#
PM> Install-Package Aspose.Cells

การดําเนินการขั้นตอนขั้นตอน

ขั้นตอน 1: ติดตั้งและตั้งค่า Aspose.Cells

เพิ่มแพคเกจ Aspose.Cells ไปยังโครงการของคุณและรวมพื้นที่ชื่อที่จําเป็น:

using Aspose.Cells;
using Aspose.Cells.LowCode;
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;

ขั้นตอนที่ 2: สร้างโครงสร้างไดเรกทอรีสําหรับการประมวลผลชุด

สร้างโครงสร้างตารางเพื่อจัดเตรียมไฟล์ Excel input และไฟล์ PDF output ของคุณ:

// Create directories if they don't exist
string inputDirectory = @"C:\Reports\ExcelFiles";
string outputDirectory = @"C:\Reports\PDFOutput";
string logDirectory = @"C:\Reports\Logs";

Directory.CreateDirectory(inputDirectory);
Directory.CreateDirectory(outputDirectory);
Directory.CreateDirectory(logDirectory);

ขั้นตอนที่ 3: การนําไปใช้ File Discovery Logic

สร้างวิธีการที่จะค้นพบไฟล์ Excel ทั้งหมดที่จําเป็นต้องแปลง:

private List<string> GetExcelFilesToProcess(string directoryPath, string searchPattern = "*.xlsx")
{
    List<string> excelFiles = new List<string>();
    
    try
    {
        // Get all Excel files (.xlsx) in the directory
        string[] files = Directory.GetFiles(directoryPath, searchPattern);
        excelFiles.AddRange(files);
        
        // Optionally, you can also search for .xls files
        string[] oldFormatFiles = Directory.GetFiles(directoryPath, "*.xls");
        excelFiles.AddRange(oldFormatFiles);
        
        Console.WriteLine($"Found {excelFiles.Count} Excel files to process.");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error searching for Excel files: {ex.Message}");
    }
    
    return excelFiles;
}

ขั้นตอน 4: การตั้งค่าตัวเลือก PDF Converter

สร้างวิธีการตั้งค่าตัวเลือกการแปลง PDF สําหรับการส่งออกที่สม่ําเสมอ:

private LowCodePdfSaveOptions ConfigurePdfOptions(bool onePagePerSheet = true, 
                                                 bool includeHiddenSheets = false,
                                                 bool printComments = false)
{
    // Create PDF save options
    PdfSaveOptions pdfOpts = new PdfSaveOptions();
    
    // Set whether each worksheet should be rendered as a separate page
    pdfOpts.OnePagePerSheet = onePagePerSheet;
    
    // Option to include hidden worksheets in the output
    pdfOpts.AllColumnsInOnePagePerSheet = true;
    
    // Configure additional PDF options
    pdfOpts.Compliance = PdfCompliance.PdfA1b; // For archival quality
    pdfOpts.PrintingPageType = PrintingPageType.ActualSize;
    
    // Configure comment display options
    pdfOpts.PrintComments = printComments ? PrintCommentsType.PrintInPlace : PrintCommentsType.PrintNoComments;
    
    // Create low code PDF save options
    LowCodePdfSaveOptions lcsopts = new LowCodePdfSaveOptions();
    lcsopts.PdfOptions = pdfOpts;
    
    return lcsopts;
}

ขั้นตอน 5: การประยุกต์ใช้โลจิกการแปลงไฟล์เดียว

สร้างวิธีการแปลงไฟล์ Excel เป็นไฟล์ PDF:

private bool ConvertExcelToPdf(string inputFilePath, string outputFilePath, LowCodePdfSaveOptions pdfOptions)
{
    try
    {
        // Create load options
        LowCodeLoadOptions loadOptions = new LowCodeLoadOptions();
        loadOptions.InputFile = inputFilePath;
        
        // Set output file path
        pdfOptions.OutputFile = outputFilePath;
        
        // Process the conversion
        PdfConverter.Process(loadOptions, pdfOptions);
        
        return true;
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error converting {Path.GetFileName(inputFilePath)}: {ex.Message}");
        return false;
    }
}

ขั้นตอนที่ 6: การประมวลผลแบทช์การปฏิบัติตามโลโก้

ตอนนี้สร้างวิธีการหลักที่จะประมวลผลไฟล์หลาย:

public void BatchConvertExcelToPdf(string inputDirectory, string outputDirectory, string searchPattern = "*.xlsx")
{
    // Get all Excel files
    List<string> excelFiles = GetExcelFilesToProcess(inputDirectory, searchPattern);
    
    // Configure PDF options
    LowCodePdfSaveOptions pdfOptions = ConfigurePdfOptions(true, false, false);
    
    // Track conversion statistics
    int successCount = 0;
    int failureCount = 0;
    
    // Process each file
    foreach (string excelFile in excelFiles)
    {
        string fileName = Path.GetFileNameWithoutExtension(excelFile);
        string outputFilePath = Path.Combine(outputDirectory, $"{fileName}.pdf");
        
        Console.WriteLine($"Converting: {fileName}");
        
        bool success = ConvertExcelToPdf(excelFile, outputFilePath, pdfOptions);
        
        if (success)
        {
            successCount++;
            Console.WriteLine($"Successfully converted: {fileName}");
        }
        else
        {
            failureCount++;
            Console.WriteLine($"Failed to convert: {fileName}");
        }
    }
    
    // Report summary
    Console.WriteLine("\n===== Conversion Summary =====");
    Console.WriteLine($"Total files processed: {excelFiles.Count}");
    Console.WriteLine($"Successfully converted: {successCount}");
    Console.WriteLine($"Failed conversions: {failureCount}");
}

ขั้นตอน 7: การประมวลผลร่วมกันเพื่อประสิทธิภาพที่ดีขึ้น

สําหรับแพทช์ขนาดใหญ่นําไปใช้การประมวลผลแบบด้ายเพื่อเร่งการแปลง:

public void ParallelBatchConvertExcelToPdf(string inputDirectory, string outputDirectory, string searchPattern = "*.xlsx")
{
    // Get all Excel files
    List<string> excelFiles = GetExcelFilesToProcess(inputDirectory, searchPattern);
    
    // Track conversion statistics
    int successCount = 0;
    int failureCount = 0;
    object lockObj = new object();
    
    // Configure PDF options (will be reused for each conversion)
    LowCodePdfSaveOptions pdfOptions = ConfigurePdfOptions(true, false, false);
    
    // Process files in parallel
    Parallel.ForEach(excelFiles, 
        new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, 
        excelFile =>
    {
        string fileName = Path.GetFileNameWithoutExtension(excelFile);
        string outputFilePath = Path.Combine(outputDirectory, $"{fileName}.pdf");
        
        // Each thread needs its own copy of the options
        LowCodePdfSaveOptions threadPdfOptions = ConfigurePdfOptions(true, false, false);
        threadPdfOptions.OutputFile = outputFilePath;
        
        Console.WriteLine($"Converting: {fileName}");
        
        // Create load options
        LowCodeLoadOptions loadOptions = new LowCodeLoadOptions();
        loadOptions.InputFile = excelFile;
        
        try
        {
            // Process the conversion
            PdfConverter.Process(loadOptions, threadPdfOptions);
            
            lock (lockObj)
            {
                successCount++;
                Console.WriteLine($"Successfully converted: {fileName}");
            }
        }
        catch (Exception ex)
        {
            lock (lockObj)
            {
                failureCount++;
                Console.WriteLine($"Failed to convert {fileName}: {ex.Message}");
            }
        }
    });
    
    // Report summary
    Console.WriteLine("\n===== Conversion Summary =====");
    Console.WriteLine($"Total files processed: {excelFiles.Count}");
    Console.WriteLine($"Successfully converted: {successCount}");
    Console.WriteLine($"Failed conversions: {failureCount}");
}

ขั้นตอน 8: ตัวอย่างการดําเนินการที่สมบูรณ์

นี่คือตัวอย่างการทํางานที่สมบูรณ์ซึ่งแสดงให้เห็นถึงกระบวนการแปลงชุดทั้งหมด:

using Aspose.Cells;
using Aspose.Cells.LowCode;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace ExcelBatchConverter
{
    public class ExcelToPdfBatchConverter
    {
        private readonly string _inputDirectory;
        private readonly string _outputDirectory;
        private readonly string _logDirectory;
        
        public ExcelToPdfBatchConverter(string inputDirectory, string outputDirectory, string logDirectory)
        {
            _inputDirectory = inputDirectory;
            _outputDirectory = outputDirectory;
            _logDirectory = logDirectory;
            
            // Ensure directories exist
            Directory.CreateDirectory(_inputDirectory);
            Directory.CreateDirectory(_outputDirectory);
            Directory.CreateDirectory(_logDirectory);
        }
        
        // Log the conversion activity
        private void LogConversion(string message)
        {
            string logFilePath = Path.Combine(_logDirectory, $"ConversionLog_{DateTime.Now.ToString("yyyyMMdd")}.txt");
            string logEntry = $"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}] {message}";
            
            try
            {
                File.AppendAllText(logFilePath, logEntry + Environment.NewLine);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Warning: Unable to write to log file: {ex.Message}");
            }
            
            Console.WriteLine(logEntry);
        }
        
        // Get all Excel files to process
        private List<string> GetExcelFilesToProcess(string searchPattern = "*.xlsx")
        {
            List<string> excelFiles = new List<string>();
            
            try
            {
                // Get all Excel files in the directory
                excelFiles.AddRange(Directory.GetFiles(_inputDirectory, "*.xlsx"));
                excelFiles.AddRange(Directory.GetFiles(_inputDirectory, "*.xls"));
                excelFiles.AddRange(Directory.GetFiles(_inputDirectory, "*.xlsm"));
                
                LogConversion($"Found {excelFiles.Count} Excel files to process.");
            }
            catch (Exception ex)
            {
                LogConversion($"Error searching for Excel files: {ex.Message}");
            }
            
            return excelFiles;
        }
        
        // Configure PDF conversion options
        private LowCodePdfSaveOptions ConfigurePdfOptions()
        {
            // Create PDF save options
            PdfSaveOptions pdfOpts = new PdfSaveOptions();
            
            // Set whether each worksheet should be rendered as a separate page
            pdfOpts.OnePagePerSheet = true;
            
            // Set additional PDF options
            pdfOpts.Compliance = PdfCompliance.PdfA1b; // For archival quality
            pdfOpts.AllColumnsInOnePagePerSheet = true;
            
            // Create low code PDF save options
            LowCodePdfSaveOptions lcsopts = new LowCodePdfSaveOptions();
            lcsopts.PdfOptions = pdfOpts;
            
            return lcsopts;
        }
        
        // Process all Excel files in the input directory
        public void ProcessAllFiles(bool useParallelProcessing = true)
        {
            LogConversion("Starting batch conversion process...");
            
            List<string> excelFiles = GetExcelFilesToProcess();
            
            if (excelFiles.Count == 0)
            {
                LogConversion("No Excel files found to process.");
                return;
            }
            
            // Conversion statistics
            int successCount = 0;
            int failureCount = 0;
            
            if (useParallelProcessing)
            {
                // For thread safety with parallel processing
                object lockObj = new object();
                
                // Process files in parallel
                Parallel.ForEach(excelFiles, 
                    new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, 
                    excelFile =>
                {
                    string fileName = Path.GetFileNameWithoutExtension(excelFile);
                    string outputFilePath = Path.Combine(_outputDirectory, $"{fileName}.pdf");
                    
                    try
                    {
                        // Create load options for this file
                        LowCodeLoadOptions loadOptions = new LowCodeLoadOptions();
                        loadOptions.InputFile = excelFile;
                        
                        // Configure PDF options for this file
                        LowCodePdfSaveOptions pdfOptions = ConfigurePdfOptions();
                        pdfOptions.OutputFile = outputFilePath;
                        
                        // Process the conversion
                        PdfConverter.Process(loadOptions, pdfOptions);
                        
                        lock (lockObj)
                        {
                            successCount++;
                            LogConversion($"Successfully converted: {fileName}");
                        }
                    }
                    catch (Exception ex)
                    {
                        lock (lockObj)
                        {
                            failureCount++;
                            LogConversion($"Failed to convert {fileName}: {ex.Message}");
                        }
                    }
                });
            }
            else
            {
                // Process files sequentially
                foreach (string excelFile in excelFiles)
                {
                    string fileName = Path.GetFileNameWithoutExtension(excelFile);
                    string outputFilePath = Path.Combine(_outputDirectory, $"{fileName}.pdf");
                    
                    try
                    {
                        // Create load options for this file
                        LowCodeLoadOptions loadOptions = new LowCodeLoadOptions();
                        loadOptions.InputFile = excelFile;
                        
                        // Configure PDF options for this file
                        LowCodePdfSaveOptions pdfOptions = ConfigurePdfOptions();
                        pdfOptions.OutputFile = outputFilePath;
                        
                        // Process the conversion
                        PdfConverter.Process(loadOptions, pdfOptions);
                        
                        successCount++;
                        LogConversion($"Successfully converted: {fileName}");
                    }
                    catch (Exception ex)
                    {
                        failureCount++;
                        LogConversion($"Failed to convert {fileName}: {ex.Message}");
                    }
                }
            }
            
            // Report summary
            LogConversion("\n===== Conversion Summary =====");
            LogConversion($"Total files processed: {excelFiles.Count}");
            LogConversion($"Successfully converted: {successCount}");
            LogConversion($"Failed conversions: {failureCount}");
            LogConversion("Batch conversion process completed.");
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            // Define directory paths
            string inputDirectory = @"C:\Reports\ExcelFiles";
            string outputDirectory = @"C:\Reports\PDFOutput";
            string logDirectory = @"C:\Reports\Logs";
            
            // Create and run the converter
            ExcelToPdfBatchConverter converter = new ExcelToPdfBatchConverter(
                inputDirectory, outputDirectory, logDirectory);
                
            // Process all files (using parallel processing)
            converter.ProcessAllFiles(true);
            
            Console.WriteLine("Process completed. Press any key to exit.");
            Console.ReadKey();
        }
    }
}

ใช้กรณีและแอปพลิเคชัน

ระบบรายงานองค์กร

ในระบบการรายงานทางการเงินรอบปลายเดือนหรือรอบเดือนมักจะต้องแปลงรายได้จํานวนมากบนพื้นฐานของ Excel ไปเป็นรูปแบบ PDF สําหรับการกระจายให้กับผู้มีส่วนร่วม โซลูชันการแปลงชุดนี้ช่วยให้ผู้พัฒนารายชื่อสามารถอัตโนมัติกระบวนการให้แน่ใจว่ารายข่าวทั้งหมดจะถูกแปลงด้วยการตั้งค่าและรูปแบบที่สม่ําเสมอในขณะที่ลดแรงบันดาลใจอย่างมาก รายงานสามารถเก็บรวบรวมในรูปแบบมาตรฐานโดยอัจฉริยะทําให้การกู้คืนและการปฏิบัติตามง่ายขึ้น

การประมวลผลข้อมูลแผนก

นักวิเคราะห์ธุรกิจที่รวบรวมข้อมูลจาก Excel จากส่วนหลายสามารถใช้โซลูชันนี้เพื่อมาตรฐานและเก็บเอกสาร โดยการแปลงหนังสือพิมพ์ที่ได้รับเป็น PDF โดยอัตโนมัติพวกเขาสร้างบันทึกคงที่ของรายการข้อมูลในขณะที่ทําให้ข้อมูลสามารถแบ่งปันได้กับผู้มีส่วนร่วมที่ไม่มี Excel ความสามารถในการประมวลผลชุดหมายความว่าแม้กระทั่งแผนกขนาดใหญ่ที่มีหลายร้อยสมุดงานสามารถดําเนินการได้อย่างมีประสิทธิภาพ

การจัดการเอกสารอัตโนมัติ Workflows

การบูรณาการกับระบบการจัดการเอกสารกลายเป็นไร้รอยต่อเมื่อรายงาน Excel แปลงเป็น PDF โดยอัตโนมัติ โซลูชันนี้สามารถวางแผนที่จะทํางานเป็นส่วนหนึ่งของกระแสทํางานที่ใหญ่กว่าที่อัพเกรดไฟล์ Excel ใหม่แปลงพวกเขาไปยัง PDF และจากนั้นนําไปสู่บันทึกเอกชนที่เหมาะสมพร้อม metadata ที่เหมาะสม การจัดรูปแบบที่สม่ําเสมอและไฟล์ที่มีคุณภาพส่งออก PDF ให้การเข้าถึงระยะยาวของข้อมูลธุรกิจ

ความท้าทายและโซลูชั่นทั่วไป

ความท้าทาย 1: การจัดการไฟล์ Excel ด้วยการป้องกันรหัสผ่าน

โซลูชัน: เปลี่ยนตัวเลือกโหลดเพื่อรวมการจัดการรหัสผ่าน:

// For password-protected Excel files
LowCodeLoadOptions loadOptions = new LowCodeLoadOptions();
loadOptions.InputFile = excelFile;
loadOptions.Password = "YourPassword"; // Supply the password

// Then proceed with conversion as normal
PdfConverter.Process(loadOptions, pdfOptions);

ความท้าทาย 2: การรักษา Excel Formatting ใน PDF Output

โซลูชัน: ตรวจสอบให้แน่ใจว่าตัวเลือก PDF มีการกําหนดค่าที่เหมาะสมเพื่อรักษารูปแบบ:

PdfSaveOptions pdfOpts = new PdfSaveOptions();
pdfOpts.OnePagePerSheet = true;
pdfOpts.PrintingPageType = PrintingPageType.ActualSize;
pdfOpts.DefaultFontName = "Arial"; // Specify a fallback font
pdfOpts.Compliance = PdfCompliance.PdfA1b; // For archival quality

ความท้าทาย 3: การจัดการไฟล์ Excel ขนาดใหญ่มาก

โซลูชัน: สําหรับไฟล์ขนาดใหญ่มากใช้การปรับปรุงหน่วยความจํา:

LowCodeLoadOptions loadOptions = new LowCodeLoadOptions();
loadOptions.InputFile = largeExcelFile;
loadOptions.MemorySetting = MemorySetting.MemoryPreference; // Optimize for memory usage

// Consider file-specific optimizations
PdfSaveOptions pdfOpts = new PdfSaveOptions();
pdfOpts.OptimizationType = OptimizationType.MinimumSize;

การพิจารณาประสิทธิภาพ

  • สําหรับประสิทธิภาพที่สมบูรณ์แบบกับชุดขนาดใหญ่ใช้วิธีการประมวลผลร่วมกันด้วยวิธีการที่เหมาะสม MaxDegreeOfParallelism ตามความสามารถของเซิร์ฟเวอร์ของคุณ
  • โปรดพิจารณาการประมวลผลไฟล์ในชุดขนาดเล็กเมื่อจัดการกับสมุดงาน Excel ขนาดใหญ่มากเพื่อหลีกเลี่ยงความดันหน่วยความจํา
  • การดําเนินงานของกลไกการตรวจสอบเพื่อติดตามขั้นตอนการแปลงและการใช้ทรัพยากร
  • สําหรับการจัดจําหน่ายการผลิตพิจารณาการดําเนินการกระบวนการแปลงบนเซิร์ฟเวอร์ที่กําหนดเองหรือในช่วงเวลากลางแจ้ง

แนวทางที่ดีที่สุด

  • **การรับรองไฟล์ Excel ก่อนการประมวลผลชุดเพื่อระบุปัญหาที่อาจเกิดขึ้น
  • ใช้การจัดการข้อผิดพลาดที่แข็งแกร่ง เพื่อให้แน่ใจว่ากระบวนการจะไม่หยุดถ้าไฟล์หนึ่งล้มเหลว
  • สร้างบันทึกรายละเอียด ของกระบวนการแปลงสําหรับการแก้ปัญหาและการตรวจสอบ
  • จัดระเบียบไฟล์ PDF ของการส่งออก ในแผนภูมิโฟลเดอร์ที่มีโครงสร้างเพื่อการจัดการที่ง่ายขึ้น
  • ตั้งค่าระบบตรวจสอบ ที่แจ้งเตือนผู้ดูแลระบบเกี่ยวกับความล้มเหลวในการแปลง
  • การทดสอบกับรูปแบบไฟล์ Excelต่างๆ (XLSX, XLS,XLSM) เพื่อให้แน่ใจว่ามีการเข้ากันได้

การ์ตูนขั้นสูง

สําหรับข้อกําหนดที่ซับซ้อนมากขึ้นพิจารณาการประมวลผลขั้นสูงเหล่านี้:

ฉาก 1: ชื่อ PDF ที่กําหนดเองขึ้นอยู่กับเนื้อหาเซลล์ Excel

คุณสามารถสกัดค่าเซลล์ที่เฉพาะเจาะจงที่จะใช้ในชื่อไฟล์ PDF:

// Advanced naming based on cell content
private string GetCustomPdfName(string excelFilePath)
{
    try
    {
        // Load the workbook
        Workbook workbook = new Workbook(excelFilePath);
        
        // Get report date from cell A1
        string reportDate = workbook.Worksheets[0].Cells["A1"].StringValue;
        
        // Get report type from cell B1
        string reportType = workbook.Worksheets[0].Cells["B1"].StringValue;
        
        // Create a custom name
        string customName = $"{reportType}_{reportDate}";
        
        // Sanitize the filename
        foreach (char invalidChar in Path.GetInvalidFileNameChars())
        {
            customName = customName.Replace(invalidChar, '_');
        }
        
        return customName + ".pdf";
    }
    catch
    {
        // Fallback to default naming if extraction fails
        return Path.GetFileNameWithoutExtension(excelFilePath) + ".pdf";
    }
}

ฉาก 2: การแปลงแผ่นตัวเลือก

แปลงแผ่นเฉพาะเท่านั้นจากแต่ละไฟล์ Excel:

// Configure PDF options to convert only specific sheets
private LowCodePdfSaveOptions ConfigureSelectiveSheetsOptions(string[] sheetNames)
{
    PdfSaveOptions pdfOpts = new PdfSaveOptions();
    pdfOpts.OnePagePerSheet = true;
    
    // Create a worksheet name collection for selective conversion
    WorksheetCollection worksheets = new WorksheetCollection();
    foreach (string sheetName in sheetNames)
    {
        worksheets.Add(sheetName);
    }
    
    // Set sheet selection
    SheetOptions sheetOptions = new SheetOptions();
    sheetOptions.SheetNames = worksheets;
    pdfOpts.SheetOptions = sheetOptions;
    
    // Create low code PDF save options
    LowCodePdfSaveOptions lcsopts = new LowCodePdfSaveOptions();
    lcsopts.PdfOptions = pdfOpts;
    
    return lcsopts;
}

ข้อสรุป

โดยการนําไปใช้ Aspose.Cells LowCode PDF Converter สําหรับการประมวลผลชิ้นส่วนนักวิเคราะห์ธุรกิจและนักพัฒนารายงานสามารถลดเวลาที่ใช้ในการแปลงเอกสาร Excel ไปยัง PDF โดยวิธีการนี้ช่วยปรับปรุงการผลิตและให้มั่นใจถึงความสอดคล้องระหว่างไฟล์ PDF ที่สร้างไว้ในขณะที่รักษาคุณภาพและการจัดรูปแบบของไฟล์ Excel ของเดิม โซลูชันนี้สามารถขยายได้ตั้งแต่การใช้งานของแผนที่ขนาดเล็กไปจนถึงระบบรายชื่อทั่วองค์กรด้วยตัวเลือกการปรับแต่งเพื่อตอบสนองความต้องการทางธุรกิจที่เฉพาะเจาะจง

สําหรับข้อมูลเพิ่มเติมและตัวอย่างเพิ่มเติม โปรดดูที่ Aspose.Cells.LowCode API คําอธิบาย .

 แบบไทย