ผู้ออกแบบรายงานและนักวิเคราะห์ธุรกิจมักจะจําเป็นต้องรวมภาพตาม Excel ในการนําเสนอเอกสารและแอพเว็บ บทความนี้แสดงให้เห็นว่าวิธีการแปลงแผนที่ Excel และแผ่นงานเป็นภาพ PNG โดยใช้ Aspose.Cells LowCode ImageConverter ในแอป .NET

บทนํา

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

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

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

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

using Aspose.Cells;
using Aspose.Cells.LowCode;
using Aspose.Cells.Rendering;
using System.IO;

ขั้นตอนที่ 2: การเตรียมข้อมูลการเข้า

Identify the Excel file containing the charts or worksheets you want to convert to PNG images. ตรวจสอบให้แน่ใจว่าไฟล์ที่มีอยู่และสามารถเข้าถึงได้จากแอพของคุณ:

// Define the path to your Excel file
string excelFilePath = "reports/quarterly_sales.xlsx";

// Ensure the directory for output exists
Directory.CreateDirectory("result");

ขั้นตอนที่ 3: การตั้งค่าตัวเลือก ImageConverter

การตั้งค่าตัวเลือกสําหรับกระบวนการ ImageConverter ตามความต้องการของคุณ:

// Basic usage - convert the entire workbook
ImageConverter.Process(excelFilePath, "result/BasicOutput.png", new ImageOrPrintOptions { ImageType = ImageType.Png });

ขั้นตอน 4: ทําการแปลงด้วยชื่อที่กําหนดเอง

ใช้ข้อตกลงชื่อที่กําหนดเองสําหรับไฟล์ออก:

// For specific sheet only conversion
ImageConverter.Process(excelFilePath, "result/FirstSheetOnly.png", new ImageOrPrintOptions { PageIndex = 0 }, null);

ขั้นตอนที่ 5: จัดการแผ่นงานขนาดใหญ่และแผนภูมิที่ซับซ้อน

สําหรับแผ่นงานขนาดใหญ่หรือแผนภูมิที่ซับซ้อนตั้งค่าตัวเลือกเพื่อจัดการได้อย่างมีประสิทธิภาพ:

// For specific chart extraction based on title
Workbook workbook = new Workbook(excelFilePath);
Worksheet worksheet = workbook.Worksheets[0];
for (int i = 0; i < worksheet.Charts.Count; i++) {
    Chart chart = worksheet.Charts[i];
    if (chart.Title.Text.Contains("Revenue")) {
        chart.ToImage("result/revenue_chart.png", new ImageOrPrintOptions { ImageType = ImageType.Png });
    }
}

ขั้นตอน 6: การจัดการข้อผิดพลาดและการเข้าสู่ระบบ

การดําเนินการจัดการข้อผิดพลาดเพื่อให้แน่ใจว่ามีความแข็งแรง:

try {
    // Conversion logic here
} catch (Exception ex) {
    Console.WriteLine("Error occurred: " + ex.Message);
    Console.WriteLine(ex.StackTrace);
}

ขั้นตอน 7: การปรับปรุงประสิทธิภาพ

การปรับปรุงประสิทธิภาพโดยใช้การไหลของหน่วยความจําและมัลติความเสี่ยงที่เหมาะสม:

// Use memory stream for batch processing
MemoryStream ms = new MemoryStream();
ImageConverter.Process(excelFilePath, ms, new ImageOrPrintOptions { ImageType = ImageType.Png });
ms.Save("result/batch_output.png", ImageFormat.Png);

ข้อสรุป

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

More in this category