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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to Set the column of excel as bold while importing from datatable

How to Set the column of excel as bold while importing from datatable

Scheduled Pinned Locked Moved C#
helptutorial
5 Posts 3 Posters 0 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.
  • S Offline
    S Offline
    superselector
    wrote on last edited by
    #1

    I am exporting a datatable to EXCEL . i want to set the font of excel columns as bold and some colour. i was using something like Excel.Application Obj_Excel_App = new Excel.ApplicationClass(); Excel.Workbook Obj_Excel_ExcelWork = Obj_Excel_App.Workbooks.Add(Type.Missing); Excel.Range obj_Range; obj_Range = Obj_Excel_App.get_Range("A1", "E1"); obj_Range.Font.Bold = true; but here i have to hardcode the range , i dont want to do that.plz help

    L C 2 Replies Last reply
    0
    • S superselector

      I am exporting a datatable to EXCEL . i want to set the font of excel columns as bold and some colour. i was using something like Excel.Application Obj_Excel_App = new Excel.ApplicationClass(); Excel.Workbook Obj_Excel_ExcelWork = Obj_Excel_App.Workbooks.Add(Type.Missing); Excel.Range obj_Range; obj_Range = Obj_Excel_App.get_Range("A1", "E1"); obj_Range.Font.Bold = true; but here i have to hardcode the range , i dont want to do that.plz help

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You could create a small helper method or class that converts a row and column number into A1 style notation.

      Veni, vidi, abiit domum

      1 Reply Last reply
      0
      • S superselector

        I am exporting a datatable to EXCEL . i want to set the font of excel columns as bold and some colour. i was using something like Excel.Application Obj_Excel_App = new Excel.ApplicationClass(); Excel.Workbook Obj_Excel_ExcelWork = Obj_Excel_App.Workbooks.Add(Type.Missing); Excel.Range obj_Range; obj_Range = Obj_Excel_App.get_Range("A1", "E1"); obj_Range.Font.Bold = true; but here i have to hardcode the range , i dont want to do that.plz help

        C Offline
        C Offline
        Chris Quinn
        wrote on last edited by
        #3

        You can refer to a specific column, or set of columns, using the Columns collection and set its properties

        ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

        S 1 Reply Last reply
        0
        • C Chris Quinn

          You can refer to a specific column, or set of columns, using the Columns collection and set its properties

          ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

          S Offline
          S Offline
          superselector
          wrote on last edited by
          #4

          Actually i want to get the range of excel and set it colour and font currently i am doing like this Microsoft.Office.Interop.Excel.Range last = excelApp.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing); Microsoft.Office.Interop.Excel.Range range = excelApp.get_Range("A1", last); range.Font.Bold = true; range.Font.Size = 12; range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White); range.Cells.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.SteelBlue); but it actually set the colour of all the rows , i just want to do it for first row which is my column header

          C 1 Reply Last reply
          0
          • S superselector

            Actually i want to get the range of excel and set it colour and font currently i am doing like this Microsoft.Office.Interop.Excel.Range last = excelApp.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing); Microsoft.Office.Interop.Excel.Range range = excelApp.get_Range("A1", last); range.Font.Bold = true; range.Font.Size = 12; range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White); range.Cells.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.SteelBlue); but it actually set the colour of all the rows , i just want to do it for first row which is my column header

            C Offline
            C Offline
            Chris Quinn
            wrote on last edited by
            #5

            Your original post said that you wanted to set the attributes of a column, not the row headers. To find the last used cell in a row, I have used this code (VBA, but you should be able to work out what it is doing.

            Dim WS As Worksheet
            Dim LastCell As Range
            Dim LastCellRowNumber As Long

            Set WS = Worksheets("Sheet1")
            With WS
            Set LastCell = .Cells(.Rows.Count, "C").End(xlUp)
            LastCellRowNumber = LastCell.Row
            End With

            ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

            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