Writing to an Excel file from SQL Server Database - c#

Currently, I need to be able to retrieve values from an SQL Server DB, populate an Excel file according to a certain template, and then allow the user to download the file. I also need this this certain template to be customizable, in the sense that the user can add new fields, and remove fields.
I understand that there are a couple of approaches I can take: using .xlt, and using C# directly. With C#, the user will need to interact with a UI, which will then populate a ExcelTemplate table in the SQL Server. This ExcelTemplate table will then be used when the user wishes to download a new Excel file.
I know all this stuff may sound kinda abstract, so please do tell me if there are some places I need to elaborate/clarify. Thanks a bunch, man.
EDIT: Sorry, I kinda missed this part out, but I'd prefer to allow the user to customize these Excel templates via a Silverlight UI.

You can create Data Sources in Excel and pull the data from MS SQL Server.
You can use MS Reporting Services which allow to get reports in MS Excel format. In this case users can use Report Builder to customize the reports.

For pulling down data from SQL Server and dumping it into Excel, you can use Officewriter. It has Reporting Services integration and supports generating .xls and .xlsx documents. There's also a template component that basically does what you're trying to do. The templates are actually Excel documents, so the users can edit them directly in Excel. Not Silverlight, but not bad. You can try an eval for free.
DISCLAIMER: I'm one of the engineers who built the latest version.

at the end of the day I think I'm gonna spend some time building a customized dashboard. It won't be generic, but rather focused on the existing database.
I know this answer is kinda vague and all, but I'd like to say thanks for all the help :) it'd be great if there are dynamic solutions for this in the future! I think...

Related

C# the best way to edit strings MS Word from database?

I will be building a desktop application that should interact with a database, I will need to build an API logically to contact the database remotely and retrieve data from there.
I was given a word file and I will be updating values where the black is the values I'm getting from the database. I will sometimes have to print the file.
I however not sure what's the best way to do this. Do I need to modify the Word file and return it to the default value each time? Should I use reports instead or something else?
I think there are no 'best of the best' practises
You may use DocX from NuGet
You may also get a direct document access using ms office interop word namespace
(afaik)

Excel data connection to API

I have an Excel client and an API and both are under my control. What I want to achieve is a data connection in Excel that can be refreshed by the end user by the click of a button instead of downloading xml/csv/xls files. The benefit would be that the user can have extensive custom charts that don't need to be rebuilt every time a new data set is used, e.g. weekly pivot reports etc.
That is pretty straight forward when using single tenant SQL Server or Access as a data source, however the requirements are a bit different here:
The data has to come through the API as data transformations have to be done.
The user should authenticate with every refresh (i.e. basic auth).
Support for Excel 2010
Data connection "From Web" seems like a good candidate, however this only works with html tables as far as I can tell. OData connections seem to be only offered with Power Query and the Professional Plus packages.
Does it make sense to go down the "From Web" connections route and convert thousands of rows of xml to html or are there other options?
UPDATE
I ended up creating an html table manually which is backwards compatible to Excel 2003 and does not require client-side VBA.
Please check below link it may be help you
http://interoperating.info/courses/perl4data/node/26
http://www.codeproject.com/Tips/463096/Converting-a-datatable-to-CSV-XML-and-HTML-by-usin
You can use ExcelDNA to create an addin that will let the user manipulate (get/set) the data from an Excel sheet, with a C# addin in the background to communicate with the API's XML. The addin works like an advanced VBA addin and can be activated via a button.

Best Way to generate excel report

Actually I'm asking this question because I want to know what is best solution for this base on what I only have. First let me show you what I really want to achieve. My boss want me to generate report in excel file (.xls or xlxs) then inside this report compose of multiple sheets with each respective sheet name. Data are presented in nice and professional way and sometime there are graphs inside that are link with the data within the sheet and it looks like this:
Below is what I only have to generate this output.
Visual Studio 2008
Sql Server 2008 (no SSRS)
Report is generated via
RDLC (naming of sheet is not possible for this but multiple sheet is
doable via pagebreak)
I try to update excel file via Sql Server but
seem too complicated for this specially when dealing with Total
fields.
I have not yet tried to render graph on .rdlc
It sounds like the data lives in SQL server. If you have access to it, why not create an ODBC connection and use this to pull raw data into each one of the sheets you need, and then configure a dashboard tab that automatically pulls data from those sheets (even if you refresh it) and shows the graphs and summary tables in the format that you want?

Call out to web service from Excel 2010

Starting an innovation week at work here. Good ol' Excel project! So here's what we want. We want to be able to create user defined functions in Excel, just like Excel has the SUM, etc. we want to make our own.
These UDF's would take Excel data in, and they would then pass that data to a Web Service which would do what it needs to do and then return the data back. I know how the Web Services will act, but what I need some guidance on is preparing the data in Excel, setting up the UDF's, and dealing with the sending/receiving of the data in Excel. I've never done any Excel programming before.
At first glance, it seems I might not need to do any VBA work, and that I could almost create a plugin for Excel (generate a .xll file from C#), and then go about it this way, but I'm not really sure how this all works.
Am I approaching this right? Are there any other things I should keep in mind? Is there anything I should read up on (any good links)?
Thanks.
There are good articles like below on MSDN.
Article 1
Article 2
Videos on Youtube and other
Video 1
Video 2
You can call a WCF service from Excel with VSTO or with VBA
You will want to use VSTO to create an add-on for Excel. Using Visual Studio create a new project. Select Office and then Excel 2007 Add-on or Excel 2010 Add-on. Check out this great resource for examples on how to get started http://www.packtpub.com/article/microsoft-office-excel-programming-using-vsto it pretty much has everything you are asking for (except calling a web service).
It may be possible to do all of this in VBA but it won't be as easy and you'll have to deal with the security issues that come with it.
You could take a look at XLLoop. This is a framework for developing Excel UDFs on a remote server. It has server implementations in a range of languages and is open source with a liberal license.
(Full disclosure: I work on this project).

How to import the data from an Excel spreadsheet so it can be manipulated in C#

I have an excel spread sheet (well, hundreds of them) which I need importing into a database.
If the excel data was in a nice uniform format I would simply save them out to CSV, read them in using something like LINQ to CSV and save the required data away.
However, the excel spread sheet is 'uneven' in that different groups of cells contain different data.
I need a way of grabbing the data and then working with cell references to grab the bits I need and save them to the database.
What's the best way to achieve this?
Thanks
UPDATE some more information
I have numerous spread sheets, all identical in structure that need to be imported into a database. The import is not simple in that different chunks of data from the spread sheet will go into different tables. The excel document itself contains a few sections (basically question / answer) type data. For each different section I need to grab the data, shape it into a form that makes sense in terms of the database and save it.
Ideally I would like to create a quick little WPF app that will let me select a spread sheet hit a button and perform the import.
You could use the Excel Object Model to read the data if you do it in a non web environment.
See for example How to automate Microsoft Excel from Microsoft Visual C#.NET.
If it has to be inside a web application. I suggest to use Aspose Cells.
Turn the Excel Spread sheet into an ODBC (Open Database Connectivity) Data Source so you can access it just like you would any database:
http://www.datamystic.com/datapipe/excel_odbc.html
Then access it just like any database using ODBC:
http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcconnection(v=vs.71).aspx
When the data is not uniform, it is often better to keep your approach as simple as possible in the first instance. Start with vba and the "Range" object (which is part of the excel object heirarchy). From there you can increase the level of automation and in most instances reuse this "Range" work.
avariable = Range("A2:A5")
That notiation is not going to change very much. It wont matter what final target language you use (language: C# / vba / etc).
There are a number of other ways of going about this -- java based / xml based / c# based / and a few other really cool ones that only apply to certain niche situations. If you can provide more information about your use case, then perhaps I can suggest some more things to try.
Q & A
example link for automation from C#: http://support.microsoft.com/kb/302084
You should probably take a look at Microsoft's Visual Studio Tools For Office (VSTO), which handles a lot of the unpleasant COM/interop stuff for you.
To those who may be interested I ended up using LinqToExcel:
http://code.google.com/p/linqtoexcel/
Did exactly what I was after with minimal fuss. Excellent

Categories

Resources