Hi Stackoverflow Users
I was recently assigned to rewrite a excel document my company uses a lot. Originally it was written in matlab, but to easier distribute it, they migrated it to excel. It was done very bad, and therefore it worked on some computers and not on others. So it was rewritten to vba. But now they want it to be able to do very complex calculations like Monte Carlo simulations. therefore we wish to write it in c#.net for performance and development reasons. the last week i have tried and tried to learn how it was best done. I found several possible solutions, but i have not found a single way to do it which meets my 2 simple demands:
The user doesn't have to go to any settings and add something, if it have to be done, it need to be done in code
The calculations is done at the press of a button, so i need to be able to receive the event
I have spent so much time on the web, but i don't know exactly what to search for, so it all just seems like a big wall of unexplained, complex com-magic.
So what are your suggestion? Is there any solution which meet those demands? Any suggestions, references or any other help will be deeply appreciated.
Excel doesn't natively support C# in macros, so you're not going to be able to meet your first demand in a stand-alone spreadsheet. If you're willing to install an add-on into Excel, you might want to consider Excel-DNA. This would allow you to compile a .NET DLL containing the functions you want to add in to Excel.
Receiving an event from a button click is simply a case of adding a button control onto a sheet, and assigning a macro to it. This macro (e.g. a VBA function) will be run on demand when you click the button.
Related
We have a couple of spreadsheets here that we populate with some data and do some formatting using Microsoft.Office.Interop.Excel.
Now that part is all good - it's something along the lines of what is done here: Write to Excel example.
What I'd like to do is:
Add a button inside the spreadsheet, bound to a macro.
When the user clicks the button, the macro fires - we grab some parameters from the cells and pass those to the C# function. That code does all the heavy lifting, populates the spreadsheet as above, etc.
What I don't understand how to do is that second step - I can't see a way to actually pass parameters and call into C# directly.
As it stands now, I can call a C# console application and pass command line parameters into it... but that seems a bit wrong, I'd have thought there would be some better way - but I can't seem to find it!
Coopernick,
It looks like you need to develop an Excel add-in and move the VBA code on the add-in rails. See Walkthrough: Creating Your First Application-Level Add-in for Excel to get started quickly. You may consider an Excel add-in as a regular .Net application where you can use any components and libraries.
You may find the Excel Solutions section in MSDN helpful.
I am currently developing a Word-Completion application in C# and after getting the UI up and running, keyboard hooks set, and other things of that nature, I came to the realization that I need a WordList. The only issue is, I cant seem to find one with the appropriate information. I also don't want to spend an entire week formatting and gathering a WordList by hand.
The information I want is something like "TheWord, The definition, verb/etc."
So, it hit me. Why not download a basic word list with nothing but words(Already did this; there are about 109,523 words), write a program that iterates through every word, connects to the internet, retrieves the data(definition etc) from some arbitrary site, and creates XML data from said information. It could be 100% automated, and I would only have to wait for maybe an hour depending on my internet connection speed.
This however, brought me to a few questions.
How should I connect to a site to look up these words? << This my actual question.
How would I read this information from the website?
Would I piss off my ISP or the website for that matter?
Is this a really bad idea? Lol.
How do you guys think I should go about this?
EDIT
Someone noticed that Dictionary.com uses the word as a suffix in the url. This will make it easy to iterate through the word file. I also see that the webpage is stored in XHTML(Or maybe just HTML). Here is the source for the Word "Cat". http://pastebin.com/hjZj6AC1
For what you marked as your actual question - you just need to download the data from the website and find what you need.
A great tool for this is CsQuery which allows you to use jquery selectors.
You could do something like this:
var dom = CQ.CreateFromUrl("http://www.jquery.com");
string definition = dom.Select(".definitionDiv").Text();
I need to create a simple application that would generate reports based on database data and some user input. I have vast C/C++ experience, but for this project we had to choose C#.
I want to avoid using 3rd-party components that would require installation on a client machine as much as possible. Thus, I would like to avoid using even the features provided by MS Office (I could have used an XLS template, just like my predecessor did, but it doesn't look like a way to go; anyways, my predecessor's sources are long lost, which is probably good, since it is a roughly 10-years-old VB monstrosity that crashes every now and then). The same goes for crystal reports and whatnot.
My current idea is to build HTML tables (using an HtmlDocument) on the fly, to present them to the user (using a WebBrowser) and to print them. Is it a right way to go? Are there any simpler/more robust/better approaches? What are the possible pitfalls?
Building tables on the fly would work. I use that technique every so often, but it seems like a hack to me. I only use it because quite often, the business need requires "good enough" and quick turnaround as opposed to "beautiful".
I'm going to preference what I'm about to suggest bysaying that I have a strong bias against 3rd-party components myself.
I flat out refuse to use them in most cases and will go out of my way to come up with another solution, because I've been burned on upgrades/licensing too often.
That said...
Fortunately, with .NET not all 3rd party components need to be installed to your clients PCs. With XCOPY deployment, often you can just reference a .DLL or a project in the solution ans specify the "Copy Always" or "Copy if newer" option to just include the dll (or resulting dll if you're referencing a class library project) and as long as the .dll is present in the same directory as the executable, the 3rd party component works.
With all of that in mind, there's a project that I've used to print a DataGridView from Windows Forms at CodeProject. This is one that you can get the source for, reference it, and use it without having to actually install anything at the client. I've used it in more than one app, and it's one of my favorite tools.
It will print ONLY the DataGridView, but it prints it exactly as it appears on screen, so if that's what you want, I'd recommend at least checking it out.
As a third option, you could consider using ASP.NET. If you're going to be generating HTML, it's just as easy (easier actually) to do it using ASP.NET than in a WinForms app. Using ASP.NET you get Repeaters, ListViews, etc, all of which make the reports easy to create.
I've got more than one real-world app that is primarily a WinForms app but has an associated reporting site.
For example, I have a WinForms app used for scanning coupons accepted at our retail locaitons. It's a WInForms app because I need to interact directly with the scanner on a COM port. However, for the reporting portion, I set up an ASP.NET website. In the WinForms app it's very simple to create a reporting menu option and point to the pages. Our users generally don't even think about the fact that this is two distinct applicaitons. They see it as one - their Coupon Scanning app.
The point of all that is that it's usually possible to do things with any given tool, but it's far easier to use the tool that is meant for the job. If you're going to be generating HTML reports, ASP.NET is a better tool than building the reports manually, and if you need to have a WinForms app for most of the UI, you can still do that and use the website for the reports - using the best tool for each task.
Maybe you would like to check RazorEngine, to create your HTML templates.
I recommend you Razor engine to render html, it is more flexible than working with HtmlDocument objects.
You can create a file with your report template, and then parse it sending a Collection of your report rows objects.
I'm trying to create create a simple word template on the fly which would just contain text and a few text input fields and then open a new word document based on that template for the user to fill out. I don't think this would be too difficult using Word automation and C#, but I've never worked with Word's COM interface before.
Any links to examples or fresh examples on how to create a simple template with some text and a text input field and then how to open a document based on that template in C# would be greatly appreciated.
Thanks in advance!
I think "Office Interop" is the keyword you are looking for.
You'll find a starting point here: http://support.microsoft.com/kb/316384
Several drawbacks involved (for instance, installed Office on client machine required; not advisable to be run as a server process), but might do the job for your requirement.
I have done a fair amount of work with Word automation - though not really so much with tempates... My advice would be first of all to have a look at the documentation that ships with the product - which is a simple matter of editing a word macro, and pushing F1 in the code editor. I have found that to be a rich source of information...
The other piece of advice I would give you is to try recording a macro in Word that covers any task you want to do, and then inspecting the code that is generated. This has proven invaluable to me in getting stuff up and running quickly...
Hope this helps...
Martin.
Every week I produce a word document with some copy, tables and charts from various sources on our network.
It can take a bit of time and sometimes I make mistakes or forget stuff that should go in. I want to automate this process as much as possible.
So basically I want to make a Word Template or Console App that when I open/run it it goes off and collects all this stuff and links it into the various parts of the document.
Assume I have to insert:
Some copy from another Word document
A PNG (pie chart)
Create a table from a CSV file
Have a standard Header and Footer with page numbering
I usually make a few changes to the copy in the document to highlight the highlight from the week.
Then I check it into a SharePoint where network users can open and view it.
I figure the thing to do is crack open VS2010 and make a Word Template project. I have never done this before and I wondered what traps there are or if its even an appropriate way to solve my problem.
My other option would be some power-shell but i'm no expert there either.
I'd be pretty comfortable writing console apps so I might end up doing it that way.
Help/Advice appreciated.
I would approach this problem by breaking down the individual steps as you would perform them if you were sitting in front of a blank Word document. Then automate it using the object model. There's a little bit of a learning curve, but once you get past that you'll be addicted to automating Office. Office is quite a powerful platform. Most of what you can achieve via the User Interface can be done programatically. I do quite a bit of automation with Excel. The code is fairly intuitive...its things like worksheet.Range["A1"] = "abcd" (setting cell A1 = "abcd").
here are some pointers:
http://support.microsoft.com/kb/316383
http://msdn.microsoft.com/en-us/library/ee861527.aspx
If you go through the tutorial in the first link, you'll get the gist of it. What's great is that you can use the debugger to step through your lines of code. As each line executes, you can see the results on the Word document. I've never used a Word Template project, so I can't speak for the pros/cons there. Going the object model route, you just have to articulate what you want to have happen, break it down into the individual steps as you would perform them, and then code it up. If you get stuck, likely someone else has blogged/posted about how to make something happen. Google will find a solution very quick. Good luck!
You have lots of options:
automate Word
do it as a macro (dotm)
create your document using Open XML SDK
VSTO
You are probably best off doing it using a macro first, though this is old VB6. The techniques you quickly learn there translate pretty well into the other approaches. Open XML SDK has the virtue of not needing Word to create the document.