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();
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.
Related
I am using epplus for reading an existing excel file which contains a checkbox in cell C23.
I would like to know how I can check this cell C23 in vb.net :
Using file As FileStream = File.OpenRead(filePath)
Using package as ExcelPackage = New ExcelPackage(file)
Dim book As ExcelWorkbook = package.Workbook
Dim sheet As ExcelWorksheet = book.Worksheets.First()
sheet.Cells("C23").Value = checked..... //I dont know how to check the checkbox
I've also tried Googling it but haven't found anything in the right direction yet.
This might be the duplicate as many posts refer to this kind of issues but I could not find an exact answer to this case.
So, what I have is an Excel file where cell "E4" contains a formula "=C4+D4". All other cells are empty, which means I cannot search or get them via OpenXml, simply because they do not exist in Xml. So in order to fill the cells "C4" and "D4" I have to create them (like this:)
var cell = new Cell(){
CellReference = new StringValue("C4"),
DataType = new EnumValue<CellValues>(CellValues.Number),
CellValue = new CellValue("123")
}
the same for cell "D4" and then append these cells to the row
row.Append(cell);
After I open the excel file it show an error "Excel found unreadable content in file.xlsx. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes."
I checked and when there is no formula in excel file the cells are appended correctly.
So my question is, using OpenXml how do I append cells in Excel file, which contains formula, so that not to break the file?
So the XML is malformed and corrupted the has file, hence the error:
Excel found unreadable content in file
To troubleshoot and fix this I suggest you compare the manually created xlsx to the programmatically created xlsx.
To do this you rename both files extensions from XLSX to ZIP > extract them to different folders > use a WinDiff tool (or better the "OpenXML SDK Productivity Tool") to compare the files.
Once you find how its malformed change your code to try and fix it up.
I solved my problem using EPPLUS
using (ExcelPackage excelPackage = new ExcelPackage(fileStream))
{
ExcelWorkbook excelWorkBook = excelPackage.Workbook;
ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets.First();
excelWorksheet.Cells["C4"].Value = "123";
excelWorksheet.Cells["D4"].Value = "321";
excelPackage.SaveAs(memoryStream);
}
I have build an excel workbook with several worksheets as a reporting template.
The task is done by creating a new excelpackage from the designed xlsx file and fill it up with data.
The template xlsx containing charts and data worksheets.
var packageReport = new ExcelPackage();
packageReport.Load(new memoryStream(Properties.Resources.Reporting_Template));
packageReport.Workbook.Worksheets.Delete(3) // not working >> Exception
packageReport.Workbook.Worksheets.Delete(packageReport.Workbook.Worksheets["Name"]) // not working >> Exception
var ws = packageReport.Workbook.Worksheets.FirstOfDefault(w => w.Name == "Name");
packageReport.Workbook.Worksheets.Delete(ws); // Exception
Also have tried to select the worksheet in a object var before, with also no luck ... Cannot delete any of my newly created worksheets ... :(
Any idea is welcome ...
Thx. Dudes.
Current EPPlus.dll (Version: 4.0.5.0, Runtime Version: v2.0.50727)
Addition:
Also tried to use the template constructor function to generate a copy of the excel stored as a memorystream object with no luck.
I have simple piece of code
public JsonResult ParseExcel(HttpPostedFileBase file)
{
var uploadedFileContent = file.InputStream;
if (POIXMLDocument.HasOOXMLHeader(uploadedFileContent))
{
//xlsx
var workbook = new XSSFWorkbook(uploadedFileContent);
var sheet = workbook.GetSheetAt(0);
}
return new JsonResult{};
}
So then I try to create a workbook using NPOI with uploaded Excel file, I get error: Exception details:
Message: The hyperlink for cell G33 references relation rId5, but that didn't exist!
I found out that this happens because G33 cell has a hyperlink, which is like this: xxx#yyy.com'. It has a single quote in the end. If I remove single quote, everything works as expected. Is there any other way to solve this problem rather that editing Excel file and removing single quote?
I tried using WorkbookFactory.Create and specifying second param as ImportOption.TextOnly. I tried creating OPCPackage object first and then passing it as a param. However, I get the same message.
As passed file to XSSFWorkbook constructor, corrupts the whole thing, I did not find any other solution then to switch to a different library.
I am using closedXML to generate an Excel file from C# and I am trying to see if there is anyway to set the zoom level of a worksheet. I can't find this in any of the documentation or from googling?
Does anybody know how to do this?
It's now possible, starting with ClosedXML version 0.87.0, via the IXLSheetView.ZoomScale property.
using Excel = ClosedXML.Excel;
var wb = new Excel.XLWorkbook();
Excel.IXLWorksheet ws = wb.AddWorksheet("zoom");
Excel.IXLSheetView view = ws.SheetView;
/* Value can be set between 10 and 400 */
view.ZoomScale = 85;
You can check the IXLSheetView source code for more information.
Update for version 0.87+: https://stackoverflow.com/a/52755386/2610249
No, ClosedXML does not support setting the zoom. The option that johny links to is only for scaling of the pages when printing.
There is a feature request on the ClosedXML page, but no answer from the developer.
As previously answered, you can't, but I've found a way around it which works well:
Create a template Excel file in advance, with all sheets' zoom levels set to how you want them.
When you create your workbook, instead of:
public XLWorkbook CreateWorkbook()
{
XLWorkbook workbook = new XLWorkbook();
IXLWorksheet worksheet = workbook.AddWorksheet("First sheet");
// ...
return workbook;
}
do this:
public XLWorkbook CreateWorkbookWithZoom()
{
XLWorkbook workbook = new XLWorkbook(#"C:\your template file.xlsx");
IXLWorksheet worksheet = workbook.Worksheet(1);
worksheet.Name = "First sheet";
// ...
return workbook;
}
where C:\your template file.xlsx is the path of your template file.
I think you can also handle having a variable number of sheets, by copying existing (blank) worksheets instead of creating new ones. You can get creative with having different blank template worksheets to choose from, if you need to set the zoom level dynamically.
A pull request for this has been logged at https://github.com/ClosedXML/ClosedXML/pull/180