How to export Sql Server Data to Excel File in C# - c#

How can one read sql server data and write it to excel file in C#?
Do I need to use an external .Net library, or is it just OK using Visual Studio itself?
Any example and link will be well appreciated..
Note: Please send examples for Winforms, not asp.net

I would use some kind of OR-Mapper to read data from SQL such as
NHibernate
Entity Framework
Linq 2 SQL
For writing data to Excel you could bind hardly to any Excel Version by referencing the Excel Assemblies. The cleaner and more beautiful way is to use Open XML SDK for Office http://www.microsoft.com/download/en/details.aspx?id=5124

Follow the below article, this even provide the facility to export the data into many formats.
Export data to Excel and other formats using C#

see:
http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/exportxl_asp2_dc.aspx
http://geekswithblogs.net/VROD/archive/2008/04/20/121433.aspx

You could find helpful information how to achieve that goal in the following blog posts:
1. http://msmvps.com/blogs/deborahk/archive/2009/07/23/writing-data-from-a-datatable-to-excel.aspx
2. http://tim.mackey.ie/HowtoExportADatasetToExcelCAspnet.aspx

See : http://www.dotnetfunda.com/forums/thread548-export-data-to-excel-file-from-a-table-in-sql-server-using-csharp.aspx
It also has a nice example on the link inside the page.

Related

How to import data from Excel to C# Service based database?

Im using C# and Service based database and I need to import some data from Excel to my database ..How could I possibly do this?? Please help. Thanks a lot.
You can open the excel file with the Excel database driver and read it like any other data source, however this means you need the driver, which isn't installed by default.
Download
HowTo
However if the sheet only contains data and doesn't need any calculations, you can unzip the XLSX file, and find sheet1.xml (or whatever it's called in your file), open it in your app like any other XML file and import the data.
This is likely to be a much better long term solution, since MS has been trying to kill off the Access database driver for ages.
Also, it's been a while, but I don't believe MS recommends using the MSDE from within a service.
I would recommend you to use OfficeOpenXml.Core.ExcelPackage or EPPlus to read/write excel files. Bellow is some links to reference
https://www.nuget.org/packages/OfficeOpenXml.Core.ExcelPackage/
https://github.com/JanKallman/EPPlus
https://www.c-sharpcorner.com/article/import-and-export-data-using-epplus-core/
https://tedgustaf.com/blog/2012/create-excel-20072010-spreadsheets-with-c-and-epplus/
http://www.talkingdotnet.com/import-export-xlsx-asp-net-core/
https://toidicodedao.com/2015/11/24/series-c-hay-ho-epplus-thu-vien-excel-ba-dao-phan-1/
http://www.zachhunter.com/2015/11/xlsx-template-with-epplus-and-web-api-save-as-xlsx-or-pdf/

Writing to an Excel file from SQL Server Database

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...

Exporting Listview in Visual Studio Into Microsoft Excel

I am new in creating Report System using Visual Stuido 2010 C#. I already have a listview in my forms where it has all the information needed for the report. I am planning to integrate it with Microsoft Excel. What I want is that the user is capable of exporting the reports in the listview into Microsoft Excel. Is there anything I can read or research on how to do this? Or is it even possible? Please help. Thanks.
see:
http://www.dotnetlogix.com/article/aspnet/56/How-to-export-listview-in-excel-in-asp.net.html
http://www.c-sharpcorner.com/UploadFile/DipalChoksi/ExportASPNetDataGridToExcel11222005041447AM/ExportASPNetDataGridToExcel.aspx
Iterate the data source and write values into CSV (comma separated) file. I think this will be the simplest way. (Must read article : Why are the Microsoft Office file formats so complicated? (And some workarounds) by Joel Spolsky).

C# Best way to read a XLS (XLSX) file and populate a datagrid

What is the best (free or paid for) to read Excel files (XLS and XLSX) using C# and populate a datagrid, I would also like to have any changes made in the datagrid be written back to the file.
I should also add that it needs to be high performing as the datagrid can contain a significant number of rows (10,000+).
This would be used for a commercial application so it would need to be distributable.
This is a Windows Forms application.
I recommend databinding the datagrid to a dataset/datatable populated via the ADO.NET OleDb provider.
You'll find an example at Read from Excel using OleDb in a Windows Service?
I also found How To Use ADO with Excel Data from Visual Basic or VBA and How To Use ADOX with Excel Data from Visual Basic or VBA to be useful in knowing how to create a table (sheet) and access sheets and ranges.
It works very well for xls files. I haven't tried using it for xlsx.
SmartXLS for .Net
It support read/write xls/xlsx files and can import/export workbook datas to/from a datagrid.
You don't define whether you speak about web or fat client application.
You can use Open XML SDK 2.0 for Microsoft Office for reading XLSX file. (see http://www.microsoft.com/downloads/details.aspx?FamilyID=c6e744e5-36e9-45f5-8d8c-331df206e0d0&displaylang=en and http://msdn.microsoft.com/en-us/library/cc850837(v=office.14).aspx for some examples and also on http://openxmldeveloper.org/default.aspx.)
You can display and allow to modify the content in a grid like jqGrid (see http://www.trirand.com/blog/?page_id=6) if you use a Web application. This grid is the best jQuery plugin which has a lot of different features and allow implement practical every scenario.
After all you can use Open XML SDK to save result in a modified XLSX file.
For xlsx :
http://msdn.microsoft.com/en-us/library/bb332058%28v=office.12%29.aspx
http://www.codeproject.com/KB/office/OpenXML.aspx

create xls with c# asp.net

Is there any free library for create xls files in asp.net on a server without office?
thanks
See this codeproject solution:
http://www.codeproject.com/KB/office/biffcsharp.aspx
You could also use SpreadsheetML. It is just xml so you could generate it by printing text to a file.
OfficeWriter is another third party tool that can be used for this purpose:
http://www.officewriter.com/trial
It's an API that generates/manipulates XLS/XLSX on a server without office.
It's not free but we use the Aspose Cells tool which is excellent

Categories

Resources