How can I read merged excel items with c# - c#

I have an excel daily program like this:
I can read excel cells in specific rows or cols. But I want to understand, A,B,C,U,M,S values are Sunday values and C,K,M,S,S,L,L,U values are Monday values. Sunday and Monday is merged cells. Sunday and monday programs have 3 columns but they may have 4 or 5 columns sometimes. So I need to read under values.
Is this possible as programmatically in c#?

You should check out the EPPlus library - Create advanced Excel spreadsheets using .NET
https://github.com/JanKallman/EPPlus
There is a MergedCells property on the ExcelWorksheet class. You will need to evaluate this property and figure out the column size of Sunday & Monday.
Also, please see the following StackOverflow posts:
Get Merged Cell Area with EPPLus
Handle merged cells in Epplus Excel conversion to HTML

Below are the following libraries that could help you parse the merged cells with c#:
EPPlus, Aspose, Microsoft Interop, Spreadsheet gear

Related

Select, Copy, and Re-Insert a range of Excel Rows Using C#

Using C# to replace an existing and now defunct legacy routine in Delphi, I’ve been trying re-develop a routine in C# that worked well in Delphi 7 that will open each of my 30 Excel files, select the last 22 rows in each, copy them, and reinsert them back into their respective files.
For example, I find the last row, August of 2019 has 22 business days so I begin my range 22 rows back from the last row.
Using the Excel file, I want to select the rows that range from A4157 to A4178. Then, I want to take the rows I’ve copied and insert them after row A4156 or before row A4157. I then take a separate list of August dates and write them to each of the lower 22 from A4178 to A4200. July is then complete and rows for August are ready to go.
I retain the last row because additional calculations on other worksheets in the same workbook refer to the last row for data and they will index automatically from A4178 to A4200 doing it this way.
As each entry made during the month, its data are copied down to the last row so the last row is always up to date. Another sheet in the workbook uses up-to-date data in its summary.
So far, I can open the Excel workbook and get the right sheet.
The following does highlight the proper row but I get a runtime error. I’m not sure what that means.
I’ve commented out the “select” line and run the “copy” line with the same result. I’m now into my second week trying to work out this problem. I need an example of a functioning routine if possible. A link to a text on C# - MS interfacing would be a great help.
Excel.Range selectRange = excelWorkSheet.Range[excelWorkSheet.Cells[A4157], excelWorkSheet.Cells[A4178].select;
Excel.Range copyRange = excelWorkSheet.Range[excelWorkSheet.Cells[A4157], excelWorkSheet.Cells[A4178]].select;
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot implicitly convert type 'bool' to 'Microsoft.Office.Interop.Excel.Range''
I'm not 100% clear on what you want in your final worksheet. But I think you're trying to do something like this.
// In your example startRow = 4157 and lastRow = 4178.
// Get the rows to copy (rows 4157 to 4178 in your example).
Excel.Range copyRange = excelWorkSheet.Rows[startRow + ":" + lastRow];
// Insert enough new rows to fit the rows we're copying.
copyRange.Insert(Excel.XlInsertShiftDirection.xlShiftDown);
// The copied data will be put in the same place (starting at row 4157 in
// your example).
Excel.Range dest = excelWorkSheet.Rows[startRow + ":" + lastRow];
copyRange.Copy(dest);
Take a look at Epplus. https://github.com/JanKallman/EPPlus. It's available as a Nuget package.
There is a tutorial in the Github Wiki and a sample app that exercises many of the features.

Is Open XML SDK 2.5 able to recalculate Excel formulas?

I am struggling with the Open XML SDK and I've already read a lot of posts on this topic but cannot figure it out. My goal is to have a locally created Excel file which contains a formula and edit the input online and retrieve the calculated value online.
I don't know if this is possible since Open XML may only change the data and I wonder if it is also able to perform Excels calculations.
For example, my local file contains three cells:
A1: 1
A2: 2
A3: =(A1+A2)
Using Open XML I adjust A2 to the value of 3, however the result of A3 remains 3 instead of 4.
I have already read about Excel having to recalculate, but my goal is to have an Excel file as some sort of calculation engine instead of transfering all calculations to C#.
All tips and advice are welcome.
Kind regards, Patrick
First of all thanks for all responses.
Second I guess the response answered my question and the open XML SDK is only able to adjust the file and won't do anything regarding recaculating existing formulas in the file. This will only occur when opened in Excel. I will take a look at EPPlus.
You can use something like this.
Cell cell; //supposing this is your cell referencing A3
CellFormula cellformula = new CellFormula();
cellformula.Text = "SUM(A1, A2)";
CellValue cellValue = new CellValue();
cellValue.Text = "0";
cell.Append(cellformula);
cell.Append(cellValue);
A similar example can be found at this link: Formula cells in excel using openXML
I feel there could be some ambiguity captured in the question. OpenXML is way of storing documents, therefore it is not possible to do calculations with OpenXML SDK. It is spread sheet engine (Excel application) which performs the calculations.
When inputs are updated, spreadsheet saved, calculated values should get updated.

Insert columns form one excel book to another in C#

I need to take values form one sheet in one Excel workbook and insert them into another existing workbook.
The values I need to take are the first 6 columns of the first file:
And I want to insert them at the beginning of another book like so
I've been using Spire.Xls to read values from the first sheet and I thought I could just do the same; parse the worksheet, read the values and just paste them into the other sheet, but that wouldn't work because three of the columns I want to copy have the same header "Descripcion" so my parser would only take the values form the first descripcion column and skip the other ones.
Is there any way, using Excel.Interop or maybe Spire itself to copy and paste entire columns between workbooks? Or alternately, is there any way to get all of the 3 "descripcion" values (without rewriting the title of the columns)?
VSTO might be helpful. I've done similar tasks in C#/VSTO.
Perhaps read through: Simple Example of VSTO Excel using a worksheet as a datasource

Using LINQ with Open XML (Excel)

I have an excel sheet and I am processing it by using Open XML SDK 2.0. The scenario is, There is a column which contains date in my excel sheet. I need to get the maximum and minimum date from that column.
I can do this by looping and reaching to that cell, doing comparisons and finding desired answer.
But due to optimality I want to do this by using LINQ to get Minimum and maximum dates.
Is it possible to do so? If yes, then how?
You can see how to get IEnumerable of all cells from column there:Read excel sheet data in columns using OpenXML, and use Max() on it.
Thanks to all
I have used like this
IEnumerable<Cell> cells = workSheetPart.Worksheet.Descendants<Cell>().Where(c => string.Compare(GetColumnName(c.CellReference.Value), strIndex, false) == 0).OrderBy(c => c.CellValue.Text);
And getting min and max values like this
int cellCount = cells.Count();
Cell MaxCell = cells.ToArray()[0];
Cell MinCell = cells.ToArray()[cellCount - 1];
You will want to take a look at the LINQ Min() and Max() functions. If you need to return the entire Cell object, you can use OrderByDescending().
You could read this post Open XML SDK and LINQ to XML by Eric White (Eric wrote a lot of posts about OpenXML and LINQ). And then you will be able to query your Excel file data with LINQ. You might want to see the spreadsheet objects structure in The Open XML SDK Productivity Tool, which can generate the source code for you. You could use this code to understand how to programmatically access the data you need.

Reading integers from Excel using oledb

I am using oledb to read data from an excel file and store it in a dataset.
My excel file contents are like as follows:
0 0 somestring somestring
500 200 somestring somestring
When i checked the contents of my data set, the values of Columns 1 & 2 are not stored as integers but rather as DateTime values.
How will I make it be stored as integer values instead of DateTime?
Have you tried adding IMEX=1 to your OLEDB connection string?
Are you sure its a number? Following could be a few options:
Right click the columns in excel and change the format to Text/Custom.
Look into the NamedRange.FormatConditions Property; change the format the data when you read it from excel, see MSDN
Or try deleting an existing format on a range:
that is,
Excel.Worksheet sheet = this.Application.ActiveSheet as Excel.Worksheet;
Excel.Range range = sheet.get_Range("A1", "A5") as Excel.Range;
//delete previous validation rules
range.Validation.Delete();
You could use a 3rd party component like SpreadsheetGear for .NET which lets you get the underlying values of cells (with IWorkbook.Worksheets["MySheet"].Cells[rowIndex, colIndex].Value) regardless of the cell format, or you can get the formatted result with IRange.Text.
You can see live ASP.NET samples here and download the free trial here.
Disclaimer: I won SpreadsheetGear LLC

Categories

Resources