C# Excel Number Format error - c#

I'm having troubles setting columns format in Excel with Interop. I'm using C#, below is the code:
Excelop.Range rg = (Excelop.Range)wb.Worksheets[1].Cells[1, i];
rg.NumberFormat = "#"; //Exception is raised here
I get the below exception when compiled:
"An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
Additional information: Unable to set the NumberFormat property of the Range class"
Appreciate your support.

My guess is that your worksheet is protected.
You likely need to do something like:
wb.Worksheets[1].Unprotect (Password:"your_password")
These may be of assistance:
https://www.mrexcel.com/forum/excel-questions/449829-run-time-error-1004-unable-set-numberformat-property-range-class.html
"Unable to set the NumberFormat property of the Range class"

Related

Creating a Name Range in Excel using NetOffice throws a COMException

I would like to create a Named Range in Excel from a NetOffice, but I cannot figure out how to invoke the method. Whatever the input, the underlying Excel Object throws a COMException with a generic HRESULT 0x800A03EC Error.
Excel version Office 365, 16.0.13001, 64 bits
NetOfficeFw package, version 1.7.4.11
I have no hint about what is wrong (syntax? Excel version? NetOffice version?).Is there a known bug on this method?
Thanks for your help!
private void CreateNameRange(Workbook wb, Range range)
{
try
{
Name existingName = (Name)wb.Names.FirstOrDefault(); //works fine if a name already exists in Excel
wb.Names.Add(); //throws
wb.Names.Add("name1"); //throws
wb.Names.Add("name1", range); //throws
wb.Names.Add("name1", range.Address); //throws
wb.Names.Add("name1", "F7"); //throws
wb.Names.Add("myName", "=Sheet1!$F$7:$I$13"); //throws
wb.Names.Add("myName", "'=Sheet1!$F$7:$I$13'"); //throws
if (_wb.ActiveSheet is Worksheet ws) {
ws.Names.Add("myname", "=Sheet1!$F$7:$I$13"); //throws as well
}
}
catch (COMException e)
{
//always ends up here with Inner Exception : COMException (HRESULT 0x800A03EC)
}
}
Actually, the root cause of this issue was not the method itself, but when the method was called.
I was trying to create Named Ranges from within a UDF Function but Excel considers this operation as unauthorized, along with other worksheet/data manipulation when calculation is being made, and that's why the COM Exception was raised.
Bottom line : Named Ranges cannot be created when a workbook calculation is in progress.

System.ArgumentNullException in mscorlib.dll

TextWriter textWriter = new StreamWriter(#System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
when I run this, I am getting the following error.
Exception thrown: 'System.ArgumentNullException' in mscorlib.dll An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll Value cannot be null.
How to fix it?
Environment Variable OUTPUT_PATH is not set.
System.Environment.GetEnvironmentVariable("OUTPUT_PATH")
This statement is returning null.
Please check environment variables to confirm if path is correctly set or not.
If path is there, then there may be access privilege issue. Your code may not have permissions to read the environment variable.

C#, EPPLUS - cannot save_as excel another time

we just have a excelpackage object given, which was saved using SAVEAS(file) correctly. But when I try to save_as another time (after re-arranging worksheets), then it will be thrown this exception:
An unhandled exception of type 'System.InvalidOperationException'
occurred in EPPlus.dll
Error saving file (report.xlsx)
Already using the latest version 4.0.5 EPPLUS.
Also tried the other post without luck:
var reportFileInfo = new FileInfo(reportFile);
if (reportFileInfo.Exists)
reportFileInfo.Delete();
ExcelReportStream.SetLength(0);
ExcelReport.Stream.Position = 0;
ExcelReport.Stream.CopyTo(ExcelReportStream);
using (var excelToSave = new ExcelPackage())
{
excelToSave.Load(ExcelReportStream);
excelToSave.SaveAs(reportFileInfo); <<<<< EXCEPTION again!
}
An unhandled exception of type 'System.InvalidOperationException'
occurred in EPPlus.dll
Error saving file (report.xlsx)
The ExcelPackage is a property and with the first try, it will be ExcelPackage.SaveAs(reportFile) works on first time. But not on second.
No further information from the compiler...
Any ideas?
Thank you.

Getting start with ArangoDB and .Net - Exception thrown by AddConnection()

I am hoping that someone who has worked with ArangoDB and .Net and C# can help me out.
When I call ArangoClient.AddConnection() an exception gets thrown (of type 'System.ArgumentException' occurred in mscorlib.dll). The message is "An item with the same key has already been added."
The call to ArangoClient is: ArangoClient.AddConnection("127.0.0.1", 8529, false, "NancyTest", "NancyTest", "root");
Any ideas?
You most probably already created a connection with specified alias. With a driver version 0.9.0 and higher you can check if the specified alias already exists through ASettings.HasConnection(string alias) static method.

Emgu.CV Not supported Uri format

I have some program on my 1st pc (everytinh works fine)
but when I copy my full project to another PC and try to run application I get this error :
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Formaty identyfikatorów URI nie są obsługiwane.
If there is a handler for this exception, the program may be safely continued.
and than:
A first chance exception of type 'System.BadImageFormatException' occurred in Emgu.CV.dll
Additional information: Próbowano załadować program w niepoprawnym formacie. (Wyjątek od HRESULT: 0x8007000B)
If there is a handler for this exception, the program may be safely continued.
in this code:
BackgroundSubtractorMOG2 pMog11 = new BackgroundSubtractorMOG2(0, 80, false);
I have really have no idea what to do with this. What is the problem with Emgu.CV in the 2nd pc?

Categories

Resources