I am using Microsoft.Office.Interop.Excel in a C# winform but I can't figure out how to create a new Excel workbook. I have tried everything in the documentation but I get nothing but errors. Workbook wb = new Workbook() compiles just fine but throws an incomprehensible runtime error.
You are probably getting the same error as you would get if you tried to instantiate a Workbook in Excel VBA using
Dim w As Workbook
Set w = New Workbook
Try instantiating the Application object, then call the Application.Workbooks.Add method. It takes an optional parameter, which is the name of a template.
Related
I have a powerpoint slide in which an embedded excel object is present, which is embedded in the following way: insert-object-create from file-browse-display as icon.
I'm trying to open this excel sheet programmatically using c#.
The problem I'm facing is that I'm receiving the error:
"Unhandled Exception: System.Runtime.InteropServices.COMException: OLEFormat (unknown member) : Invalid request. The window must be in slide or notes view.
at Microsoft.Office.Interop.PowerPoint.OLEFormat.Activate()"
I have tried changing the viewtype to slide as follows:
presentation.Application.ActiveWindow.ViewType = PowerPoint.PpViewType.ppViewSlide;
But this doesn't seem to help and I'm receiving the same error.
`
Microsoft.Office.Interop.PowerPoint.Application PowerPoint_App = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentations multi_presentations = PowerPoint_App.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation presentation = multi_presentations.Open(#"D:\test.pptx");
foreach (var item in presentation.Slides[1].Shapes)
{
var shape = (PowerPoint.Shape)item;
if(shape.Name.Contains("Object")) {
presentation.Application.ActiveWindow.ViewType = PowerPoint.PpViewType.ppViewSlide;
presentation.Application.Activate();
presentation.Application.ActiveWindow.Activate();
shape.OLEFormat.Activate();
Microsoft.Office.Interop.Excel.Workbook wb = (Excel.Workbook)shape.OLEFormat.Object;
}
}`
This is the code which I'm using.
Could anyone please help me on this.
Thanks in advance!
It makes the job simple while working with clean presentations using Office.Interop lib. But it gets tricky when we try to access and edit OLEObjects in the presentation.
Try this!:
PowerPoint.OLEFormat ole = shape.OLEFormat;
ole.Object.Activate();
Excel.Workbook workbook = ole.Object;
this will create a workbook instance.
Note: OLEFormat.Object is dynamic type, no need of type cast the object. but yes you need handle Error when the type cast fails.
Hope this helps.
Thanks.
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.
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 am developing an Excel Addin that is able to fetch data from our database.Instead of dumping the data directly , I would like to have the data presented in a nice format. I have got our local VBA expert to develop a templates for the same since there can be multiple sheets all of which can have different formats.
It is while adding the worksheet that I am stuck at.
string TemplateFileLocation = Path.GetFullPath(fileName);
if(File.Exists(fileName))
{
Worksheet newWorkSheet = (WorkSheet)Globals.ThisAddin.Aplication.Worksheets.Add(Missing.Value,Missing.Value,1,TemplateFileLocation);
}
The code crashes while it hits this location.
The Error code returned is : 0x800A03EC
I have verified that the path to the template file is correct.
Each of these templates have only one work sheet.
You can use EPPlus
using System.IO;
using System.Reflection;
using OfficeOpenXml;
//Create a stream of .xlsx file contained within my project using reflection
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("EPPlusTest.templates.VendorTemplate.xlsx");
//EPPlusTest = Namespace/Project
//templates = folder
//VendorTemplate.xlsx = file
//ExcelPackage has a constructor that only requires a stream.
ExcelPackage pck = new OfficeOpenXml.ExcelPackage(stream);
I've tried creating a new Excel file with NPOI and it works OK, reading (only) an Excel file is also OK. However now I want to open an existing Excel file and append some rows on it, it's crashed right at the code line of the NPOI.HSSF.UserModel.HSSFWorkbook constructor. Here is the code:
using(FileStream fs = new FileStream(myFile, FileMode.Append)){
HSSFWorkbook wb = new HSSFWorkbook(fs); //<-- It is crashed right at here
....
}
There is no exception detail, it just shows a window saying that the app is crashed and give 3 options to choose, the middle one is to close the application:
snapshot
That's why I can't understand and feel hard to solve it.
If I change the FileMode to FileMode.Open, the constructor can work well, but that's just for reading. I think there is some thing related to FileAccess policy and tried this:
using(FileStream fs = new FileStream(myFile, FileMode.Append, FileAccess.ReadWrite)){//<-- However it is crashed right at here
HSSFWorkbook wb = new HSSFWorkbook(fs);
....
}
I've tried a workaround by opening (read only) the existing Excel file, copy 1 sheet of it and add this sheet to a new HSSFWorkbook and write (write only) to a new file stream, however there is no way to do that, because the HSSFWorkbook has no collection of Worksheet and there is also no Add method, the new sheet is just created by CreateSheet() method. It's so annoying.
Could you please help me with this? I'm trying NPOI first, then I'll try EPPlus.Your help would be highly appreciated.Thanks.
What's your file format? xls or xlsx? If xlsx, you can try NPOI 2.0 now. xlsx is supported.
Here is the download link: https://npoi.codeplex.com/releases/view/112932