How to select data from database and display in excel - c# - c#

I want to select data from database table and display it on excel sheet.
Basically similar to exporting data to excel, but in my case when Ill open the excel, a select query will be fired which will fetch data from table and display in excel sheet from A1 Cell.
Is this possible? I dont need the code by maybe some idea or reference of how to implement it. I am no expert in c#.

there are many ways you can do this,
it all depends on how you want to manage the logic :
here is a good way to start with :
http://www.c-sharpcorner.com/UploadFile/83fe73/solutions-to-export-data-from-database-to-excel-in-C-Sharp/

Related

Dynamic Excel columns and rows to SQL

I have an Excel template which has dynamic rows and columns (it means the Excel template will be downloaded based on the parameters I select in my UI). Now there will be some data in the Excel sheet against those dynamic rows and columns coming from the database. I want to modify that Excel and import it so that only the updated(modified) cell values are updated in database. I thought a lot and I think this can be done with macros (by changing the color of cells with updated values and picking them up from C#). I would welcome any other thoughts/approaches for this.
An approach I have used is to create a webdav/REST service which can then be mounted as a drive in Windows.
The exposed file on the webdav service was simply a CSV, which users could edit in Excel, but the GET request was serviced by reading the database.
After making the changes, the user saved in Excel, which caused a PUT on the webdav service. The PUT request wrote the new values to the database.

EPPlus / How to get data from pivot table? Or how to manipulate data easily?

I am wasting very much time on manipulating data in reports. Using pivot table is a good idea but how? I tried some free PivotTable classes but they were lacking subtotals.
Then, another approach. For excel output of reports I am using EPPlus. It also supports pivottable. The problem is some of our customers do not have office(OpenOffice, MicrosoftOffice etc.), so just creating and saving an xlsx file does not work. The only thing I can try with EPPlus is creating an ExcelPackage, filling a worksheet with data, and then creating a PivotTable with data.
I have several questions;
1) From that PivotTable object can I access the output of PivotTable fields and values. (Up to now I could not).
2) Related to the above question... Does an xlsx file contains data about the PivotTables or just the rules of creating PivotTable(Like name of table, sourceRange, rowFields, columnFields, dataFields, aggregate options etc). I have made a small test about this. Steps as following:
Opened a new excel file.
Inserted some raw data.
Created pivot table with the data.
Changed some values of data. (without refreshing pivot table)
Saved and closed the file.
Opened the file back.
In fact my guess was "pivot table would update according to new data", but I was wrong. It did not update. This may be a proof for "xlsx file contains not only rules for a pivot table but also all the values of it". If this is so I have a hope to access that data without saving the file (and I do not need any office programs).
3) Any other approach appreciated.
Thanks in advance
I am by no means an expert on EPPlus but have been working with it for the past few months and can hopefully shed some light on your questions.
If you create a brand new xlsx in EEP, add data to a worksheet, create a pivot table pointed at the data/worksheet, and save it - then the PivotTable does NOT contain any data. It merely contains the definition of the how the PT should slice the data when the file is opened in excel (as you mentioned in one of your questions).
When you actually open the file in excel and SAVE IT what excel does is copies all of the data that the PT relies on and puts it in the pivot table cache. This is why you can then delete the original cells that contained the data, save the file, and then reopen it in excel (might have to dismiss some errors), and still see the PT with data. You can even double click on one of the data cells in the PT and excel will regenerate some or all (depending on which cell you clicked) of the associated data into a new sheet.
Yes, your guess was in fact wrong because of this pivot table cache. You have to tell excel to update the data source in the proper Ribbon (assuming the data is still there) to see the new data show up.
So, to access the data you can figure out where it sits by going into the PivotTable.WorkSheet object and pulling the data out from that. You can see how I did it in the extension method i created here:
Create Pivot Table Filters With EPPLUS
Another option would be to extract the actual worksheet.xml file from the xlsx. An xlsx file (and any other MS Office .???x files) are just ZIP files renamed. So you can use the standard .NET methods to get the xml files out of the zip and use something like LinqToXml to extract the data. So something like this:
var zip = new ExcelPackage(file).Package;
var recordspart = zip.GetPart(new Uri("/xl/worksheets/sheet1.xml", UriKind.Relative));
var recordsxml = XDocument.Load(recordspart.GetStream());
It wont be pretty doing all the XML manipulation but if a final format of XLSX will not work it may be your best option.

Warehouse management using Excel and C#

I need basic idea on warehouse management using Excel and C#.
Actually, the warehouse file consist of 30 columns and 40,000 above record.
I need to select only select columns (suppose-12) from the uploaded Excel file (.XLSX)
I need to display 40,000 rows of data (all) with customized columns
Provide user auto search text-boxes (suppose for 5 columns-like truck no, order no, orderdate, wh-name) and display the search result at grid view
Edit and update the data searched by auto search (any row or implement update for all rows)
finally save the data into a new Excel or PDF file
Please help to implement these, using C# and Excel.
I tried but it takes 7-10 mins to save data from datagrid to new Excel file. I have problem with dynamic auto search columns
As a Suggestion: use http://epplus.codeplex.com/ to read the incoming file. Then put all the incoming material in some DataTables.
These DataTables could be used to implement the other functions way easier, since they are build to support that kind of actions.
Save any modified data to these Datatables.
Export them to a new xlsx file, that you can hand back. Agian, you can use epplus

How to Export data from a dataset to Excel in c# whose rows are expandable?

I have a DataSet which has two tables. They have relation among them, like Customer and Orders.
I need to export this data into Excel in a way that the resulting excel will have rows that are expandable. So each Customer row will be expandable and, upon expansion, it will show that specific customer's orders.
I tried by creating nested html table rows, bu in vain.
Your help is greatly appreciated.
Note: I am working in a windows form application;
**
Thanks.
You could use the Excel object library (namespace Microsoft.Office.Interop.Excel) to create the target file, arrange the orders in the rows beneath each customer and group them so the user can expand and collapse them on demand (or, embed macros to expand/collapse based on other ways of user interaction).
There are ways to do this with better performance (and, IMHO, also easier to do) - I have used SpreadsheetGear for more complicated Excel exports and can recommend it wholeheartedly.

Dataset to excel

I'm developing an application that as to export a dataset to excel, this dataset have 3 tables and the relation between them, I would like to export this to a single excel sheet where for each parent table there is a '+' sign that when expanded shows the child values related to it. To export a single table to excel I know how to do it, but to create this type of relation I have no idea how to do it, do I have to create a macro on the excel file?
I use a DevExpress Reports or a ActiveReports for this. I create a normal report and next i save it in xls, and Done. Its very complicated to this using us code. Its more easy to use a third-party components
There's a free tool: https://closedxml.codeplex.com/ to create XLSX outputs from code.

Categories