EPPlus Date Formatting in Excel - excel

I need to format the date in Excel while writing in the Excel sheet.
It should display this format "MM/dd" but when click on this then formula bar show the complete date "MM/dd/yyyy".
For reference I have attached the screenshot as well. Please check and suggest how I can achieve this in EPPlus Code.
I have tried the following code but no luck:
namedWorksheet.Cells["E4"].Style.Numberformat.Format = "M/dd/yyyy";
namedWorksheet.Cells["E4"].Value = DateTime.Now.ToString("MM/dd/yyyy").Substring(0,4);

Here is the solution for this issue.
Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL")
{
DateTimeFormat = { YearMonthPattern = "MM/dd" }
};
namedWorksheet.Cells["G4"].Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.YearMonthPattern;
var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;
namedWorksheet.Cells["G4"].Value = date;

Related

How to get computed values from formula cells using ExcelJs. It is returning formula instead of value that I am on seeing on excel

I am reading an excel file using ExcelJs. However, when I got formula cells, It is not giving me value but giving formula.
Can any body help please.
var Excel = require('exceljs');
let workbook = new Excel.Workbook();
var FormulaParser = require('hot-formula-parser').Parser;
var parser = new FormulaParser();
workbook.xlsx.readFile('test.xlsx').then(function() {
var worksheet = workbook.getWorksheet(2);
var row = worksheet.getRow(1);
console.log(row.getCell(1).value); //5
console.log(row.getCell(2).value); //8
console.log(row.getCell(3).value); // =IFERROR(MIN(MAX($A1-$B1,0),$E1),"")
console.log(row.getCell(3).text); // blank
//I also tried with using FormulaParser library as below but getting null.
console.log(parser.parse(row.getCell(3).formula).result); // null
});
The result is here:
console.log(row.getCell(3).result);

Am unable to read the date field from excel file selenium reads the date as Col=9=43070 Col=10=43070 Col=11=42931 Col=12=43296

Selenium is incorrectly reading the date as 43095 when I enter 26-12-2017. How to get Selenium to read the correct date?
for (int i=0;i<=TcRow;i++)
{
for (int j=0;j<TcCol;j++)
{
Cell Cell=TcSheet.getRow(i).getCell(j);
}
}
Am I reading the format incorrectly?
TcSheet.getRow(i).getCell(j).setCellType(Cell.CELL_TYPE_STRING);
What changes do I need to do here to make sure they read both the string and the date field?
data[i][j]=TcSheet.getRow(i).getCell(j).getStringCellValue();
}
I also faced the same issue during reading the excel file where I'm fetching date is formatted in dd/mm/yyyy format and selenium fetching wrong value.
For that, I have used DataFormatter. It will returns Excel cell value with format e.g. Date format 15-04-208 in excellent then it will returns date with same format. Look below code that I used in my Framework. Hope it will also work for you.
FileInputStream fis = new FileInputStream("path\\to\\file.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(worksheet);
DataFormatter formatter = new DataFormatter();
Cell cell = sheet.getRow(rowNum).getCell(cellNum);
String cellValue = formatter.formatCellValue(cell);
System.out.println(cellValue);
return cellValue;
Let me know if you have any query.

openxml sdk excel total sums

i am having difficulties with open xml sdk:
i am trying to generate excel file with several columns that have numbers and i want to have total sum at the end
i have tried to Generate Table Definition Part Content and inside define every column (id, name etC). If column has true for TotalColumn, it adds code (rough example)
var column = new TableColumn{
id = 1,
name = "example",
TotalsRowFunction = TotalsRowFunctionValues.Sum,
TotalsRowFormula = new TotalsRowFormula("=SUBTOTAL(109;[" + rowName + "])")
};
I can't get it to work, when i open excel it reports error, but it doesn't explicitly says what the problem is... I tried with microsoft validator but can't figure anything out...
I'd appreciate any help / example code since i can't google anything out
EDIT:
i use this at the end:
workbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
workbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
Why not use a cell formula?
E.g.
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
cell.CellFormula = new CellFormula(string.Format("=SUBTOTAL({0};[{1}])", "109", rowName));
//This will force a full recalculation of all the cells
workbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
workbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
I ended using EPPlus for this as it seems to be working simple and efficient

In C#, Excel cell's custom date format not working with EPPlus

I'm having trouble with Excel's custom format using EPPLUS. Here's my code:
var destFile = new FileInfo(#"C:\temp\test1.xlsx");
var fileName = "test1";
using (ExcelPackage pck = new ExcelPackage(destFile))
{
pck.Workbook.Worksheets.Add(fileName); // Create the worksheet in package
pck.Workbook.Worksheets[fileName].Cells["A2"].Value = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
pck.Workbook.Worksheets[fileName].Cells["A2"].Style.Numberformat.Format = "d-mmm-yy";
pck.Save();
}
I'm getting the following:
The custom format is showed right, but the value in the cell doesn't display the format needed. Here's what I'm trying to get:
Note: I need the full date value DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") for other files, but the custom format is all I need for this file.
What do I need to change to get this to work?
Thanks to #PaullAbbott, here's the correct answer:
pck.Workbook.Worksheets[fileName].Cells["A2"].Value = DateTime.Now;
This displays the results that I needed.

JXL datetime no need the part time

Hello i wrote this code to add a Date in excel but when the cell is added he show also the time. I want eliminated the time part. Thank you in advance if someone can help ..
Tabla[tabReg][tabCol]); is String Array
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = dateFormat.parse(Tabla[tabReg][tabCol]);
DateFormat df = new DateFormat("yyyy/MM/dd");
WritableCellFormat wdf = new WritableCellFormat(df);
cf = new WritableCellFormat(df);
cell = new jxl.write.DateTime(exCol,exReg, convertedDate);
cell.setCellFormat(wdf);
sheet2.addCell(cell);
The solution was to put the format when you create de cell.
cell = new jxl.write.DateTime(exCol,exReg, convertedDate,wdf);

Resources