excel watermark

How to insert watermark in Excel(XLS, XLSX) using C#

Excel is an incredibly powerful tool for analyzing data and generating reports, but as your workbooks grow in size and complexity, it can become difficult to manage and share them efficiently. Large Excel files can take up valuable storage space, slow down your computer, and make it harder to collaborate with others. That’s where compressing your Excel workbooks comes in. By reducing the file size, you can make it easier to store, share, and work with your Excel files, without sacrificing any of the data or functionality that you need. In this article, we are going to learn the steps on how to compress Excel workbooks and reduce file size using C# .NET & Rest API.

API to Compress Excel File

One of the options to compress Excel workbooks is to use the Aspose.Cells Cloud API. Aspose.Cells Cloud offers a simple and powerful way to work with Excel files in the cloud, including the ability to compress them to reduce their size. With Aspose.Cells Cloud, you can compress your Excel workbooks using a variety of compression algorithms or specify the compression level. These capabilities give you a greater control over the compression process. And because Aspose.Cells Cloud is a cloud-based solution, you can compress your Excel files from anywhere, without having to install any software on your local machine.

Furthermore, using an SDK is the best way to speed up the development. An SDK takes care of low-level details and lets you focus on your project tasks. So, as per the scope of this article, we are going to add the reference of Aspose.Cells Cloud SDK for .NET in our project. So, please search Aspose.Cells-Cloud in NuGet packages manager and click the “Add Package” button. Furthermore, we also need to create an account over Dashboard using a valid email address.

Compress Excel using C#

Given below is a code snippet to compress Excel file size using C# .NET.

Given below are the details of above code snippet:

LightCellsApi lightCellsApi = new LightCellsApi(clientID, clientSecret);

Create an object of LightCellsApi class while passing client credentials as arguments.

vIDictionary<string, Stream> mapFiles = new Dictionary<string, Stream>();
mapFiles.Add("source.xlsx", File.OpenRead(@"source.xlsx"));

Create IDictionary object where we read and add the input Excel files from local storage.

Aspose.Cells.Cloud.SDK.Model.FilesResult filesResult = lightCellsApi.PostCompress(mapFiles, 1,false);

Call the API to compress the Excel file, and we have specified the CompressionLevel as ‘1’.

The input Excel workbook used in the above example can be downloaded from input.xls.

Reduce Excel File Size using cURL Commands

Another way to compress Excel workbooks is to use cURL commands with the Aspose.Cells Cloud API. This approach offers several benefits, such as the ability to automate the compression process using scripts and batch files, and the ability to integrate compression functionality directly into your own software applications. With Aspose.Cells Cloud and cURL commands, you can compress your Excel workbooks quickly and easily, using a wide range of compression levels to achieve the optimal balance of file size and quality.

Now, once we have installed cURL on your system, generate an accessToken based on your client credentials:

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now, execute the following command to compress the Excel file to smaller size:

curl -v "https://api.aspose.cloud/v3.0/cells/compress?CompressLevel=1&checkExcelRestriction=true" \
-X POST \
-H  "accept: application/json" \
-H  "authorization: Bearer {accessToken}" \
-H  "Content-Type: multipart/form-data" \
-d  "File":{"excelFile"}

Replace {excelFile} with the name of input Excel file in cloud storage Replace {accessToken} with the access token generated above

  • We may also download the compressed file to local drive using –o argument.

Concluding Remarks

In conclusion, compressing Excel workbooks is an essential task that can help save disk space and reduce network traffic when working with large amounts of data. With Aspose.Cells Cloud and cURL commands, you have a powerful and flexible set of tools at your disposal to accomplish this task quickly and efficiently. Whether you prefer to use the Aspose.Cells Cloud SDK for .NET or to work directly with cURL commands, you can compress your Excel workbooks to a smaller size without compromising on quality. So why not give it a try today and see how much disk space and bandwidth you can save?

Please visit the following links to learn more about: