I am trying to convert the data I already have in a Worksheet in to a DataTable using ClosedXML but I cannot find an overload method for what I need.
This is the code that I am trying:
NewWorksheet.Cell(1, 1).InsertTable();
The closest overload method is the single with an IEnumerable or DataTable but what would I put in there? There is no overload method that takes a range.
Here is the documentation page but nothing meets my need.
InsertTable inserts an existing DataTable into the spreadsheet. You want to do the reverse. That's not possible in ClosedXML.
Related
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 Display Data Table as output in rest service in XML FORMAT
DataTable has a method WriteXml that contains many overloaded versions. One of them is DataTable.WriteXml(string). You can use this to save the contents of DataTable as an XML file. The other option you can use is the overloaded method that writes to Stream object. You can use either FileStream, MemoryStream or similar.
Any of the overloaded methods contains a version with XmlWriterMode argument that allows you to specify whether you want to write schema or ignore it.
This might sound strange but exactly this is what I want to do:
I have a TestMehod Currency_ReadItem_Test() which tests ReadItem() method in Currency class. This ReadItem() takes IDataReader object as an parameter and fills local data members in the class.
Now the problem is I want to test this ReadItem() in my TestMethod which has a Excel Sheet DataSource.
Any idea how can I first fill my Reader from Excel sheet so that it can be passed to this function in oder to test it?
Any help would be appreciated.
Thanks
One way is to read the spreadsheet via ADO.NET - then you just use ExecuteReader for some query you define.
Alternatively, you could use the Excel API and load the data you want into a DataTable, and then use a DataTableReader. Or if you don't like DataTable and you know for sure the schema of the data (i.e. fixed and rigid) you could also populate a List<SomeType> and use ObjectReader from "FastMember" (open source)
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.
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.