How to do Format Excel Cells in C#
-
Hi, I am creating Excel sheets using Excel Automation library. I want to change 'Negative Number' format for 'Currency' category. Please help, if you know how to do it! Thanks!
jayasshc
-
Hi, I am creating Excel sheets using Excel Automation library. I want to change 'Negative Number' format for 'Currency' category. Please help, if you know how to do it! Thanks!
jayasshc
Hi, You might required to reach the required cells/range of cells to format the cells.
Excel.Application excelApp = new Excel.ApplicationClass(); Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(File_Path, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); Excel.Sheets excelSheets = excelWorkbook.Worksheets; Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(fileName); Excel.Range excelCells; excelCells = (Excel.Range)excelWorksheet.get_Range("A1", "G1"); excelCells.Select();
The above example code will select a range of cells from A1 (column name) to G1 (column name). Now you have range of required cells to apply any specific format. Here let me explian some formating tips interms of Font style and name.excelCells.WrapText=true; excelCells.HorizontalAlignment=Excel.Constants.xlCenter; excelCells.Borders.LineStyle=Excel.Constants.xlSolid; excelCells.Font.Bold = true; excelCells.Font.Size = 9; excelCells.Font.Name = "Verdana";
If the post/article served your purpose then, please assist me in keeping it up by donating a small amount of money to my Paypal account. Email: sreejithssnair@hotmail.com
-
Hi, You might required to reach the required cells/range of cells to format the cells.
Excel.Application excelApp = new Excel.ApplicationClass(); Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(File_Path, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); Excel.Sheets excelSheets = excelWorkbook.Worksheets; Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(fileName); Excel.Range excelCells; excelCells = (Excel.Range)excelWorksheet.get_Range("A1", "G1"); excelCells.Select();
The above example code will select a range of cells from A1 (column name) to G1 (column name). Now you have range of required cells to apply any specific format. Here let me explian some formating tips interms of Font style and name.excelCells.WrapText=true; excelCells.HorizontalAlignment=Excel.Constants.xlCenter; excelCells.Borders.LineStyle=Excel.Constants.xlSolid; excelCells.Font.Bold = true; excelCells.Font.Size = 9; excelCells.Font.Name = "Verdana";
If the post/article served your purpose then, please assist me in keeping it up by donating a small amount of money to my Paypal account. Email: sreejithssnair@hotmail.com
Hi, I want to give Currency Format for Negative Numbers, do you have any code snippet for that? Thanks a LOT!
jayasshc
-
Hi, I want to give Currency Format for Negative Numbers, do you have any code snippet for that? Thanks a LOT!
jayasshc
Hi, In line with the last post, you can use this line of code to retrieve the value from a specified cell and use string format.
excelCells = (Excel.Range)excelWorksheet.get_Range("A1",Type.Missing); excelCells.Value2 = "File Name"; //set your logic here for formatting. String Format would do.
If the post/article served your purpose then, please assist me in keeping it up by donating a small amount of money to my Paypal account. Email: sreejithssnair@hotmail.com