Adjusting Compression Level in Workbook

Introduction

Managing large Excel files can be a challenge, especially when it comes to storage and transfer efficiency. Fortunately, file compression can significantly reduce the size of these files, making them easier to handle. If you’re using Aspose.Cells for .NET, you have the ability to adjust the compression level of your workbooks easily. This guide will walk you through the process step-by-step, providing clear explanations of each part of the code.

Prerequisites

Before we dive into the code, make sure you have the following prerequisites:

  1. Basic Knowledge of C#: Familiarity with C# programming will help you understand the code snippets better.
  2. Aspose.Cells Library: Download and install the Aspose.Cells library from here.
  3. Visual Studio: A development environment like Visual Studio is necessary to run the code.
  4. .NET Framework: Ensure your project is set up with a compatible version of the .NET Framework.

Importing Necessary Packages

To get started, you need to import the necessary packages in your C# project. Add the following lines at the top of your code file:

using Aspose.Cells.Rendering;
using Aspose.Cells.WebExtensions;
using System;

These packages are essential for working with Excel files using the Aspose.Cells library. The Aspose.Cells namespace contains all the classes needed to manipulate Excel files, while Aspose.Cells.Xlsb provides options for saving files in the XLSB format.

Step 1: Define Source and Output Directories

First, set up the directories where your source files are located and where you want to save the output files:

// Define source and output directories
string sourceDir = "Your Document Directory\\";
string outDir = "Your Document Directory\\";

Make sure to replace "Your Document Directory\\" with the actual paths to your directories. This ensures your program can locate the files it needs to work with.

Step 2: Load the Workbook

Next, load the workbook that you want to compress:

Workbook workbook = new Workbook(sourceDir + "LargeSampleFile.xlsx");

Here, we create a new instance of the Workbook class and load an existing Excel file. Ensure the file name matches the one in your source directory.

Step 3: Set Up Save Options

Now, configure the save options for your workbook:

XlsbSaveOptions options = new XlsbSaveOptions();

The XlsbSaveOptions class allows you to specify various options when saving your workbook in the XLSB format, including compression levels.

Step 4: Measure Compression Time for Level 1

Start with the first compression level and measure the time it takes to save the workbook:

options.CompressionType = OoxmlCompressionType.Level1;
var watch = Stopwatch.StartNew();
workbook.Save(outDir + "LargeSampleFile_level_1_out.xlsb", options);
watch.Stop();
Console.WriteLine("Level 1 Elapsed Time: " + watch.ElapsedMilliseconds + " ms");

This snippet sets the compression type to Level 1, saves the workbook, and measures the elapsed time.

Step 5: Measure Compression Time for Level 6

Next, test the performance with Level 6 compression:

options.CompressionType = OoxmlCompressionType.Level6;
watch = Stopwatch.StartNew();
workbook.Save(outDir + "LargeSampleFile_level_6_out.xlsb", options);
watch.Stop();
Console.WriteLine("Level 6 Elapsed Time: " + watch.ElapsedMilliseconds + " ms");

This step is similar to the previous one, but with a higher compression level.

Step 6: Measure Compression Time for Level 9

Finally, evaluate the performance with the highest compression level:

options.CompressionType = OoxmlCompressionType.Level9;
watch = Stopwatch.StartNew();
workbook.Save(outDir + "LargeSampleFile_level_9_out.xlsb", options);
watch.Stop();
Console.WriteLine("Level 9 Elapsed Time: " + watch.ElapsedMilliseconds + " ms");

This step sets the compression level to Level 9, where you will likely see the most significant reduction in file size, although it may take longer to process.

Step 7: Final Output

After completing all compression levels, output a message indicating that the process has finished successfully:

Console.WriteLine("Compression adjustment completed successfully.");

This simple line confirms that your program has executed without issues.

Conclusion

Adjusting the compression level of your workbooks using Aspose.Cells for .NET is a straightforward process that can lead to significant improvements in file size and performance. By following the steps outlined in this guide, you can efficiently implement compression in your applications, enhancing your Excel file management capabilities.

FAQ’s

What is Aspose.Cells?

Aspose.Cells is a powerful library for .NET that allows developers to create, manipulate, and convert Excel files without needing Microsoft Excel.

How do I install Aspose.Cells?

You can download and install Aspose.Cells from the Aspose website.

What compression levels are available?

Aspose.Cells supports multiple compression levels ranging from Level 1 (lowest compression) to Level 9 (highest compression).

Can I test Aspose.Cells for free?

Yes! You can get a free trial of Aspose.Cells here.

Where can I find support for Aspose.Cells?

For any queries or support, visit the Aspose support forum here.