Excel file not loaded correctly - c#

I am using Gembox-spreadsheet to parse excel files with multiple sheets.
I am currently loading a file using the following code:
excelFile.LoadXlsx(inputExcel, XlsxOptions.None);
where inputExcel is a fullpath. After importing the excel, I try to access its content (for each sheet, parse rows and obtain cell data). The problem here is that after loading the document, in debug mode if I check the values in a sheet, I see this error:
'excelFile.Worksheets.ActiveWorksheet.Cells.Value' threw an exception of type 'System.InvalidOperationException'.
However, when I try to retrieve the information from a cell (which contains some info, doesn't matter of which type) it retrieves 0.
Does anybody know why this error occurs and how I can prevent it?
I must mention that the values in the cells are generated through formulas, from a separate worksheet. Could this be why the values are not loaded?

Well, I found the problem. Because the cell values are obtained through formulas, when loading the file you must set XlsxOptions.PreserveWorksheetRecords (for xls) or XlsOptions.PreserveKeepOpen (for xlsx)

Related

C# change SourceData of chart in excel

I have got a chart and the data has been inserted so I have to extend the range of source data.
The original format
line chart
I am just writing like this
sheet.ChartObjects(0).SetSourceData(sheet.get_Range("B2", columnA + 5),Excel.XlRowCol.xlColumns);
and the program shutdown at this and log reads "System.Runtime.InteropServices.COMException"
"HRESULT:0x800A03EC"
I don't know the error and I didn't know how to show the previous source data range and format.If I can get the original source range with right format,I may modify parameter appropriately.
PS:I have found something here setsourcedata-correct-call-parametersbut I couldn't correct my code......

C# Aspose.Cells set data to Excel file with format

I have search around the web but could not find any solution.
I have a Excel file used as a template, with a area to parse data from DB.
Among the columns of the Excel file, there is a number column with a custom format.
My code (C#) of setting data to Excel file is as below:
Aspose.Cells.Worksheet ws;
Aspose.Cells.Range rg;
...
rg = ws.Cells.CreateRange("A1", "M10");
var setData = genDataArray(dt); //this function convert Datatable to 2D object array
rg.Value = setData;
Another attempt is also a failure:
ws.Cells.ImportDataTable(dt, false, "A1");
The code works fine. Data is set correctly to the Excel file, but without any format of the Excel file.
When I click the cells of Excel file (after the process), and press Enter, the value is formatted with the style I previously set in the Excel file.
How can i make the Aspose to apply the format I set in the Excel file?
Thank in advance.
After varous attempts, I finally found an solution.
The number column of inputted DataTable have data type of String, so i guess Aspose set it to the Excel file as Text value.
After changing it to Decimal type, the value is correctly formatted.

Importing an Excel WorkSheet into a Datatable

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.

Is it possible to access Excel file through ADO.NET that does NOT have a Header Row?

I have a formated Excel file, over which I do not have control, and I need to read the information contained in it.
The problem with the file is that the first few rows contain formated information and I can not modify that file nor I can not ask for a format change.
Is it possible then, to read such a file through ADO.Net?
Thanks in advance,
I think this article explains how to do this pretty well:
http://support.microsoft.com/kb/316934
As for the lack of a header row...
With Excel workbooks, the first row in
a range is the header row (or field
names) by default. If the first range
does not contain headers, you can
specify HDR=NO in the extended
properties in your connection string.
If you specify HDR=NO in the
connection string, the Jet OLE DB
provider automatically names the
fields for you (F1 represents the
first field, F2 represents the second
field, and so on).

Datatable not returning values from Excel (values from formulas)

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.

Categories