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

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/

Related

Modifying Excel Sheet through C# without Interop.Excel

I know questions like this are around in stack and there are 3rd part libraries to do the trick but none of them is fixing my issue at the moment. So the issue.
I have an Excel workbook (.xlsx) with multiple sheets generated by another system. I have to read the data from this via SSIS and dump it to a SQL DB.
Now the issue is although the Excel sheet contains data and when I open manually it opens without any error and the data displays when I use a script task and use OLEDB connection to connect to the excel and open it up the connection is made successfully but when reading data the column names are not picked (I get F1, F2 likewise) and no data rows are read. I simply get a blank row and that's about it. I have tried with HDR= YES and NO and IMEX=1 and 0 but always the result is same.
Funny thing is if I open the excel sheet do some modification (like change a sheet name save and change back the sheet name and save and close) and after that I try to run the package the data gets picked without any issue (also I noticed that the file size increases from 164KB to 196KB). Now because of this what am trying to do is modify the the file a bit and save via code.
So the initial step I tried was through using Office.Interop.Excel and it works like a charm in my machine but on the server NO OFFICE so IT NO WORKS. And nope the IT guys are never going to install access engine or excel or anything there.
Then I tried via OpenXML and 3rd party library like NPOI and even via OLEDB connection to modify the file. in both NPOI and OLEDB methods the file got changed but still it didn't get picked up properly by the SSIS package (I noticed that the file size didn't change and remained at 164kb). In OpenXML it wasn't able to open the file and threw an error saying "the document cannot be opened because there is and invalid part with an unexpected content type".
So right now I am stuck with no proper method in sight and would appreciate any help in solving this either through c# code or any other SSIS method available. SSIS version am using is 2008.
Edit 1
So I noticed that the script task is able to read the data from the first sheet out f the multiple sheets but the other sheets are the problem. So somewhere the xml for these sheets are broken. Anyway I can copy the xml configs of the first sheet to other ones? Just a thought...
Edit 2
So the first sheet is of ContentType "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" while all the other sheets are of ContentType "application/xml"
Ultimately ended up using two libraries for this. The data was read without an issue by using exceldatareader (http://exceldatareader.codeplex.com/). Using this the data was read into a dataset easily and then it was written to a new Excel file using epplus (http://epplus.codeplex.com/).
After that when the new excel file was read via the SSIS package data got picked without an issue. Hope this will help someone out there.

Exporting to Excel without MS office installed for Wpf Product

I have a small wpf product which requires exporting data to excel with out excel installed on the client machine.How to achieve this in C#.After exporting, this excel can be opened by Open office. All I wanted is to save excel file to the client hard disk. Even excel is not installed he should be able to save the file,he may not be read it without excel but should be able to save. I dont want to any 3rd party or some other open xmls.
Recently I downloaded a product which is able to export to excel without excel installed and able to open it with open office.
When i checked their binaries they contain office.dll ,Microsoft.Vbe.Interop.dll and Microsoft.Office.Interop.Excel.dll's only .I want to know how they are able to manage with these dlls.
I have already written code for this but its breaking when excel is not installed.
I have read many open xml and other stuff relating to this but not satisfied.
My requirement is too simple ,just exporting datatable data to excel,no reading back the data and no fancy oparations with excel.
Please give me suggestions and links will be appreciated.
Thanks in advance
Either work out with CSV format or you may like to use EPPlus library. See similar answer here
You can use CSV, XML, or ADO
How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET
xslt transformation can also to the job. i use it to export wpf datagrid data.

How to upload data from Excel file to Sql Server using BizTalk

I wanted to upload data from an Excel file (which will be located at some specified folder) to SQL Server using BizTalk.
I am using Visual Studio 2010,.NET V4 and SQL Server 2008.
Can anyone please provide me detailed steps of how to achieve this or any existing discussion/forum link for the same.
Thanks,
Mayur Jadhav
Mayur, Excel sheets are basically binary files, you need a custom pipeline component for processing this excel file. I'm pasting a link here that will help you out to carry out the desired task.
There is another article uploaded here in the code project that aims to discuss how to read an Excel file from a pipeline in BizTalk. A custom pipeline need to be developed to read Excel files. Excel file will be read by taking the file adapter and the pipeline will process it. A custom pipeline component is nothing more than a simple .NET DLL that implements a class with some predefined interfaces. This interface represents the layer between a .NET program and the BizTalk Server.
Hope that it helps!
first you need to create flat file schema
how to create flat file schema in biztalk
than you need to insert the data in to the DB i would use a store prudence that will insert the data into the db and than use the LOB adapter in order to cunsume it inside biztalk orchestration LOB Adapter
There are a couple of options to process Excel files..
If you can control then save the Excel Files as XML and process them, otherwise looks for excel adapter for Biztalk. For example
You will then just have to work with mappings and port files. This should help you.

openXML SDK create Excel file

I am creating Excel files using OpenXML SDK 2.0.
Is it required to install OpenXML SDK in the server?
If possible, can someone send me a sample code to create Excel file without using a template?
Is it necessary to save the excel file created using OpenXML? I am creating the file but would not like to save it. Also, would be creating pivot tables in the excel file.
Thanks,
You will need to include the dll in order to use the sdk, which should get built out when you deploy.
I would recommend using a template since it will already contain a bunch of the XML that will make the Excel file valid and complete that you will not have to worry about adding. If you are totally against using one, then explore this blog for examples.
You will need to save the changes so that when you return the file it will contain your changes.
3) You can use a memory stream, SpreadsheetDocument has Create and Open functions based on stream, too (not only file path or package).

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

Categories

Resources