How can a border be set around multiple cells in excel using C#

后端 未结 8 1498
无人共我
无人共我 2020-12-18 22:18

I am working on a project that creates excel files.

I am having trouble placing a border on multiple cells to organize the excel file.

Let\'s say I want a b

相关标签:
8条回答
  • 2020-12-18 22:32
    // ** - You Should do it in all Cells 
    
    //BorderAround: Medium**
    
    worksheet.get_Range("b5", "b5").Cells.BorderAround(Missing.Value, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, ColorTranslator.ToOle(Color.FromArgb(255, 192, 0)));
    
    worksheet.get_Range("b6", "b6").Cells.BorderAround(Missing.Value, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, ColorTranslator.ToOle(Color.FromArgb(255, 192, 0)));
    
    worksheet.get_Range("b10", "b10").Cells.BorderAround(Missing.Value, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, ColorTranslator.ToOle(Color.FromArgb(255, 192, 0)));        
    
    0 讨论(0)
  • 2020-12-18 22:34

    Maybe this can help :

    workSheet_range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick);
    
    0 讨论(0)
  • 2020-12-18 22:41

    Here is my solution, use simply UsedRange() function

    Excel.Range tRange = oSheet.UsedRange;
                tRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                tRange.Borders.Weight = Excel.XlBorderWeight.xlThin;        
    
    0 讨论(0)
  • 2020-12-18 22:45

    I did this without impacting the performance. I am taking a simple excel to format :

    Before

    I managed to store the range as A1:C4 in a variable dynamically in exRange and used the below code to give border

    ((Range)excelSheet.get_Range(exRange)).Cells.Borders.LineStyle = XlLineStyle.xlContinuous;
    


    After

    0 讨论(0)
  • 2020-12-18 22:50
    ws.UsedRange.BorderAround(
                            Xl.XlLineStyle.xlDash,
                            Xl.XlBorderWeight.xlThick,
                            Xl.XlColorIndex.xlColorIndexAutomatic,
                            ColorTranslator.ToOle(Color.Blue));
    
    0 讨论(0)
  • 2020-12-18 22:52

    You need to individually set these

    .Borders[Excel.XlBordersIndex.xlEdgeBottom] 
    .Borders[Excel.XlBordersIndex.xlEdgeRight]
    .Borders[Excel.XlBordersIndex.xlEdgeLeft]  
    .Borders[Excel.XlBordersIndex.xlEdgeTop]
    
    0 讨论(0)
提交回复
热议问题