I want to show an excel file inside my wpf window like this:
if I Create an excel application and then set Visible property to true, Excel application will open and I don't want this behavior.
Microsoft.Office.Interop.Excel.Application xlsxApp = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook = xlsxApp.Workbooks.Open(path);
xlsxApp.Visible=true; //Excel will open, I don't want this.
What you are looking for is Excel embedded in a Wpf Control. This page could be a good start. To sum it up, it must be included in your UI using ActiveX.
There is not pure WPF excel control per se.
If you need to mimic some excel features without the need to open an existing file, there are plenty of "excel like" controls available on the internet free or not. For instance this one.
Related
I am creating a web page to select data to download an Excel workbook (complete with macros), with that data filled in to the appropriate cells.
I was given a .vbs as a starting point for filling out an Excel workbook.
In the .vbs, the original programmer calls
Set objExcel = CreateObject("Excel.Application")
set ObjWorkbook =objExcel.Workbooks.Open(blankFile)
`...
objWorkbook.Saveas(outputFile)
objExcel.Quit
I'm trying to make a web page using C# and MVC, so I looked into Excel.Application and found this and this. I developed an action, just to test things out:
public FileResult filledOutWorkbook()
{
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
oXL = new Excel.Application();
oXL.Visible = false;
oWB = oXL.Workbooks.Open(Server.MapPath("~/Content/Worksheet2.xlsm"));
oSheet = oWB.Sheets["Information Sheet"];
oSheet.Range["V2"].Value2 = "customer test lol";
if (!Directory.Exists(Server.MapPath("~/App_Data/temp")))
{
Directory.CreateDirectory(Server.MapPath("~/App_Data/temp"));
}
var filename = Server.MapPath("~/App_Data/temp/") + DateTime.Now.ToString("o").Replace(":", "") + ".xlsm";
oWB.SaveCopyAs(filename);
oXL.Visible = false;
oXL.Quit();
return File(filename, "application/vnd.ms-excel.sheet.macroEnabled.12", "Worksheet.xlsm");
}
(I'm going to do a scheduled task or something to clear these out because I'm pretty sure I can't just get a byte stream of the file.)
The problem is, this doesn't close Excel. Apparently C# is not interacting with a library that understands Excel workbooks' formatting, you're automating Excel. I would install Excel on the production server, but when I tell it to quit Excel it doesn't quit. It asks the user if they want to save changes to the template document. I don't know how many times this page is going to get used but I think it's bad practice to have to click "no" however many times on the server to free up memory.
Can I circumvent this? Is there a better library than the COM object Microsoft.Office.Interop.Excel? Can I force Excel to close or is my best bet to write all the Excel interaction in VB and call that from the controller action?
As someone who has used and suffered with the Microsoft Excel Interoperability Library, I'm glad you are looking for an alternative.
Don't get me wrong. The interoperability library is extremely useful if you're trying to automate some simple task with small amounts of data. But it's ridiculously slow when it involves large amounts of data. And if you want this on a web server, you need to have Excel Installed on the server as well.
I suggest you use ClosedXML. It's open source and MIT licensed. It's basically a wrapper around OpenXML. It's also very easy to use, much faster and doesn't need Excel installed on the server
https://github.com/ClosedXML/ClosedXML
If it asks you to save, then it means it thinks the workbook has unsaved changes. Just close the workbook and don't save changes before quitting:
oWB.Close(false);
You might also need to close the first default workbook, in fact that might actually be the one that's causing the save message. Look for another workbook and close it without saving. Something like:
oXL.Workbooks[0].Close(false);
Or just enumerate the collection and close all of them.
It will depend on the actual state that Excel is in. If you're unsure of what to do, show Excel instead of closing it, and try to see what it is that it thinks is unsaved and decide if you want to save it or discard it.
You have two good answers here, but as an alternative, you could also tell Excel to suspend warnings. This is helpful any time you do something that would normally prompt a dialog, such as "Save As" on a file that already exists.
The DisplayAlerts property on the Excel object controls this and is true by default. Changing it to false will suspend alerts, so this should also work:
application.DisplayAlerts = false;
application.Quit();
application.DisplayAlerts = true;
I would suggest you take a different approach here. I would create a tab in the excel workbook where the user enters their choices that drive the data selection. Then write some script to use those settings to pull the data from a web service. See this article as an example: https://atinkerersnotebook.com/2012/12/28/creating-consuming-web-services-with-excel/
Now all you have to do is write the web service to return the requested data.
I want to suggest a bit of a different way to generate an excel file. For medium complexity worksheets it's much easier to use Report Designer to design the report, and then export it to an excel file. It can be generated to a stream, you don't even have to generate a file.
I am using SpreadsheetGear 2012 in my application to load, modify then save a new copy of a template workbook.
My code takes invoicing information from a database and formats it for the workbook. Then the user will print an invoice from the formatted information using a button on the workbook.
I use a template workbook with some formatting already provided to make my life easier. Included on the original template workbook is a button that runs a VBA macro in the spreadsheet. The VBA macro loads successfully but the button just disappears in the new workbook.
Some of the steps I have tried to rectify the issue: I've added a new button, changed the VBA macro code from a function to a sub, saved the template file as a macro-enabled spreadsheet (.xlsm) and saved the revised copy as a macro-enabled spreadsheet file.
Has anyone experienced this issue and do you have a solution?
If you are using the Open XML file formats (XLSX/XLSM), then this is a known limitation:
http://www.spreadsheetgear.com/support/help/spreadsheetgear.net.7.0/#SpreadsheetGear_2012_Limitations.html
In short, SpreadsheetGear 2012 does not support reading/writing Form Controls (like buttons), as well as Cell Comments, when working with the Open XML file formats. So your button is getting dropped when the file is initially read into SpreadsheetGear.
If you want to preserve these objects through SpreadsheetGear, you will need to use the XLS (FileFormat.Excel8) file format, which does support them.
folks,
Environment
Windows 8.1
Visual Studio 2013
C#
Issue
How do I write values and make charts on visible Excel sheets using NPOI (https://npoi.codeplex.com/).
Why do I want that?
I'm developing an application to measure temperature in an apparatus. To put together experimental data in one place, I'd like to record data on an Excel sheet and make a chart on the sheet. In addition, I'd like to keep the Excel sheet visible and check the chart updated in real time.
You could also make graphs on Windows Form apps with MeasurementStudio by NationalInstruments for example but considering the flexibility of Excel charts (size and xy range changeable, easy-to-use user interface, etc...), I'd like to stick to Excel.
You can easily do this with Microsoft.Office.Interop.Excel by
ExcelApp.visible = true;. However, this module requires users to release every COM object generated. Otherwise, the objects remain and eat up memory. This is the reason I prefer to use NPOI.
How can I achieve this? Any answers would be appreciated.
You cannot do this with NPOI. NPOI reads and writes data from serialized Excel files. You cannot access those files while Excel has them open, and even if you could, Excel simply wouldn't re-read the files so your modifications wouldn't show up.
The problem you describe comes down to "I want to interact with a running Excel instance without using Excel interop". That's not going to work.
I am creating an Excel file in an WinForm app (VS2008) in runtime. I am exporting some data into it from datagridview. After export, i would like to show this file in excel, but dont want to save anywhere. My goal is to use it as an alternative of creating many reports in crystal. Is it possible ?
If you are using Interop Service, you only can make workbook application visible equals to true.
[workbookobject].Application.Visible = true;
I have a c# application that generates an Excel workbook. The problem is that when you open the workbook and click on the "Insert" menu, "Chart" is disabled. Any idea on how to enable this option when creating the workbook in c#?
I write this part taken from here.
Hope this helps...
Sounds like you have the setting to
not display objects on the sheet (a
chart is an object).
You probably
can't add a rectangle, either!
Find
the option in Office/Excel Options.
Or easier, press ctrl/6 (not F6, 6).
Bob Umlas
Excel MVP
EDITED:
Another solution taken from here.
Yes, the Shared workbook option will
grey out the chart wizard.
You would
have to remove the sharing to get the
wizard back in that workbook.