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;
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.
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 have a problem, i need to show a group of excel files in some views of an mvc4 project. The real problem is that the files are Excel PowerPivot and they can not be shown as images or pdf, it has to be Excel files.
Any ideas?
Thanks for the help...
If you can open the workbook in the Browser-based version of excel and see what you need to display, you can then use the Share->Embed feature of the Excel online app to generate HTML or JavaScript embed code.
If you need to have the end user manipulate the workbook to retrieve the necessary data from the PowerPivot table, then they'd most likely be better off downloading the workbook and going from that.
If they don't need to manipulate anything, look into if you can export the workbook to HTML in some fashion and embed that.
Hopefully that might put you down the right track.
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.
There is some code/werbparts on our site that display a lot of data in a grid on our site. In addition, they have another page with a few charts.
I need a way to create functionality to export this to excel so the user can click a button and save everything.
The chart will actually have to be built in excel, I can't just grab the picture from the website and display it.
Also, the data from the grid will have to be put into a spreadhseet. I am not as worried about this part.
I have exported to excel before, but it was just basically printing some data which was tab delimited with a certain doc type .
This actually will need to use different work sheets and use some excel functionality.
(I need a better answer than use excel services)
Thanks!
Use Epplus is a third party library for creating and manipulating excel files(xlsx)
Dunno about sharepoint, but the OpenXML SDK might be what you're after:-
http://msdn.microsoft.com/en-us/library/bb448854.aspx