I have an excel sheet with a complex formula applied in it. I want to use this formula from my C# code for some calculations. My scenario is like this User fill a form (web) and submit, our program will fetch these values and applied in the hosted excel sheet and get the result.
is this possible?
Help is highly appreciable.
Thanks,
This really should go in the comments section, as I have a few questions to clarify your request. However, as I do not yet have the reputation, my questions must go here.
Is it a formula residing in your existing Excel document that you want to utilize that formula/document without re-writing the code?
Or are you asking if it is possible to write the same formula that you have working on the Excel sheet in c#?
If the former, it is conceivable to pass data into the Excel sheet from the code. After all we can use code to append to other document types, why not an Excel document? And if we can fetch specific lines from other document types, why not from an Excel document (especially since the grid/cell layout provides easier addressable points). However, while it is conceivable, doesn't mean it's easy. It's an interesting proposition, and I'd like to do some experiments on that.
If it is the latter, then depending on the formula's requirements, you may just need to use the appropriate math libraries and write out the formula directly in the c# code. You may not be able to visually see how everything ties together as nicely in the Excel document (with the referenced cells highlighted), but you could modularize components of the formula more easily as sub-functions and re-use values easily enough with variables.
Related
The excel file has super long formulas and rather than convert them to C# I would prefer to just be able to read from it the result cell after I input values in the calculations cell via the app which would be written in C#
To partially answer your question, If you're not familiar with using the Interop library within C# I suggest you check out this link for basic implementation.
http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm
Then, once you've got your app set up to manipulate excel files check out this one for how to read a value from a cell:
How to read single Excel cell value
HOWEVER, depending on how large your excel file may end up being, it may be a good idea to just go ahead and convert the formulae into C#. Taking a little bit of extra time on that will save you tons of time while waiting for excel to execute statements, as I believe it runs off of/in conjunction with VBA(which is an interpreted language as opposed to C# being a compiled language)
I need to make the information in the database usable by allowing the user to download it as a PDF or Excel spreadsheet (either one works, both is perfect).
I've looked around at a bunch of options, but I really can't decide which one I should use, let alone if any of those options are actually useful. Most of the options I've found revolve around converting already existing HTML files into PDFs which is not what I need. Also, it needs to be free. My bosses haven't given me a budget to spend on this
I'm not sure what other information I should include here.
Well, any help is greatly appreciated. If you have questions about missing information, I'll get it posted ASAP. I'm here all day, so I'll be able to respond to any comments very quickly.
EDIT: Oh wow! Huge thanks, guys, for the massive response! I got a ton of ideas. This is super-helpful. Thanks!
if you want to generate an Excel (or also a Word) you can use openXml. You can create a new document exactly the way you want from pure code.
OpenXml SDK page
The solution I usually propose to my clients in this situation is to use Sql Server Reporting Services (SSRS). You can use the ReportViewer control included with it in order to generate PDF's, Excel spreadsheets, XML files, CSV files, and others. If you need ad hoc reporting, there is a Report Builder available as well.
Barring that, you can use OpenXml to generate Excel spreadsheets and there are a host of PDF toolkits available.
Have you looked into the reportviewer control, which is part of Visual Studio?
It allows you to export the report in PDF or Excel format.
http://www.carlosag.net/tools/excelxmlwriter/sample
check this might be useful for you
There are lots of reporting solutions out there such as SQL Server Reporting Services(for which you might already have a license). Take a look at Reporting (free || open source) Alternatives to Crystal Reports in Winforms which can likely be applied to the web with a bit of serialization.
I would suggest thinking about rolling your own depending on the situation. You could use pdfsharp for the pdf export and EPPlus for excel. They are both very easy to use and, I'm pretty sure, available in nuget with a couple of clicks.
If you want to go the Excel route, i'd recommend this article from Stephen Walther entitled ASP.NET MVC Tip #2 - Create a custom Action Result that returns Microsoft Excel Documents. This uses an old trick of writing an HTML document with an Excel mime type. This is different than streaming a native Excel file. And it's fairly easy to change the to rendering a CSV file if you want to strip it down, and make it a more universal file. Just remember to double-quote all the fields if there's a possibility of commas showing up.
If what your doing isn't too complicated you can use CSV files. CSV stands for comma separated values, and it is what it sounds like. You can create simple tables and columns using commas. For example paste the following lines into a text file:
heading1,heading2,heading3
info1,info2,info3
info1,info2,info3
Save the text file as a .csv file and voila - an excel spreadsheet. Obviously it is extremely easy to build these looping object collections. Mind you if you need any complicated text formatting etc then it is not really the best option.
I have a few pdf files that were created from word or excel files.
I need to get the information thats in the tables.
The text in the document is not an image so I'm able to extract the text using tools such as pdfbox.
When I have the text I have no way of knowing what cells in the table it belongs to because I don't know where the table borders are.
Iv'e tried a few desktop tools such as abby or solid pdf converter and they are able to convert the files into nice word documents but this doesn't suit my needs as I want to be able to do this programatticly in C#.
Some of the tables have nested tables wich I think makes this a little bit more diffucult.
I appreciate your help
The difficulty here is caused by the fact that the text in the PDF is not contained within any table. It might look like it is, but underneath the surface, it is not.
So there are a couple of options that I can think of. But none of them are going to be quite as satisfying as you'd probably like.
There are some companies that offer SDKs for PDF to Excel/Word conversion. Investintech and Iceni are a couple of examples. But these solutions are not free.
If you know the exact layout of the PDF files that you need to extract the table data from, then you can use any SDK that lets you extract text from a PDF and also tells you the exact co-ordinates of the extracted text. Using this method you need to know in advance where the text is going to be, so that you can extract text from a specific area on the page. It obviously won't work if you need to process any random document.
It's a difficult task, but hopefully this will give you a starting point.
I am required to produce a somewhat complicated excel workbook containing over 30 worksheets, each using information from several database tables to dynamically draw several tables per sheet, with formating. I have used c# in the past although I am unfamilar with its use in Excel. Any recomendations regarding to how to format and posistion cells dynamically from a database would be a great help.
Thanks in advance.
Visual Studio Tools for Office (VSTO) is your friend :) I use this all the time to build complex worksheets. This is way, way better than importing from CSV or using some kind of wacky DB connectivity.
Here are some links
MSDN
Blog
Wikipedia
This is very easy to do, though there are a few oddities. I find it very effective to use the Excell MACRO recorder to record actions, then look at the source and re-impliment in C#. The object model is exactly the same between VB and C#.
VSTO is part of Visual Studio 2008. I looked on the product page, but I cannot determine if it comes with VS2008 C# Express Edition. The easiest thin to do is just download it and see :)
Your question sounds like you already understand the technology involved, so this answer is restricted to formatting & layout.
I did something very similar 10 years ago. It's been a while, but here's what I figured out.
Put all calculated & summary info on top. (including 'bottom line' totals)
Base charts on the totaled cells at the top of the sheets since they don't move.
Put all charts, summaries, etc on their own sheet.
If there are calculations involved, make each step of the process available so the user can step through them. This will prevent you spending countless hours proving your code calculates according to spec, if you don't do this, your 'black box' calculations will be questioned constantly.
If they don't want to see the raw data, only the calculations, then consider pushing it out to a hidden sheet.
Store a template workbook with your app, then just copy it so you can do all formatting in XL, not code.
One thing you really want to avoid is keeping track of different cell locations. You want to push values into cells which are always in the same location.
I find a very effective and often underused feature in Excel is the Web Query.
If you can present your data in a website, you can pre-build your Excel workbook with web queries set up to gather the data from the website on demand, which is presented in the form of tables in appropriate web pages.
You can add query string parameters for passing arguments to the web page and you can set auto-refresh, formatting preservation, etc. PivotTables/Charts can refer to the data and can made up to update upon data refresh with a few lines of VBA, for example.
This is often a much simpler solution than VSTO if it can meet the requirements.
Another option would be to use a third party Excel file generation assembly.
I'm in need of a solution to print or export (pdf/doc) from C#. I want to be able to design a template with place holders, bind an object (or xml) to this template, and get out a finished document.
I'm not really sure if this is a reporting solution or not.
I also don't want to have to roll my own printing / graphics code -- I'd like all display concerns handled in a template.
I initially think of this as something Crystal Reports can do (although I've never used CR), but I'm not sure if I'm abusing the system here -- I'm not really interested in binding ADO.NET datasets at the moment (screw datasets). Can Crystal deal with binding to objects?
Does SSRS or WPF play in this field too?
A subset of WPF-P is XPS which can be used to present your objects via databinding.
One of the best choices if you are already using WPF.
Google Keywords: XPS, FixedDocument, FlowDocument, WPF Printing
Might read through this thread:
http://groups.google.com/group/nhusers/browse_thread/thread/e2c2b8f834ae7ea8
Seems a lot of people like iTextSharp
http://itextsharp.sourceforge.net/
For Word docs, look into Word's Mail Merge feature and Word automation. I did this recently in a form letter printing project. Basically what I did was create a Word template file (file extension .dot) and in this template file I defined MergeFields in a standard form letter. My application queries a database for the records it needs to print and then for each record it returns it matches fields in the database with these merge fields and sends the result (the merged doc) to the printer.
It's working really well and if I had a link that gave a definitive explanation, I'd provide it (check back here, I'll see if I can't find the most useful ones). Hopefully I've provided enough keywords to let you find your own resources. I can go into more detail if you need.
I've never had to export PDF files but for a project I'm working on now I'll have to. For a free solution my research has lead to iTextSharp (like Will Shaver points out) but I've only done the initial investigations and I have found a few pay solutions I might end up resorting to.