c# excel cell format to hh:mm:ss - c#

I am having a problem while converting text into the format [hh:mm:ss].
When I double click, formatting is fixed and cell value is automatically aligned to the right.
I am using [SpreadsheetStreams.net] NuGet package to create an excel file.
Here what I have already tried:
1.
writer.AddCell(TimeSpan.Parse(columnNames[i].ToString()))
2.
Style test = new Style();
NumberFormat format = new NumberFormat(NumberFormatType.Custom);
format.Custom = "hh:mm";
test.NumberFormat = format;
writer.AddCell(TimeSpan.Parse(columnNames[i].ToString()), test) OR
writer.AddCell(columnNames[i].ToString(), test)
Please refer to this image:
How can I format the cell to [hh:mm:ss]?

After trying many solutions I found a way :
Create a Style object having a custom type like below :
Style timeStyle = new Style();
NumberFormat format = new NumberFormat(NumberFormatType.Custom);
format.Custom = "hh:mm";
timeStyle.NumberFormat = format;
Then pass this as the second parameter and first parameter as a day (in double)
writer.AddCell(TimeSpan.Parse(columnNames[i]).TotalSeconds / 86400, timeStyle );

Related

Syncfusion : unnecessary Date format in print preview in WPF

I'm working with a Syncfusion SFDatagrid in WPF and I want to show date on the string format.
var col = new CustomGridTextColumn();
col.HeaderText = ReportSourceDataTable.Columns[i].ToString();
col.DisplayBinding = new Binding(ReportSourceDataTable.Columns[i].ToString());
if (ReportSourceDataTable.Columns[i].DataType == typeof(DateTime))
{
col.DisplayBinding.StringFormat = "MM/dd/yyyy";
}
In the main application, it works fine.
But when I preview or export those data, then an unnecessary 'MM/dd/yyyy' string format is applied. Below are examples from a print preview
Can anyone tell me how I can remove this unnecessary string format?
You can achieve your requirement to displaying the correct date format in print preview by setting the UseBindingValue in GridTextColumn like below,
<syncfusion:GridTextColumn MappingName="OrderDate" UseBindingValue="True" DisplayBinding="{Binding Path=OrderDate,StringFormat=MM/dd/yyyy}"/>
Regards,
Jai

ClosedXML does not give me option to format column

I'm having an issue when I try to change the format of an entire column.
My issue is that I don't get the XLCellValues operand. I am able to choose XLDataType.Text but that does not work. This is what I have:
eventSheet.Column("A").CellsUsed().SetDataType(XLDataType.Text);
I have also tried replacing the A with 1. No luck that way either. Could it be a newer revision of the ClosedXML package? Thank you!
The IXLCell.SetDataType method does not change the display format. It just changes the type of the underlying value. To change the display format, use IXLCell.Style.NumberFormat
Example:
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Style NumberFormat");
var co = 2;
var ro = 1;
// Using a custom format
ws.Cell(++ro, co).Value = "123456.789";
ws.Cell(ro, co).Style.NumberFormat.Format = "$ #,##0.00";
ws.Cell(++ro, co).Value = "12.345";
ws.Cell(ro, co).Style.NumberFormat.Format = "0000";
// Using a OpenXML's predefined formats
ws.Cell(++ro, co).Value = "12.345";
ws.Cell(ro, co).Style.NumberFormat.NumberFormatId = 3;
ws.Column(co).AdjustToContents();
workbook.SaveAs("StylesNumberFormat.xlsx");
You can find more information on the ClosedXML wiki, e.g. at https://github.com/ClosedXML/ClosedXML/wiki/Styles-NumberFormat

Change data type of excel Cell using Aspose?

We are to show data with Currency symbol ("TRL") in Excel cell with Currency type and we write the excel cell using this data "TRL 100.00" then that cell automatically converted in to General type instead of Currency type although we have changed the format of the particular cell using (styleFlag's property NumberFormat=true)
Please see the following sample code, its comments and the screenshot showing the output Excel file. The code first formats the cell A1 with TRL currency format. Then it formats the entire column C with TRL currency format.
C#
//Create workbook
Workbook wb = new Workbook();
//Access first worksheet
Worksheet ws = wb.Worksheets[0];
//Add some value in cell A1
Cell cell = ws.Cells["A1"];
cell.PutValue(22);
//Format cell A1 with TRL formatting
Style st = cell.GetStyle();
st.Custom = "[$TRL]\\ #,##0.00";
cell.SetStyle(st);
//Make the entire column C with TRL formatting
StyleFlag flg = new StyleFlag();
flg.NumberFormat = true;
ws.Cells.Columns[2].ApplyStyle(st, flg);
//Now enter data in column C cells
ws.Cells["C5"].PutValue(31);
ws.Cells["C6"].PutValue(32);
ws.Cells["C7"].PutValue(33);
ws.Cells["C8"].PutValue(34);
//Save the workbook
wb.Save("output.xlsx");
Screenshot:
Update - I
Screenshot showing the result of this following line.
st.Custom = "[$฿-41E]#,##0.00";
Note: I am working as Developer Evangelist at Aspose
Most easy way is set style number for the cells.
//$1,234.56
ws.Cells[1,1].Value = 1234.56;
var style=ws.Cells[1.1].GetStyle();
style.Number=5; // 5 is build-in style for currency
ws.Cells[1,1].SetStyle(style);
You can find all build-in styles here: https://docs.aspose.com/cells/net/list-of-supported-number-formats/

How to set Text format to a particular column in Excel using Aspose Cells

I am using Aspose Cells and I need to convert a particular column to text format, as the column consists of both numbers and Text, by default the column is taken as number format. I have used
Aspose.Cells.Style style = worksheet.Cells["A3"].GetStyle();
style.Number = 49;
worksheet.Cells["A3"].SetStyle(style);
the above code works only for particular Cell but I need to Set Text format entire column, I have tried using this
Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
style.Number = 49; //Sets the Text format.
StyleFlag flag = new StyleFlag();
worksheet.Cells.ApplyColumnStyle(0, style, flag);
worksheet.Cells.ApplyColumnStyle(1, style, flag);
the above code not working. Is there any other way to fix this?
Thanks in Advance
I got solution for this,
var workbook = new Workbook();
var worksheet = workbook.Worksheets[0];
Aspose.Cells.Style TextStyle = workbook.CreateStyle();
TextStyle.Number = 49;
StyleFlag TextFlag = new StyleFlag();
TextFlag.NumberFormat = true;
worksheet.Cells.Columns[0].ApplyStyle(TextStyle, TextFlag);
worksheet.Cells.Columns[1].ApplyStyle(TextStyle, TextFlag);

c# Datagridview cell not accepting currency format change

I am trying to change the format of a cell in a datagridview using the following code.
this.dataGridView[2, 1].ValueType = typeof(decimal);
this.dataGridView[2, 1].Value = 500;
this.dataGridView[2, 1].Style.BackColor = System.Drawing.Color.OrangeRed;
this.dataGridView[2, 1].Style.Format = "c";
The colour of the cell changes but it doesnt not show in currency format.
Anyone know why?
Are you ensuring that the value assigned to .Value is numeric? (i.e. not a string)
In the examples I've seen, the "c" format is lowercased. You may not need the "2" either.

Categories

Resources