Free Support Forum - aspose.com

Problems with Excel file size

Before save my workbook I check image original sizes and if they are very big then replace it with new pictures with smaller size. And after call method

Worksheet.Shapes.DeleteShape

I can delete pictures on my sheet, but file size not changed. How fix it?

Example:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using Aspose.Cells;

namespace ConsoleApplication1

{

class Program

{

static void CreateTestWorkbook(string fileName, string imagePath, int imageCount)

{

Workbook workbook = new Workbook();

for (int i = 0; i < imageCount; i++)

workbook.Worksheets[0].Pictures.Add(0, 0, imagePath);

for (int i = 0; i < imageCount; i++)

workbook.Worksheets[0].Shapes.DeleteShape(workbook.Worksheets[0].Pictures[0]);

workbook.Save(fileName, FileFormatType.Excel2003);

}

static void Main(string[] args)

{

string firstWorkbook = "workbook1.xls";

string secondWorkbook = "workbook2.xls";

string imagePath = @"C:\my.bmp";

CreateTestWorkbook(firstWorkbook, imagePath, 20);

CreateTestWorkbook(secondWorkbook, imagePath, 0);

Console.WriteLine("{1} size = {0}", File.ReadAllBytes(firstWorkbook).Length, firstWorkbook);

Console.WriteLine("{1} size = {0}", File.ReadAllBytes(secondWorkbook).Length, secondWorkbook);

Console.ReadKey();

}

}

}

I think that these workbooks must have equal size.

Hi,

Thanks for reporting it.

We will figure it out soon.

Thank you.

Hi, thank you.

I try new code:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using Aspose.Cells;

namespace ConsoleApplication2

{

class Program

{

static void CreateTestWorkbook(string fileName, string imagePath, int imageCount)

{

Workbook workbook = new Workbook();

for (int i = 0; i < imageCount; i++)

workbook.Worksheets[0].Pictures.Add(0, 0, imagePath);

for (int i = 0; i < imageCount; i++)

workbook.Worksheets[0].Shapes.DeleteShape(workbook.Worksheets[0].Pictures[0]);

Workbook cworkbook = new Workbook();

cworkbook.Worksheets.Clear();

cworkbook.Combine(workbook);

cworkbook.Save("C" + fileName);

workbook.Save(fileName, FileFormatType.Excel2003);

}

static void Main(string[] args)

{

string firstWorkbook = "workbook1.xls";

string secondWorkbook = "workbook2.xls";

string imagePath = @"P:\alexr\BMS.BMP";

CreateTestWorkbook(firstWorkbook, imagePath, 20);

CreateTestWorkbook(secondWorkbook, imagePath, 0);

Console.WriteLine("{1} size = {0}", File.ReadAllBytes(firstWorkbook).Length, firstWorkbook);

Console.WriteLine("{1} size = {0}", File.ReadAllBytes(secondWorkbook).Length, secondWorkbook);

Console.WriteLine("C{1} size = {0}", File.ReadAllBytes("C"+firstWorkbook).Length, firstWorkbook);

Console.WriteLine("C{1} size = {0}", File.ReadAllBytes("C"+secondWorkbook).Length, secondWorkbook);

Console.ReadKey();

}

}

}

You can see that combined workbook have equal size. But i try use it in real code and get error:

Object reference not set to an instance of an object.

Stack Trace:

at Aspose.Cells.Name.ౝ(Worksheet ୿, Worksheet ౞)

at Aspose.Cells.Worksheet.ন(Worksheet ୿, Boolean ஁)

at Aspose.Cells.Worksheet.Copy(Worksheet sourceSheet)

at Aspose.Cells.Worksheets.ૡ(Worksheets ૢ)

at Aspose.Cells.Workbook.Combine(Workbook secondWorkbook)

I can combine workbooks after save workbook to MemoryStream and load from it.

Hi,

For workbook.combine issue, we will post a fix soon.

For removing image issue, we are looking into this issue.

Hi,

Please try the fix (4.4.1.13) for Workbook.Combine method issue

Thank you.

Hello,

Thank you for fix.

Hi,

Please try this fix.

We will remove the image data if no shape refer to it in this fix.