I have been asked to create import functionality in my application. I am getting an excel worksheet as input. The worksheet has column headers followed by data. The users want to simply select an xls file from their system, click upload and the tool deletes the table in the database and adds this new data.
I thought the best way would be too bring the data into a datatable object and do a foeach for every row in the datatable insert row by row into the db.
My question is what can anyone give me code to open an excel file, know what line the data starts on in the file, and import the data into a datable object?
Take a look at Koogra.
You instantiate a WorkBook object from a path to an XLS file.
You access a WorkSheet object from the workbook's Sheets property.
You can enumerate over the rows in the worksheet by accessing the sheet's Rows property from index MinRow to MaxRow.
You can enumerate over the cells in a given row by accessing the row's Cells property from index MinColumn to MaxColumn.
Each cell has a Value property (object) as well as a FormattedValue method (string).
Give it a try -- I've found it to be extremely intuitive and easy to use.
You can make use of an OleDbConnection to connect to excel file and the query it using SQL queries.
If it is an Asp.Net application, then you make use of the FileUpload control and get the bytes from the file. Then you will have to manually convert it to a datatable.
Try out these links:
OleDbConnection to excel file
Byte array to datatable
What your looking for is the concept described Here
Providing you dont want to use a third party library anyway, else Dans solution will suit you
First you have to download the dll file namely
NExcel.dll
By using this dll you can make various object which are very useful for
import excel data in .net using both vb as well as c#.
Good luck.
Related
is it possible to overwrite data in cells with OleDb in C# with a specific row range?
I only found adding data to first empty cells in specific column like
string sql = "Insert into [Tabelle1$] (testA, testB) values(6,7)";
If its not possible what assembly is fast for this task ....Microsoft.Office.Interop is horrible slow.
Thanks for any tip
I have implemented this code as mentioned here :
From Excel to DataTable in C# with Open XML
My excel is like this :
So now the code which I wrote does not work as I am unable to skip the first row with unwanted text.
I need to skip the first row & last row, extracting data only from the columns & rows
Please help
The easiest way to read an Excel file is to use EPPlus library.
You could then take a look at the following examples to import your worksheet into datatables here or here.
Hope this helps !
How to read rows one by one by its column name from xlsx file using C# or making use of DocumentFormat namespace.
Please help..!
Thanks.
Look at below question. You can get the data into dataset first and then read it from there by column name. I think that's a better option than trying to figure out how to read data using column name in excel(if at all it's possible which I doubt).
Get column name from excel worksheet
I am working on a project which involves getting data from a .doc or a .docx file. The input requirements are in a tabular format. Is it possible to retrieve data from table in a row wise manner or as a dataset.I am using Microsoft.Office.Interop.Word to get the data from the doc file.
You can use the property Tables of the Document interface to get a collection with all the tables in your document. For each Table in this collection you can get the rows and for each row the cells.
I.e. if app is your Application object you can write something like this to get the text contained in each cell(supposing that there is exactly one in your doc):
string aCellText;
foreach (Row aRow in Application.ActiveDocument.Tables[0].Rows)
foreach (Cell aCell in aRow.Cells)
aCellText = aCell.Range.Text;
That is not possible with the word, but if you want something like that than you should put tabular data in you excel file and than you can easily read it in the dataset object....
this is not possible to get the data in dataset object from .doc or .docx file. But if your data is in tabular form and also in the excel sheet than you can retrieve the data in dataset object. MS Word is for documentation purpose and excel is used for maintaining data sheets..
A project I am working on requires values from a C# application to update 6 values in an Excel sheet. This excel sheet then populates a named range of items and prices, driven by a number of VLOOKUP formulas and worksheets within the same Excel File.
The two columns in the range are named 'Item' & 'Price'.
'Item' is a column of items concatenated from formulas on a spreadsheet, and these appear fine in the DataTable. However, none of the values in the 'Price' column (all number values from VLOOKUP formulas) appear in the DataTable.
When loading the DataTable in C#, I can see that the two columns' types of string and double are fine. If I replace one of the formula derived values with a hard-coded value the DataTable is fine, so I guess the DataTable itself is working as expected.
If I open the Excel file after the values are passed into it, I can see that the input values went in fine and the formulas have calculated correctly.
So, the only thing I can think is causing the problem is that the formulas are not being recalculated after the values are passed into it.
Is there a way to open an excel file in C# and get it to recalculate all the formulas?
Never mind,
Replaced the OLEDB update with an Excel Interop, opening the Excel file with the Interop allows the formulas to recalculate.