我只是想编辑一个已写入文本的电子表格,将文本更改为空白。
然而它无法正常工作。该方法似乎无能为力。
我的代码是:
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");
}
}
}
没有运行时错误。它运行,完成,电子表格不变。我不知道为什么。
答案 0 :(得分:6)
完成编辑文件后,只需致电package.Save();
即可。
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
答案 1 :(得分:1)
尝试这样的事情:
public static void ClearCells(string SpreadsheetFilePath)
{
var excelFile = new FileInfo(SpreadsheetFilePath);
var excelPack = ExcelPackage(excelFile);
var workSheet = excelPack.Workbook.Worksheets.Add("Content"); // New worksheet
workSheet.Cells[1, 1].Value = "hello123";
excelPack.Save();
}
答案 2 :(得分:-1)
如何使用SetCellValue
代替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");
}
}
}