Hi Team,
I am facing an issue where formulas in the spreadsheet are not properly reflecting when importing or downloading sheets in my application. Specifically, the formulas are not retained during the upload process or included correctly during the export/download operation.
Below are the reference codes for upload and download functionality:
Upload Code:
using (var package = new ExcelPackage(stream))
{
var workbook = package.Workbook;
var worksheet = workbook.Worksheets[0];
var rows = new List<dynamic>();
for (int row = 1; row <= worksheet.Dimension.End.Row; row++)
{
var rowData = new List<string>();
for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
{
rowData.Add(worksheet.Cells[row, col].Text);
}
rows.Add(new { cells = rowData.Select(val => new { value = val }).ToArray() });
}
var sheetData = new
{
name = worksheet.Name,
rows = rows.ToArray()
};
return Json(new { success = true, data = sheetData });
}
Download Code:
using (var package = new ExcelPackage())
{
var sheet = spreadsheetData.sheets.FirstOrDefault();
if (sheet != null)
{
var worksheet = package.Workbook.Worksheets.Add(sheet.name);
foreach (var row in sheet.rows)
{
foreach (var cell in row.cells)
{
var excelCell = worksheet.Cells[row.index + 1, cell.index + 1];
// Set font properties
excelCell.Style.Font.Size = cell.fontSize > 0 ? cell.fontSize : sheet.defaultCellStyle.fontSize;
excelCell.Style.Font.Bold = cell.bold;
// Set background color
if (!string.IsNullOrEmpty(cell.background))
{
excelCell.Style.Fill.PatternType = ExcelFillStyle.Solid;
excelCell.Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml(cell.background));
}
// Set font color
if (!string.IsNullOrEmpty(cell.color))
{
excelCell.Style.Font.Color.SetColor(ColorTranslator.FromHtml(cell.color));
}
// Set text alignment
excelCell.Style.HorizontalAlignment = cell.textAlign switch
{
"center" => ExcelHorizontalAlignment.Center,
"right" => ExcelHorizontalAlignment.Right,
_ => ExcelHorizontalAlignment.Left,
};
// Set number format
if (!string.IsNullOrEmpty(cell.format))
{
excelCell.Style.Numberformat.Format = cell.format;
}
// Set formula if available
excelCell.Formula = cell.formula;
// Set cell value
excelCell.Value = cell.value;
}
}
}
fileStream = new MemoryStream(package.GetAsByteArray());
}
Could you please guide me on resolving this issue to ensure formulas are preserved and processed accurately?
Thank you for your assistance!
I am facing an issue where formulas in the spreadsheet are not properly reflecting when importing or downloading sheets in my application. Specifically, the formulas are not retained during the upload process or included correctly during the export/download operation.
Below are the reference codes for upload and download functionality:
Upload Code:
using (var package = new ExcelPackage(stream))
{
var workbook = package.Workbook;
var worksheet = workbook.Worksheets[0];
var rows = new List<dynamic>();
for (int row = 1; row <= worksheet.Dimension.End.Row; row++)
{
var rowData = new List<string>();
for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
{
rowData.Add(worksheet.Cells[row, col].Text);
}
rows.Add(new { cells = rowData.Select(val => new { value = val }).ToArray() });
}
var sheetData = new
{
name = worksheet.Name,
rows = rows.ToArray()
};
return Json(new { success = true, data = sheetData });
}
Download Code:
using (var package = new ExcelPackage())
{
var sheet = spreadsheetData.sheets.FirstOrDefault();
if (sheet != null)
{
var worksheet = package.Workbook.Worksheets.Add(sheet.name);
foreach (var row in sheet.rows)
{
foreach (var cell in row.cells)
{
var excelCell = worksheet.Cells[row.index + 1, cell.index + 1];
// Set font properties
excelCell.Style.Font.Size = cell.fontSize > 0 ? cell.fontSize : sheet.defaultCellStyle.fontSize;
excelCell.Style.Font.Bold = cell.bold;
// Set background color
if (!string.IsNullOrEmpty(cell.background))
{
excelCell.Style.Fill.PatternType = ExcelFillStyle.Solid;
excelCell.Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml(cell.background));
}
// Set font color
if (!string.IsNullOrEmpty(cell.color))
{
excelCell.Style.Font.Color.SetColor(ColorTranslator.FromHtml(cell.color));
}
// Set text alignment
excelCell.Style.HorizontalAlignment = cell.textAlign switch
{
"center" => ExcelHorizontalAlignment.Center,
"right" => ExcelHorizontalAlignment.Right,
_ => ExcelHorizontalAlignment.Left,
};
// Set number format
if (!string.IsNullOrEmpty(cell.format))
{
excelCell.Style.Numberformat.Format = cell.format;
}
// Set formula if available
excelCell.Formula = cell.formula;
// Set cell value
excelCell.Value = cell.value;
}
}
}
fileStream = new MemoryStream(package.GetAsByteArray());
}
Could you please guide me on resolving this issue to ensure formulas are preserved and processed accurately?
Thank you for your assistance!