EPPLUS To Clear Contents of A Range Of Cells

跟風遠走 提交于 2019-12-10 16:05:54

问题


I want to use EPPLUS to clear a range of cells. I tried the syntax below, but it gives me an error of

object reference not set to an instance of an object

What would be the proper way to clear the contents of cells A24:C36 with EPPLUS?

ExcelPackage package = new ExcelPackage();

ExcelWorksheet ws = package.Workbook.Worksheets["Sheet1"];

ws.Cells["A24:C36"].Clear();

回答1:


Your code is correct. I think the .xlsx file does not have Worksheets with Sheet1 name.

For example, I created this excel file like this:

I wanted to erase A24:C36. I encountered null reference error before executing ws.Cells["A24:C36"].Clear(); like this:

If I use code below instead of it, it works properly (Sheet2).

ExcelPackage package = new ExcelPackage();

ExcelWorksheet ws = package.Workbook.Worksheets["Sheet2"];

ws.Cells["A24:C36"].Clear();

Notice that having no value in A24:C36 does not make an error.



来源:https://stackoverflow.com/questions/45551557/epplus-to-clear-contents-of-a-range-of-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!