Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to do Format Excel Cells in C#

How to do Format Excel Cells in C#

Scheduled Pinned Locked Moved C#
csharptestingtoolshelptutorial
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Charith Jayasundara
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • C Charith Jayasundara

      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

      S Offline
      S Offline
      sreejith ss nair
      wrote on last edited by
      #2

      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

      C 1 Reply Last reply
      0
      • S sreejith ss nair

        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

        C Offline
        C Offline
        Charith Jayasundara
        wrote on last edited by
        #3

        Hi, I want to give Currency Format for Negative Numbers, do you have any code snippet for that? Thanks a LOT!

        jayasshc

        S 1 Reply Last reply
        0
        • C Charith Jayasundara

          Hi, I want to give Currency Format for Negative Numbers, do you have any code snippet for that? Thanks a LOT!

          jayasshc

          S Offline
          S Offline
          sreejith ss nair
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups