Aspose.Cells for .NET how to Excel corruption detection write in console

@amjad.sahi, @Professionalize.Discourse, @John.He, @simon.zhao

image.png (4.9 KB)

How to get the witch line getting an error.

@imparthgalani

Cause

You are encountering issues with Excel file corruption and need to identify which line is causing the error during processing. This can occur due to various reasons, such as excessive data, unsupported formats, or corrupted cells.

Solution

To detect corruption in an Excel file and identify the problematic line, you can implement error handling in your code. This will allow you to catch exceptions and log the specific line or cell that is causing the issue.

Code

Here’s a sample code snippet that demonstrates how to read an Excel file and catch exceptions to identify the problematic line:

using Aspose.Cells;
using System;

class Program
{
    static void Main()
    {
        string filePath = "path_to_your_excel_file.xlsx";

        try
        {
            Workbook workbook = new Workbook(filePath);
            // Loop through each worksheet
            foreach (Worksheet sheet in workbook.Worksheets)
            {
                // Loop through each row
                for (int row = 0; row < sheet.Cells.MaxDataRow + 1; row++)
                {
                    try
                    {
                        // Accessing a cell to trigger any potential errors
                        var cellValue = sheet.Cells[row, 0].Value;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error in sheet '{sheet.Name}' at row {row + 1}: {ex.Message}");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Failed to load workbook: {ex.Message}");
        }
    }
}

This code attempts to read each cell in the first column of every worksheet. If an error occurs, it catches the exception and logs the sheet name and row number.

Next Steps

  1. Replace "path_to_your_excel_file.xlsx" with the actual path to your Excel file.
  2. Run the code to identify any errors and their corresponding locations.
  3. Investigate the specific rows or cells that are causing issues based on the output.

If you have any further questions or need additional assistance, feel free to ask!

@imparthgalani
Please share a template file with sample project, we will check them soon.

1 Like

@simon.zhao

using Aspose.Cells;
using Aspose.Cells.Drawing;
using Aspose.Cells.Drawing.Texts;
using GanttChart_JRE_Editor.Common;
using ImageMagick;
using Microsoft.Ajax.Utilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using NLog.Fluent;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI;
using static GanttChart_JRE_Editor.Controllers.PrintingController;
using static Npgsql.Replication.PgOutput.Messages.RelationMessage;

namespace GanttChart_JRE_Editor.Controllers
{
	public class PrintingController : Controller
	{
		private IDBUtil _iDBUtil;
		public PrintingController(IDBUtil iDBUtil)
		{
			_iDBUtil = iDBUtil;
		}

		private static Logger logger = LogManager.GetCurrentClassLogger();
		CommonController commonController = new CommonController();
		string fullFileName = "";

		public string ExportToExcel()
		{
			Workbook workbook = null;
			Worksheet worksheet = null;

			try
			{
				// Create a new workbook
				workbook = new Workbook();
				worksheet = workbook.Worksheets[0];

				ShapePath shapePathBottom = new ShapePath();
				shapePathBottom.MoveTo(0F, 0F);
				shapePathBottom.LineTo(245340F, 0F);
				Shape oShapeBottom = worksheet.Shapes.AddFreeform(0, 0, 0, 0, 0, 0, new ShapePath[] { shapePathBottom });
				oShapeBottom.Width = shapePathBottom.WidthPixel;
				oShapeBottom.Height = shapePathBottom.HeightPixel;
				oShapeBottom.X = 15;
				oShapeBottom.Y = 10;


				// Save the workbook 			
				workbook.Save("品川PJC全体工程表(全部)_210402~300401.xlsx", SaveFormat.Xlsx);				

				// Cleanup (no need for explicit cleanup in Aspose)
				workbook.Dispose();

				return JsonConvert.SerializeObject((new { fileName = fullFileName }));
			}
			catch (Exception e)
			{
				logger.Error(e);	
				return JsonConvert.SerializeObject((new { errorMsg = MSGConstant.GANTT_PROCESSING_FAILED }));
			}

		}
	}
}

image.png (4.9 KB)

out1.zip (6.8 KB)

@imparthgalani
The line is too large(245340 pixels, about 2453 columns). Is it your need?
And we will fix this issue soon.
See Excel crash when open it - #9 by simon.zhao
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-58774

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like

Thanks for your reply.

@imparthgalani
Thank you for your feedback. You are welcome. We will notify you promptly once there are any updates.

1 Like

Aspose.Cells for .NET whenever this type Excel corruption how to implement loggers.

@imparthgalani
Excel has its own standards and limitations, and certain requirements need to be met when assigning values. If the limit is exceeded, an error will occur when opening the file using Excel. Regarding specifications and limits of Excel, please refer to the following document.

If Cells checks every assignment and throws an exception, it will have a significant impact on performance. Please make conditional judgments based on the actual situation and record the logs.

1 Like

@imparthgalani,

We are pleased to inform you that your issue (Ticket ID: “CELLSNET-58774”) has been resolved. The fix/enhancement will be incorporated into the upcoming release of Aspose.Cells (v25.8), which is scheduled for the first half of August 2025. You will be notified when the new version is published.

1 Like

@amjad.sahi, Thanks for solving this.

@imparthgalani
You are welcome. We will notify you once the version v25.8 is released. If you have any questions, please feel free to contact us at any time.

The issues you have found earlier (filed as CELLSNET-58774) have been fixed in this update. This message was posted using Bugs notification tool by leoluo