Populate excel from epplus on windows app - c#

I have a windows app that opens excel. The user can then decide to save it.
This uses Interop and is very slow when we have over 100 rows. I'm experimenting with EPPlus and see how to save directly to a file. But I don't want to do that, I need to open Excel from the app. So I'm thinking to write out the worksheet using EPPlus (thus saving time) and then copying the whole worksheet into Excel and open it. Does that make sense? Are there examples of this?

First off, I've used EPPlus and it's a great tool.
I'm a little confused by your question, so let me ask some clarifying questions.
Why are you launching excel from your app? Is it to edit some kind of file your program generates, or just random files the user wants to edit?
Once the user is finished editing the file, do you need to read it back into your app? If so, that can also be done with EPPlus.
Once I know more about what exactly you're trying to do, I'll be glad to help you.
--John

Related

How to update data in excel File using C#?

This Question might be repeated, But I couldn't get solution regarding my problem so far. I'm new to Interop. I'm using excel file (as a database).
Here is data presentation in excel file
in my data If Card ID repeated then I need to increment '1' in Counter in the same row, similarly I need to fetch IP address of same row..
I'm using Interop Excel approach to insert data in excel file..
Kindly tell me how can I perform that update operation to that excel file through C# (WPF)
Sorry for bad English..
Thanks
I recommend using Closed XML
You write to the file directly and don't need Excel. It will need to be the latest version of an Excel file to work (The open xml standard).
Epplus.dll or npoi.dll will also read/write to excel files w/o excel.
Save the data in an XML or JSON file, then when you want to visualize them you create the excel file from these data, so you will have a very light file and easy to read and update if you wish.
I haven't done this specifically through wpf, but you can access powershell cmdlets through .net and powershell has commands for retrieving and writing Excel data.
That said, my experience has been it's very tedious and inconsistent with bugs. I would tell your client that using an Excel file as a database is impossible and certainly prone to failure in practice.
For one thing you will run into read/write restrictions if it is used by anything else.
If you don't mind to use comercial libraries, you can try to use Aspose.Cells. It has rich cells API and able to work without Excel interop API.

Prompted to Save Changes on file created with EPPlus

I am creating a series of Excel Workbooks using EPPlus v3.1.3. When I open the newly created files, if I close it without touching anything it asks me if I want to save my changes. The only thing I've noticed changes if I say "yes" is that the app.xml file is slightly altered - there is no visible difference in the workbook, and the rest of the XML files are the same. I have tried both of these approaches:
ExcelPackage p = new ExcelPackage(new FileInfo(filename));
p.Save();
as well as
ExcelPackage p = new ExcelPackage();
p.SaveAs(new FileInfo(filename));
and both have the same problem. Is there a way to have the app.xml file output in its final form?
The reason this is an issue is because we use a SAS program to QC, and when the SAS program opens the files as they have been directly output from the EPPlus program it doesn't pick up the values from cells that have formulas in them. If it is opened and "yes" is chosen for "do you want to save changes", it works fine. However, as we are creating several hundred of these, that is not practical.
Also, I am using a template. The template appears normal.
What is particularly strange is that we have been using this system for well over a year, and this is the first time we have encountered this issue.
Is there any way around this? On either the C# or SAS side?
What you are seeing is not unusual actually. Epplus does not actually generate a full XLSX file - rather it creates the raw XML content (all office 2007 document formats are xml-based) and places it in the zip file which is renamed to XLSX. Since it has not been ran through the Excel engine it has not be fully formatted to excels liking.
If it is a simple data sheet then chances are Excel does not have to do much calculation - just basic formatting. So in that case it will not prompt you to save. But even then if you do you will see it change the XLSX file a little. If you really want to see what it is doing behind the scenes rename the file to .zip and look at the xml files inside before and after.
The problem you are running in to is because it is not just a simple table export Excel has to run calculations when opened for the first time. This could be many things - formulas, autofilters, auto column/row height adustments, outlining, etc. Basically, anything that will make the sheet look a little "different" after excel gets done with it.
Unfortunately, there is no easy fix for this. Running it through excel's DOM somehow would be simplest which of course defeats the purpose of using EPPlus. The other thing you could do is see the difference between the before and after of the xml files (and there are a bunch in there you would have to look at) and mimic what excel would change/add in the "after" file version by manually editing the XML content. This is not a very pretty option depending on how extensive the changes would be. You can see how I have done it in other situations here:
Create Pivot Table Filters With EPPLUS
Adding a specific autofilter on a column
Set Gridline Color Using EPPlus?
I ran into this same issue using EPPlus (version 4.1.0, fyi) and found adding the following code before closing fixed the problem:
p.Workbook.Calculate();
p.Workbook.FullCalcOnLoad = false;

Excel to Powerpoint - looking for a helpful nudge

Good Morning,
I have a C# agent which runs periodically and updates certain values in a particularly important spreadsheet, the reason that this spreadsheet is updated is that periodically someone will manually go into this .xls file and print screen the worksheets and paste them in to a Powerpoint presentation template as images.
These 'images' aren't charts, nor tables, simply ranges of cells that are coloured etc in the spreadsheet - and what I'm looking to do is basically automate this by customising my agent so that everytime it updates the spreadsheet, it 'print screens' a certain range that I specify and copies it as an image into the .ppt file.
I appreciate this question lacks a code example, and I'm not expecting someone to 'do it for me', any advice or pointers on how I might accomplish this would be much appreciated.
Also VSTO is not an option unfortunately (work environment).
Many Thanks
You may not have the ability to control how other people create their PowerPoint slides, but if they want a specific range of cells to update to match what the excel sheet is currently, they can Ctrl-C the section then Paste Special.
I know your question asked about automating a print screen capture of the cell range, but would this work for you? Or must there be no possibility of an accidental update, or some reason it must be an image?
The linked section will automatically update if the file is open and if it isn't it will ask if you want to update the links on opening the PowerPoint. Or right-click on the object in the slide and update link.
I've been doing it too reccently.
It might give you an idea.
Put an Alternative.Text to your shape in PowerPoint, it might help you place your new image in the correct place.
C# Paste HTML to Excel or PowerPoint
Showing HTML in PowerPoint

making ax excel ribbon that passes spread sheet columns to another program/API

I am new to C# and VS. I am working on a problem in which I need to make an excel ribbon, that provides me with the functionality of passing column values in a sheet to another program that we have developed that does data visualization. I need some ideas on how to get started and how will it all work. For the same I needed some guidance. Any help on how to proceed will be appreciated.
Thanks.
Arun
I would think you'll have to make your visualization program able to start with one or two parameters. I would have the excel plugin save the selection to the temp folder in whichever format your visualization program uses, and do a process.start() on your visualization program, with an argument pointing at the just stored data. From there on your can read the stored data in the temp folder and display it.
Perhaps you could use copy and paste via the operating system clipboard?

Export DataTable to Excel with C# from ASP.NET

I have an ASP.NET web forms app with a button on it. This button allows a user to download some content to Excel. However, I'm having problems getting it to work. Everyone has articles that are like export DataGrid or GridView to Excel. My problem is, I have a plain old DataTable. I can't put this DataTable in my UI. At the same time, I do not want to use the Excel libraries.
Does anyone know a way to export a DataTable to Excel with formulas?
You can use ClosedXML library (an OpenXML wrapper) to create any Excel you want dynamically.
Office Writer is a very powerful and equally expensive solution which I have used in the past. ClosedXML in Peters answer also looks good (and free).
If you don't need Formulas this answer to another SO question may help you out. No library required.

Categories