How can I read data from xlsm file in C#.Net?

@nitin495,
You can achieve this by implementing the LightCellsDataHandler interface. When encountering columns that you need to save, make both StartCell (int column) and ProcessCell (Cell cell) return true, while other columns return false.
Please refer to the following documents:

Hi,
How can I import data in particular column(D3,D4,D5,D6…) of xlsm file, Also I want to read particular cell of XLSM file Using .Net Core application.
Thanks
Nitin Sharma

@nitin495,

We already told you that you can use different ways to accomplish your task. You may choose any of the below approaches:

1). You can use Cells.ImportData method (see the document on importing data for your reference) to import data from various data sources into your desired cells. For example, to import data starting from D3 cell, the line of code can be:

worksheet.Cells.ImportDataTable(dataTable, true, "D3");

2). You can use Smart Markers feature for the task.

3). You can achieve this by implementing the LightCellsDataHandler interface. When encountering columns that you need to save, make both StartCell (int column) and ProcessCell (Cell cell) return true, while other columns return false. Please refer to the document on using LightCells mode for your complete reference.

See the document on how to read/retrieve data from different cells in Excel spreadsheet via Aspose.Cells for your complete reference.

@amjad.sahi
Last time I was working with .Net 4.8 framework. This question is around .Net Core 5.0.
I am trying to build Core web api and using same code which was earlier suggested but in .net core web api getting below error.
image.png (4.7 KB)

@nitin495,

Thanks for the screenshot.

The error is not related to Aspose.Cells APIs by any means. It is related to VS.NET and it might be related to configurations. Please browse and try online resources for assistance and figure your issue out.

Hi Amjad,
In the same solution rest of the code is working fine. But when I am trying to access the workbook (Workbook workbook = new Workbook(filepath)) this line is throwing the exception attached as image in previous comment. Also the same code was working fine with .net 4.8 framework.
In a new solution I am using .Net Core3.1. Does Aspose.Cells is compatible with the .Net Core3.1 framework?

@nitin495,

Yes, Aspose.Cells for .NET is compatible with .NET Core 3.1. Which version of the APIs you are using? Please try using latest version of Aspose.Cells for .NET v23.4, please use/add reference to relevant Aspose.Cells.Dll from “\netstandard2.0” sub-folder at your installation directory (if you have manually downloaded the version from Downloads section) or import/install Aspose.Cells from Nuget repos.

In case you still find the issue with latest version of Aspose.Cells for .NET, kindly do provide a standalone sample VS.NET project (please zip the application prior attaching) with resource files, we will check it soon.

Hi @amjad.sahi
When I am writing the values for very first time using worksheetStramData.Cells.ImportArray(dataStream4, 14, 3, true); this code the very first time it is taking around 2 seconds.If I am writing same data in subsequent request it is taking around .5 sec.
May I know why is this time difference and How can I take this time down to around .5 sec everytime?

@nitin495,

When importing an array into Excel spreadsheet, the time cost may depend upon the array size and type of data inside it. Moreover, initialization of array with its elements may take time and will also add to time cost. So, you may also evaluate/check the time of the underlying array initialization and its creation with data into account for first time.

If you still think it is an issue with the APIs, kindly do provide a standalone sample console application, zip the project (with resource files) and provide us, we will check it soon.

@amjad.sahi
Please find the sample file and code for writing the data. Here issue is that I have 1 button which is responsible to read and write the data also it have 2 text boxes which have read and write time. When I run this application very first time it is taking almost 2 secs but when I click button 2nd time the time would reduced to less than a second. How can I reduce time to less than second at very first time?

@nitin495,

Thanks for the template file and sample.

I checked your sample project with template file. Could you please try commenting out the following line of code in “WriteDataFromAspose()” method:
workbook.CalculateFormula();

This is necessary when you are retrieving the results in “ReadFileFromAspose()” method though:

Let us know if it makes any difference on your end?

@amjad.sahi
When I run the code very first time which I has given to you is taking around 2 seconds. But when I run it 2nd time and in further subsequent request it is taking few milliseconds. I am not sure what is the main reason of this time difference. Also I have changed the code suggested by you, I move workbook.CalculateFormula() this line of code to ReadFileFromAspose() but difference is still there.
For reference I am attaching screen shot you can see there is significant time difference in “Total Time(ms)” field when I run it 1st and 2nd time.

image.png (6.7 KB)
image.png (5.4 KB)

@nitin4,

Did you first add/import data to worksheets in the template file using “WriteDataFromAspose()” and re-saved the file, then used ReadFileFromAspose() method to open the updated file via Aspose.Cells APIs to get the resultant values (after using workbook.CalculateFormula())?

Could you please share “newfile4.xlsm” which is not touched/processed by “WriteDataFromAspose()” method at all. We will test it soon.

@amjad.sahi
Yes, First I am writing the data in sheets “Data from EB” and “Stream data (EB)” and there are formulas written in file which update the few sheet cells “DP-Calcs” and after writing the values I read sheet “DP-Calcs”.
I am attaching the sample file which is fresh and not touched by Aspose because of some privacy concern I am deleting sheets which are not required rest of sheets are therein the file.

file.7z (130.3 KB)

@nitin495,

Thanks for the template Excel file.

We need to evaluate/investigate your issue in details. We will check why it takes more time in first call. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-53327

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@amjad.sahi
Do I need to call any particular method of Aspose before reading a cell where macros are written? Macros are running fine on my local machine.

@nitin495,

Do you mean the cell values/results are based on execution of macros/vba codes in Excel spreadsheet? If so, I am afraid, Aspose.Cells does not support to run/execute macros/vba codes, it only supports to add/manipulate macros/vba codes.

@nitin495

We will init some objects when first running application. It will take some time.
So please init a workbook when you start appliction as the following :

 using (MemoryStream ms = new MemoryStream())
            {
                Workbook workbook = new Workbook();
                workbook.Save(ms, SaveFormat.Xlsx);
            }
     Application.Run(new Form1());

@amjad.sahi
As aspose cannot run macro, so I am trying to run macro function on C# code.

How can I get the macro definition in my c# code along with cells which are input parameters for this macro?

Regards,

Nitin

@nitin495
Please check document Manage VBA codes of Excel Macro-Enabled workbook.|Documentation