Issuing a letter to a spreadsheet using EPPlus

I'm just trying to edit a spreadsheet that already has text written so that the text is blank.

However, it does not work. The method does not seem to do anything.

My code is:

public static void ClearCells(string SpreadsheetFilePath)
{
    var SpreadsheetPath = new FileInfo(SpreadsheetFilePath);
    var package = new ExcelPackage(SpreadsheetPath);
    ExcelWorkbook workBook = package.Workbook;
    if (workBook != null)
    {
        if (workBook.Worksheets.Count > 0)
        {
            ExcelWorksheet currentWorksheet = workBook.Worksheets.First();
            currentWorksheet.SetValue(1, 1, "hello123");
        }
    }
}

Runtime error at runtime. It starts, ends, and the table does not change. I do not know why.

+4
source share
3 answers

Just call package.Save();after editing the file.

var SpreadsheetPath = new FileInfo(@"C:\temp\abc.xlsx");
var package = new ExcelPackage(SpreadsheetPath);
ExcelWorkbook workBook = package.Workbook;
if (workBook != null)
{
    if (workBook.Worksheets.Count > 0)
    {
        ExcelWorksheet currentWorksheet = workBook.Worksheets.First();
        currentWorksheet.SetValue(1, 1, "hello123");
    }
}

package.Save();  // <========= MISSING IN YOUR CODE
+6
source

Try something like this:

public static void ClearCells(string SpreadsheetFilePath)
{
    FileInfo excelFile = new FileInfo(SpreadsheetFilePath);
    ExcelPackage excelPack = ExcelPackage(excelFile);

    var workSheet = excelPack.Workbook.Worksheets.Add("Content"); // New worksheet.           
    workSheet.Cells[1, 1].Value = "hello123";

    excelPack.Save();
}
+1
source

How to use SetCellValueinstead SetValue?

public static void ClearCells(string SpreadsheetFilePath)
{

var SpreadsheetPath = new FileInfo(SpreadsheetFilePath);
var package = new ExcelPackage(SpreadsheetPath);
ExcelWorkbook workBook = package.Workbook;
if (workBook != null)
{
    if (workBook.Worksheets.Count > 0)
    {
        ExcelWorksheet currentWorksheet = workBook.Worksheets.First();
        currentWorksheet.SetCellValue(1, 1, "hello123");
    }
}
}
-1
source

Source: https://habr.com/ru/post/1543834/


All Articles