Set row color with closedXML based on column value - closedxml

I am trying to write Excel file from datatable with VB.NET. I am able to achieve few excel operations also. Now I want to colour a row based on cell value of a particular column (Col-4 for example). Here is my Excel file -
Here is my Excel file
I want to achieve like this -
Looking some guidance to achieve this.

I suggest you add conditional formatting. For example:
var workbook = new XLWorkbook();
var ws = workbook.AddWorksheet("Sheet1");
ws.FirstCell().SetValue(1)
.CellBelow().SetValue(1)
.CellBelow().SetValue(2)
.CellBelow().SetValue(3)
.CellBelow().SetValue(4);
ws.RangeUsed().AddConditionalFormat().WhenBetween(2, 3)
.Fill.SetBackgroundColor(XLColor.Red);
Reference: https://github.com/ClosedXML/ClosedXML/wiki/Conditional-Formatting

Related

Google script copy data from a sheet to another in efficent way

I have to copy data from one spreadsheet to another.
On the source I have a sheet with columns DataName and DataID
I need to copy DataName column in SSdest sheet('Data'), DataID in SSdest sheet('ID').
I can do it in several ways, but I need to be time effective due to a big load of data. I pick data using Data = Source.getDataRange().getValues()
I can paste in the correct way using a for loop with setvalue() but is time-consuming. Is there a way to paste only one column from getValues data?
as #Casper told you, you can use range.getValues() to get a multidimensional array then use range.setValues(). You only need to set the range to one column to get all the datas wanted as so : sheet.getRange(rowNumber, columnNumber, numberOfRows).getValues().
One other solution would be to select all the datas with getDataRange(), then programatically using javascript create a new array you will then add using setValues(array)

How to read filtered rows in ClosedXML

using (var workBook = new XLWorkbook(file)
I'm reading excel xlsx file with already defined filter.
Let's say that only rows with text 'abc' in column 2 are shown.
I cannot find any way to read this with ClosedXML. I can see that this filter exists by looking inside worksheet.AutoFilter but I cannot find a way to apply this. All the time I'm getting all rows from the worksheet. I want to get only those that are filtered and displayed when I open it in Excel.
Depending on version you use, you can do
var visibleRows = worksheet.RowsUsed(x => x.IsHidden == false);
Any rows that is hidden is filtered out.

How do I highlight all text and wrap in data exported from Access to Excel using VBA?

I am using VBA in Access to export a file into Excel and format the text in Excel. I am trying to highlight all data in Excel (once it has been exported) and then wrap that data using VBA code in Access.
How would I get this task completed?
Trying to use the code below is not working. The data starts in cell A1 and should end with the end xlright ans down (meaning, I do not have a set column or row where the data ends).
objApp.ActiveWorkbook.Worksheets(1).Activate
objApp.ActiveWorkbook.Worksheets(1).Range("A1").Select
objApp.ActiveWorkbook.Worksheets(1).Selection.End(xlToRight).Select
objApp.ActiveWorkbook.Worksheets(1).Selection.End(xlDown).Select
objApp.Selection.WrapText = True
Probably the following would work clearly for you:
objApp.ActiveWorkbook.Worksheets(1).usedrange.wraptext=true

Add validation to entire column

In my current project I need to create an excel file with a list validation on an entire column. Googling turned up with the following two results:
http://www.aspose.com/docs/display/cellsnet/Working+with+Validations+in+Columns
This refers to aspose.cells.griddesktop which actually has the worksheet.Columns[n].Validations property. Aspose.Cells doesn't.
http://www.aspose.com/docs/display/cellsjava/Data+Filtering+and+Validation
All the examples use a CellArea which requires a start- and end row.
Anything I missed?
There are two types of excel formats. One is older XLS format and other is newer XLSX format. The number of rows inside the column in XLS format is 65536 and in XLSX format is 1048576. So you can use the above two values to cover your entire column in the CellArea.
You can also use CellArea.CreateCellArea() static method to create cell area object easily
For XLS format, the following CellArea code covers entire column A
CellArea ca = CellArea.CreateCellArea("A1", "A65536");
For XLSX format, the following CellArea code covers entire column A
CellArea ca = CellArea.CreateCellArea("A1", "A1048576");
Note: I am working as Developer Evangelist at Aspose
Here is another way to cover entire column.
// Cover entire column A
CellArea ca = CellArea.CreateCellArea("A", "A");
This will work with both XLS and XLSX format.
Please see the following sample code, execute it at your end and also read its comments. You will get two output Excel files. One in XLS format and other in XLSX format.
Now enter 200 (or any value greater than 100) in these cells and you will get validation error.
A65536
A1048576
C#
// Create workbook
Workbook workbook = new Workbook();
// Accessing the Validations collection of the worksheet
ValidationCollection validations = workbook.Worksheets[0].Validations;
// Cover entire column A
CellArea ca = CellArea.CreateCellArea("A", "A");
// Creating a Validation object
Validation validation = validations[validations.Add(ca)];
// Setting the validation type to whole number
validation.Type = ValidationType.WholeNumber;
// Setting the operator for validation to Between
validation.Operator = OperatorType.Between;
// Setting the minimum value for the validation
validation.Formula1 = "10";
// Setting the maximum value for the validation
validation.Formula2 = "100";
// Save in XLS format
workbook.Save("output.xls", SaveFormat.Excel97To2003);
// Remove the area of validation and add it again
validation.RemoveArea(ca);
validation.AddArea(ca);
// Save in XLSX format
workbook.Save("output.xlsx");
Note: I am working as Developer Advocate at Aspose

Can I use SpreadsheetGear to read from a CSV file without it formatting the cells?

Given a simple CSV file that consists of a string of digit characters and a date in UK format:
"00000000","01/01/2014"
and code to get the used cells:
IWorkbookSet workbookSet = SpreadsheetGear.Factory.GetWorkbookSet();
IWorkbook workbook = workbookSet.Workbooks.Open(#"C:\file.csv");
IRange cells = workbook.Worksheets[0].UsedRange;
when I access cells[0,0].Text it gives it as 0, because it's treating it as numeric and therefore the leading 0s are meaningless. It will do the same for the date. I'm trying to manually construct a DataTable from the cells, but I need the original values in the file.
I tried:
SpreadsheetGear.Advanced.Cells.IValues cells = (SpreadsheetGear.Advanced.Cells.IValues)workbook.Worksheets[0];
var sb = new StringBuilder();
cells[0,0].GetText(sb);
but nothing is appended to the string builder.
How can I get access to the original file values?
SpreadsheetGear does not make available the original values as found in in the CSV file (such as "00000000" in your case). You would only be able to access cell data after it has been parsed and processed by SpreadsheetGear (i.e., converting the above to a double value of 0). If you need the CSV's original values, then you'll need to open up file yourself and manually process and parse it.
It sounds like you ultimately want a DataTable, but if you still require to create a workbook file from your CSV data, once you've created a routine to manually open and parse each "cell" in your CSV file, you could enter each value into a spreadsheet as Text, so that it is preserved as it is found in the CSV file. You can go about this in two ways:
1) Set IRange.NumberFormat to "#", which will treat any future input into that IRange as Text. Example:
worksheet.Cells["A1"].NumberFormat = "#";
worksheet.Cells["A1"].Value = "00000000";
2) Prepend your inputted value with a single apostrophe, which indicates that you want the input to be treated as text. Example:
worksheet.Cells["A1"].Value = "'00000000";
If you still need a DataTable at this point, you could use the IRange.GetDataTable(...) method to accomplish this. Because the cell data is stored as Text, your DataTable values should also reflect these same values Example:
DataTable dt = worksheet.Cells["A1"].GetDataTable(GetDataFlags.None);
(There is a GetDataFlags.FormattedText option, but this isn't really relevant for your case since the cell data is stored as text anyway and so won't be formatted)