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

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.

Related

How to create a csv file in c# for downloading and set column type to text

I have an app that already allows the user to download a grid to a csv file utilizing the appropriate headers and content type as follows:
thisPage.Response.ContentType = "application/vnd.ms-excel";
thisPage.Response.AddHeader("Content-disposition", "attachment; filename=GridData.csv");
When the user opens the csv file in Excel I need some of the columns to automatically format as text. For reasons beyond this post the download has to be csv and not xsl.
Is there a way to format columns in csv?
If you output your field as (e.g.)
="01234"
Excel will show as 01234. You must include the = sign.
Note this is assuming Excel is the target for the data - some other application will likely choke a bit on that.

Reading excel data of custom data type in excel to load in to data set but it's truncating the decimal value using oledb provider

I tried and fed up with the problem.
Actually I have a requirement of excel sheet containing Custom format in Excel.
While loading into dataset the decimal value has 0.123456 but its dispalying as 0.1235.
oledb connection string is having Imex=1, HDR=Yes. I need header and the data has to be displayed as 0.123456.
Can you please help me on this.

Excel file not loaded correctly

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)

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.

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