Date validation for OpenXml Spreadsheet - excel

I am trying to create date validation on an xlsx column so far I have:
DataValidation dataValidation = new DataValidation
{
Type = DataValidationValues.Date,
AllowBlank = false,
ShowErrorMessage = true,
ErrorTitle = "Invalid value entered",
Error = "Please enter a valid date in dd/mm/yyyy format",
SequenceOfReferences = new ListValue<StringValue> { InnerText = "A2:A10000" }
};
if I open an xlsx document and select the date validation type there are fields for the following:
Data: Between, Greater than, less than
Minimum
Maximum
How can I set these programmatically?

This gives you a date time validation for column A rows 2 to 10,000, set your start date with formula1 (the number of days past 1900) and end date with formula2 which is the number of days after the start date (in this case 01/01/2021)
DataValidation dataValidation = new DataValidation
{
Type = DataValidationValues.Date,
AllowBlank = false,
ShowErrorMessage = true,
ShowInputMessage = true,
ErrorTitle = "Invalid value entered",
Error = "Please enter a valid date in dd/mm/yyyy format",
SequenceOfReferences = new ListValue<StringValue> { InnerText = "A2:A10000" },
Formula1 = new Formula1("1");
Formula2 = new Formula2("44196");
};

Related

Flutter/Dart: Excel Date to Date Object

I am parsing an excel document in excel using the excel: ^1.1.5 package. In my sheet, i have a Date column and this Date is being received in my Flutter code in the following format
"44663"
rather than:
"2022/04/12"
How do I parse this to a format such as YY-MM-DD.
I have tried DateTime.parse(), but it throws an error that my date format is invalid.
I found the answer :
const gsDateBase = 2209161600 / 86400;
const gsDateFactor = 86400000;
final date = double.tryParse("44663");
if (date == null) return null;
final millis = (date - gsDateBase) * gsDateFactor;
print(DateTime.fromMillisecondsSinceEpoch(millis.toInt(), isUtc: true));

exceljs read Date Cell with format

I've a problem when I'm reading a Date value from columns in a excel file using exceljs package, I can't get the truth value in the correct format like a Date data type, instead I get a number.
My project is build with Nest, and I use exceljs to write/read xlsx files, the code is:
worksheet.eachRow({ includeEmpty: true }, (row, rowNumber) => {
row.eachCell((cell, colNumber) => {
...
const header = headers[colNumber - 1];
switch (header) {
...
case 'dateColumn':
// Date format is dd/mm/yyyy
// Original value in excel is '16/06/2020'
const cellValue: Date = cell.value as Date; // When i get value from cell is 43998
console.log(cellValue); // print 43998
break;
...
}
}
The cell format is 'dd/mm/yyyy'
You will need to convert Excel Date Serial Number to JS Date.
Here is a very basic example:
const date0 = new Date(0);
const utcOffset = date0.getTimezoneOffset();
const cellValue = new Date(0, 0, cell.value - 1, 0, -utcOffset, 0);
To convert Excel Serial Date to normal you can use
new Date(Date.UTC(0, 0, cell.value - 1, 0, 0, 0))

Can we use Cell Range Validation to populate a dropdown list in excel from another sheet in EPPLUS

I am new to using EPplus. I actually wanted to populate a dropdown list in Sheet1 from a list of values in Sheet2. This can be achieved by Cell Range Validation in Excel.But I am not sure whether EPPlus support this programmatically. It will be really helpful if anyone could help me out.
Populate a dropdown in this Designation Column from Designation Column in Sheet 2.
Sheet 2
Here's how to do it:
var departmentSheet = excel.Workbook.Worksheets.Add("department");
departmentSheet.Cells["A1"].Value = "Management";
departmentSheet.Cells["A2"].Value = "Administrator";
departmentSheet.Cells["A3"].Value = "Quality Engineering";
departmentSheet.Cells["A4"].Value = "Enablers";
var designationSheet = excel.Workbook.Worksheets.Add("designations");
designationSheet.Cells["A1"].Value = "Developer 1";
designationSheet.Cells["A2"].Value = "Developer 2";
designationSheet.Cells["A3"].Value = "Developer 3";
designationSheet.Cells["A4"].Value = "Developer 4";
// add a validation and set values
// the range of cells that will contain the validation
var validation = departmentSheet.DataValidations.AddListValidation("B1:B4");
// set validation rules as required
validation.ShowErrorMessage = true;
validation.ErrorStyle = ExcelDataValidationWarningStyle.warning;
validation.ErrorTitle = "An invalid value was entered";
validation.Error = "Select a value from the list";
// set the range that contains the validation list
validation.Formula.ExcelFormula = $"={ designationSheet.Name }!$A$1:$A$4";

RubyXL : How can I make a cell as dropdown in XLSX

I am using RubyXL gem to read and write a xlsm file.In my project there is a field Country which should diplay all list of countries in a dropdown in xlsm.I have tried with DataValidation but when I tried to open the file it is showing as 'We found a problem with some content in filename.xlsm.Do you want to try us to revcover as much as we can?' and if I clicked 'yes'it is not showing dropdown list on that cell.My code is given below.
workbook = RubyXL::Parser.parse(dest_file_path)
worksheet1 = workbook["Form"]
content = ['Afghanistan','Albania,'Algeria']
formula = RubyXL::Formula.new(expression: content)
loc = RubyXL::Reference.new(1, 1048000, 2, 2)
worksheet1.data_validations =
RubyXL::DataValidation.new(prompt_title: nil, prompt: nil,
sqref: loc, formula1: formula,
type: 'list', show_input_message: true,
show_drop_down: true)
How should I associate DataValidation to a specific worksheet ? Is there any other way I can do this? Could anyone please help.
RubyXL Data Validations are collection, wrapping your Data Validation in brackets should add appropriately add the data validations to worksheet1
workbook = RubyXL::Parser.parse(dest_file_path)
worksheet1 = workbook["Form"]
content = ['Afghanistan','Albania,'Algeria']
formula = RubyXL::Formula.new(expression: content)
loc = RubyXL::Reference.new(1, 1048000, 2, 2)
worksheet1.data_validations = [
RubyXL::DataValidation.new(prompt_title: nil, prompt: nil,
sqref: loc, formula1: formula,
type: 'list', show_input_message: true,
show_drop_down: true)]

Unable to fetch the exact date value from excel cell

I am fetching the value present in the excel cell which is a date like 20-3-2004 using the following code:
string logResult = null;
string ResultFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + ResultFile;
Microsoft.Office.Interop.Excel.Application myapp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = myapp.Workbooks.Open(ResultFilePath);
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
var cell = (Microsoft.Office.Interop.Excel.Range)sheet.Cells[row, col];
logResult = cell.Value.ToString();
but in the logresult I am always getting date in format 20/3/2004 . please suggest how get the exact format of the date which is written in the cell.
A date value has no format. It is the code that you use to display it that present that value is some form on video.
In your case, it is the ToString() call that trasform whatever has been read on the cell in a string. If it is a date then it trasform it according to the current international settings of your machine.
You could force the ToString() method to use a particular format applying a Format Mask parameter like
logResult = cell.Value.ToString("d-M-yyyy");
See the topic on MSDN about DateTime.ToString()

Resources