I tried to set NumberFormat in a column:
_worksheet.Columns[index].NumberFormat = "#,##0.00";
but I have the result (custom format):
This is similar to recorded macro in the Excel VBA:
Columns(25).NumberFormat = "#,##0.00"
and I have the result (numeric format):
Where is the problem in my first code?
How to set the numeric format?
I have the MS Excel 2016.
EDIT:
When I try to write the number in a formated cell, I have the view:
When I record setup format which I setup by VS, I have the result:
Selection.NumberFormat = "#.##0\.00"
Based on my test, your code can set the numeric format successfully.
Also, I find that you can use NumberFormatLocal to set the format.
Here is a code example you can refer to.
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Open("E:\\1.xlsx");
Excel.Worksheet worksheet = workbook.Worksheets[1];
worksheet.Columns[1].NumberFormat = "#,##0.00";
worksheet.Columns[2].NumberFormatLocal= "#,##0.00";
workbook.Save();
workbook.Close();
You can see the following result:(123.456 ->123.46 ;456.789->456.79 and format is
Number)
Related
We have a process to return results of a search, and export the data to Excel. It's converting the results to a data table, and adding the data table to a blank worksheet:
var wb = new XLWorkbook();
wb.Worksheets.Add(ds);
return wb;
The users want some formatting added to the sheet - setting date fields to display as MM/DD/YYYY, for example.
Can't find the right reference to get to the worksheet in the new workbook, however.
Also unsure if one can set a format to a column, as opposed to the code below which I think is converting it to a string,
((Excel.Range)sheet.Cells[i, 2]).EntireColumn.NumberFormat = "yyyy-MM-dd";
sheet.Cells[i, 2].Value2 = Convert.ToDateTime(dr["date"].ToString());
Anyone?
EDIT - #ScottHannen identified the library I should be investigating, and I've made more progress. I have drilled into the worksheets and successfully added the formatting, but it's not reflecting as such in the final document.
Here's the code:
var wb = new XLWorkbook();
wb.Worksheets.Add(ds);
foreach (IXLWorksheet ws in wb.Worksheets)
{
if (value == 1)
{
ws.Column(16).Style.DateFormat.Format = "yyyy-MM-dd";
ws.Column(17).Style.DateFormat.Format = "yyyy-MM-dd";
}
else
{ }
}
return wb;
When I look at the final document, the dates are still displaying as verbose - 12/7/2017 12:00:00 AM - but the formatting IS in the document. If I do the manual "hit enter in the cell and exit" thing, the formatting updates, as does doing Text to Columns.
Of course, I don't WANT to do that, I want the formatting to take straight away.
I assume the issue is that I'm adding the data first and formatting it after, just like a regular sheet. But is there a command in there to refresh/apply/what have you the formatting?
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/
I have multiple excel files, i need to create a small tool(C#) where i need to create a log file to tell, which excel file and cell has more than 2 decimal places in it.
Also is it possible to highlight on excel cell which has more than 2 decimal places
yes it's possible, i tried to iterate through all active(cell has value) cells
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Workbook wb = excel.Workbooks.Open(path);
Worksheet excelSheet = wb.ActiveSheet;
var test = excelSheet.Cells[x, y].Value.ToString();
int chk = GetDecimalCount(decimal.Parse(test));
Tool is now ready
I am using c# to color particular cells of excel file.
I am using:
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(destPath);
Worksheet ws = wb.Worksheets[1];
ws.get_Range(ws.Cells[row, clmn]).Cells.Interior.Color = 36;
...to color cells, but this is not working.
Can anyone help me out?
Try something like that
ws.Cells[row, clmn].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
Cells[row, clmn] is a range so you don't need to call get_Range() and there is a enum that you can use for colors.
ws.Cells[row, clmn].Interior.Color = XlRgbColor.rgbBlack;
If you want to set color by color index, you need to use this method:
Cells[row, col].Interior.ColorIndex = 36;
You can color a cell or a entire column or entire row.
The below code will help you out.
xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 2], xlWorkSheet.Cells[2, 4]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
else
xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 3], xlWorkSheet.Cells[2, 3]).Interior.Color = Excel.XlRgbColor.rgbRed;
Here xlWorksheet is the object excel Worksheet object.
get_Range takes 2 variable one start cell and other is end cell.
so if you specify both the values same then only one cell is colored.
xlWorkSheet.cells[row, column] is used to specify a cell.
System.Drawing.ColorTranslator.ToOle(SystemDrawing.Color.Green) is used to define the color in OLE format.
Excel.XlRgbColor.rgbRed is a excel way of coloring the cells
This method gives access to large number of colors which can be found here list of colors
The below code is the way i defined the excel worksheet.
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range xlwidthadjust; //used this to adjust width of columns
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
with this code i am sure that you wont get this exception Exception from HRESULT: 0x800A03EC
Make sure you are using:
using Excel = Microsoft.Office.Interop.Excel;
If you have a variable for the range you want to change, then use:
chartRange = xlWorkSheet.get_Range("a5", "a8");
chartRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
If you want to just change the color of a specific cell, then use:
xlWorkSheet.Cells[row, col].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
...where 'row' is the row number, and 'col' is the column number assigned to the given lettered columns (starting at 1).
Exception from HRESULT: 0x800A03EC
Solution: Change the misValue to sheet1, sheet2 or sheet3.
xlWorkBook = xlApp.Workbooks.Add("sheet1");
This works for me. system.reflaction.missing.value what was that, it is not related to Excel.workbooks.add came from a Excel file default value.
When you create a Excel file, the default worksheets are sheet1, sheet2 and sheet3.
I currently have an excel sheet with one of the columns being in the date format.
What I see when I open up the spreadsheet is something like 12/29/09 and the program sees 40176.
I figured out this is the value present when I change the column to general text.
My question is how can I read the value 12/29/09 instead of 40176 or how can I change 40176 into a valid date?
My program is in c# Must be read in in c#
Here is sample code of my connection if it helps any.
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
string myPath = #"C:\Test.xls";
excelApp.Workbooks.Open(myPath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "t", false, false, 0, true, 1, 0);
Microsoft.Office.Interop.Excel.Sheets sheets = excelApp.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
excelApp.Visible = true;
if(((Microsoft.Office.Interop.Excel.Range)excelApp.Cells[r, 1]).Value2 != null)
DateString = ((Microsoft.Office.Interop.Excel.Range)excelApp.Cells[r, 1]).Value2.ToString();
You can use DateTime.FromOADate() to convert the double into a DateTime value.
As Reed Copsey said, the DateTime.FromOADate() method will convert the value into a DateTime. If, however, you want 12/29/09 as a string, and don't want to manipulate it any further, you can use cell.Text instead.
Use the excel TEXT(val,format) function.
Example:
In cell A2 enter =today() and you'll get a number like 40189
if you enter into cell A2
If you enter =TEXT(today(),"mm/dd/yyyy") you'll get todays date formatted as text and it will look like "01/30/2012"
you could use the Text function.
Syntax
Text(Value,FormatText)
Example
1)I put the 40176 in a excel A2
2)then i refrence it in the formula below
=TEXT(A2,"mm/dd/yy")
on A3
Then the value comes 12/29/09. just like you asked
Caution:
If you put single quote instead of double quote it mightn't work