I have saved the specifications of a mobile phone into an Excel spreadsheet. Now I want to copy the data from this Excel sheet into a database programmatically.
How can I do this? Is there any way? I copied the phone specs from gsmarena.com. Please help me.
It's completely possible. There are several ways, not all of which require programming.
If the Excel sheet is in a tabular format (one "record" per row, with static column names), you may be able to use your database management tool (MS Sql Management Studio for Sql Server, for instance) to do a "bulk insert". Consult the documentation for your particular database.
You can also use Excel formulas to create a column in the Excel sheet that contains "Insert" statements that will insert each row into the DB. You simply copy that column out of the sheet and paste it into a SQL command parser, maybe wrap it in a transaction, and then hit "execute". I've done this a few times when doing data migration; it's kind of a one-off, but for a one-time operation that's just fine.
If you want to use a program to solve the problem, you'll need to use OLE automation to open your Excel sheet, and programatically iterate over the rows and columns to create a "record" that you save to the database. The exact wheres and hows are a little in-depth, and depend a lot on the type of database, your version of Excel, the type of Excel document you have (XLS or XLSX), and how your Excel sheet is organized.
Well for starters, is the database properly designed and ready to go? If not, you need to first design one, which I suggest you base on normalizing the Excel data. Once that's complete, you can use the interop libraries for .NET to pull the data from Excel and write to the DB through MySQL, Access or some other DBMS.
You could use a tool such as SSIS, or connect via ADO.Net and OleDb/Jet drivers.
Related
Is there a way to import data from an excel sheet into a database on SQL Server without using the bulk method? Keeping in mind that I'm creating a web application where the user uploads an excel file to the page and it proceeds to import it into an existing database.
The bulk method works using OleDbDataReader, except it copies the excel sheet and pastes it onto the database without considering the column names compatibility. Am I missing something?
If you are working through SSMS, use the SQL Import Export Wizard. There's enough documentation on MSDN
From code you can use the SqlBulkCopy class for importing into SQL databases. If you are importing bulk records into Oracle you can use ODP.net OracleBulkCopy. Oracle bulk copying is only supported right now through the full ODP.net provider. Managed ODP.net does not support bulk operations.
To read the Excel file you can use any number of libraries.
LinqToExcel,
OleDb,
closedxml
Take a look at the DocumentFormat.OpenXml.Spreadsheet namespace: MSDN Link
It will let you look at the different parts of the spreadsheet as it is stored in OpenXML.
You can use format file along with bulk insert. The format file maps between table columns and the fields in the file.
Refer :
https://learn.microsoft.com/en-us/sql/relational-databases/import-export/use-a-format-file-to-bulk-import-data-sql-server
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?
I have one Excel sheet, in that plenty amount of data is there, so need to write these data in the SQL server table directly. I searched in google but I didn't found a proper solution for my requirement.
My Excel contains the data in row wise, I mean that data is not continually provided. Having sub headings & some other information about tables description written after every table ended.
I hope you all understand my requirement. Please anybody have idea about this issue, or alternate solution to resolve. Have to store this excel data in to the SQL server table.
Here data is columns as rows & rows as columns. So how to read in this case.
firstname vijay vikram vikek varun
lastame kumar rathod goshal kandari
mobile 45425 456546 54656 454615
Like above my data in excel sheet so how capture this kind of data.
Did you try using a excel interop.Using interop you can even read particular cells.You can read values by specifying a range and build a datatable or list.
These links may help
http://sandeep-aparajit.blogspot.in/2008/08/how-to-read-excel-in-c.html
C#: Getting a cell's value with Excel.interop
If you want to insert data into db directly, without front-end functionality, then you can do it by using SSIS packages or import functionality available in SQL server
I have tried importing data from excel to sql server table. The process is in sql server right click on particular database and under tasks tab select import data option then select source excel file and destination table. It's working fine for me.
Thanks
Venkat
I have a requirement that , i have a table of 400 columns.
Here i need to Export this table to Excel sheet.
And i need to upload to Open Office Sheet. I do not have MS-office.
This is in ASP.NET and C#.NET
I am new to this concept.
So please help me to solve this.
And friends there is a problem , i have 400 columns , which is not possible to insert into Open Office Excel , i need to insert other columns into new sheet
Thank You All!!!!
Have a look at this.
Alternatively you can look at using OleDB to do this
Reading and Writing Excel Spreadsheets Using ADO.NET
I've used this method a number of times and found it to be quick and effective.
3rd Alternative, if it doesn't have to be a true xls/xlsx file and you want lots of control over what the final output looks like, try Microsoft's Spreadsheet XML.
XML Spreadsheet Reference
Again, I've used this method to great effect
You can read from one source and update to another using DataSet, with 2 distinct tOleDb connections.
Here's some code that does that.
CopyData-SqlToExcel-TwoOledbConnections.cs
I need to create a workbook which has a raw data on a sheet and a pivot table on another sheet. The pivot is created from the raw data. Then I need to create a chart with this pivot on the same or new sheet.
How can I do this in C# - and is this possible using VSTO?
Easiest way to do it?
Use Open XML SDK 2.0
Build a pre-generated version of the workbook with raw data, pivottable and pivotchart already created.
Install the OpenXML SDK 2.0 setup package
Use the OpenXML SDK 2.0 productivity tool from this install to open the workbook
Select the root element on the left hand pane, and then right click and select Reflect Code
Done - on the left pane is a complete C# class with the necessary code to generate the file.
Well not quite done as you still need to refactor the code to take into account dynamically adding the required data content, however this will get you 97% of the way if you already know C# fairly well.
I've done something similar to this before, but I didn't do it using C#. I used VBA since the language is already built in to Excel.
My approach was this:
Create a worksheet called "RAW DATA." This worksheet has a QueryTable object in it that can be updated via code in VBA.
I manually created a pivot table based on the QueryTable in the RAW DATA worksheet.
I then added code in VBA so that after RAW DATA was updated, Pivot Table was refreshed with the new data.
This method works really well if the layout of your raw data and your pivot table stays the same. I have a workbook that I made for a cowork that updates multiple sheets with pivot tables based on one set of data. She really likes it because just by clicking one button, she has a refreshed view of all of her data.
If this approach works for you and you'd like more details as to how to implement some of those methods, let me know more details of your situation and I can try to help you out.
One option is to connect to database from Excel and refresh the "Raw data" sheet, via VBA or defining an SQL query in Pivot data source. This is not so great as the user who opens the file must be able to connect to the database.
The other option is to fill the "Raw data" sheet programatically via C#. There are numerous libraries that can help you with that, even some free ones, but you can also do it yourself by using the Excel XML format (SpreadsheetML). You can use the Excel 2003 XML format or the new Open XML Excel format. The latter is far more complicated, but with it you can also take advantage of the OpenXML SDK and the Excel Package API.