C# Few issues regarding SpreadsheetLight usage - c#

1) i am using SpreadsheetLight library and i like to know how could i set row's color red or yellow ?
2) also tell me how could i set color range wise say Range["A1:Z1"] ?
3) how to apply format cell range wise ?
sheet.Range[DataRangeCoordinate].NumberFormat = "#,##0.000;[Red](-#,##0.000);#,##0.000";
the above code is devexpress spreadsheet related. so how to do the same when working with SpreadsheetLight ?
4) how to iterate in all cell value with in For loop ?
when i am using dev express spreadsheet grid then i use below code to set back & fore color
sheet.Range["A1:Z1"].Font.Color = Color.IndianRed;
sheet.Range["A1:Z1"].Fill.BackgroundColor = Color.LightGray;
sheet.Range["A1:Z1"].Style.Font.Bold = true;
5) How to set column width for all column ?
6) How to set autofit all columns ?
7) i am getting error when i am trying to create CreateStyle
my code as follows
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using SpreadsheetLight;
SLStyle style1 = sl.CreateStyle();
style.Fill.SetPattern(PatternValues.Solid, System.Drawing.Color.IndianRed, System.Drawing.Color.LightGray);
sl.SetCellStyle(1, 0, style1);
i have installed latest version of OpenXml from Nuget.
please help me with code sample. thanks

Did you check the developer documentation of spreadsheetlight?
They provide an example here: http://spreadsheetlight.com/downloads/samplecode/StyleRowColumnCell.cs

Related

Merging excel cells based on the cell value using c#

I am using excel with c# I want to merge all adjacent cells that contain a specific value using c# code.
I want to merge all the cells that contains the value (Merged cells) using c# code
i want it to be like this
First, you need to identify the cells where the range of repetitive value starts and where it ends.
Then, use this code to merge the cells:
String startRange = "A1";
String endRange = "D3";
String repetitiveValue = "Merged Cells";
Microsoft.Office.Interop.Excel.Range range = worksheet.Range[startRange, endRange];
range.Value2 = repetitiveValue;
range.Select();
range.Merge(Missing.Value);
You need to use one or the other Office library, which can help you with reading/writing spreadsheets & also with the cell merges.
Check SpreadSheetGear.net or PowerTools for OpenXml (for >= office 2007 formats)
http://ericwhite.com/blog/powertools-for-open-xml-expanded/

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.

How can I hide a large number of columns in an excel spreadsheet using EPPlus?

I am using EPPlus version 3.1.3 to create a spreadsheet and I want to hide all of the columns from column L to column XFD and all the rows from the bottom most row to the end. I'm trying to hide the columns by using:
for (int i = 12; i <= 16384; i++)
{
worksheet.Column(i).Hidden = true;
}
This takes forever though for this loop to run. Does anyone know of an alternative way to hide a large amount of columns? I also have no idea how to hide the rows.
I'm wondering if there is another solution outside of EPPlus, but I really don't want to add another library to this.
I've found a solution for the columns.
I wanted to hide columns 10 to 16384 (last). The following Code did the trick and has a good performance.
//EPPlus 4.04 is used.
Dim col As ExcelColumn = osheet.Column(10)
col.ColumnMax = 16384
col.Hidden = True
Does either of these work?
worksheet.columns("L:XFD").Hidden=True
or
worksheet.columns("12:16384").Hidden=True
(please forgive me if these are miles away as I don't know EPPlus too well)
EDIT
I think Sean Cheshire's comments answer your question?
worksheet.cells("L:XFD").Hidden=True
The reference he provided seems to confirm this: EPPlus - Working with multiple columns by index rather than Alphabetical Representation

Set Conditional IconSets in Excel

When adding conditional formatting to a sheet using c#
How do I set the IconSet being used?
Below isn't currently working. It gives me a set of default icons but not the ones I want.
Excel.IconSetCondition cfIconSet =
(Excel.IconSetCondition)excelWorksheet.get_Range(cellNumber, cellNumber)
.FormatConditions
.AddIconSetCondition();
cfIconSet.IconSet = Excel.XlIconSet.xl3Flags;
Solved
cfIconSet.IconSet = cfIconSet.IconSet(Excel.XlIconSet.xl3Flags);

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.

Categories

Resources