I see from Microsoft's documentation that I can access the particular border edges of a cell using the 'xlBordersIndex' property and for example set the border style for the left edge of a cell:
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
But what if I just want to draw all borders? I have tried
range.BorderAround2();
but that just draws a box around the range itself, which I understand. So then I tried
range.Cells.BorderAround2();
thinking that it would go through each of the cells within the range and place all borders around each cell. This is not what occurred. So in order to get all borders around all cells in a range, must I manually access each of the four border indices?
private void AllBorders(Excel.Borders _borders)
{
_borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders.Color = Color.Black;
}
oRange = SHEET2.get_Range("a1", "a10");
oRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlContinuous;
oRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlContinuous;
oRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Excel.XlLineStyle.xlContinuous;
oRange.Borders.get_Item(Excel.XlBordersIndex.xlInsideVertical).LineStyle = Excel.XlLineStyle.xlContinuous;
Finally, I got it. I did this without impacting the performance too. I am taking a simple excel to explain here :
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
I'm not yet familiar wit C#, but in VBA there are Range.Borders(xlInsideVertical) and Range.Borders(xlInsideHorizontal) properties. Try to use macro-recorder and apply all borders for any workbook region. Perhaps that will help.
For Each range In ranges
For Each row As Range In .Range(range).Rows
row.Cells.BorderAround(XlLineStyle.xlContinuous)
row.Cells.Borders.Item(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous
row.Cells.Borders.Item(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous
Next
Next
Why not to do simply:
Excel.Range tRange = xlWorkSheet.UsedRange;
tRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
tRange.Borders.Weight = Excel.XlBorderWeight.xlThin;
Note: apply border after the row and cell (range) filled with data to get range simply using function .UsedRange()
Microsoft.Office.Interop.Excel.Range tRange = xlWorkSheet.UsedRange;
tRange.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
tRange.Borders.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
Excel.Range tRange = xlWorkSheet.UsedRange;
tRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
tRange.Borders.Weight = Excel.XlBorderWeight.xlThin;
That way is not working, you will get something like this:
Related
I tried setting all related properties ExcelLineChartSerie has to offer, but still cannot set or change the color of the Excel marker from default ugly blue.
ExcelChart ec = ws.Drawings.AddChart("LineChart01", eChartType.LineMarkers);
var rangeX = ws.Cells["A2:A11"]; // X-Axis
var range1 = ws.Cells["B2:B11"]; // 1st LineSerie
ExcelLineChartSerie serie1 = (ExcelLineChartSerie)ec.Series.Add(range1, rangeX);
serie1.MarkerLineColor = System.Drawing.Color.Gray;
serie1.MarkerSize = 10;
serie1.Fill.Color = System.Drawing.Color.Gray;
serie1.LineColor = System.Drawing.Color.Gray;
serie1.Border.LineStyle = eLineStyle.Solid;
It is available in EPPlus (current source!). Just convert your base series to specific one.
var chartType3 = (ExcelLineChart)chart.PlotArea.ChartTypes.Add(eChartType.Line);
var serie5 = (ExcelLineChartSerie)chartType3.Series.Add(worksheet.Cells["F1:F12"], worksheet.Cells["A1:A12"]);
serie5.Marker = eMarkerStyle.Circle;
serie5.MarkerLineColor = Color.FromArgb(165, 165, 165);
serie5.MarkerSize = 5;
From looking at the current source code, it looks like this functionality is not implemented in EPPlus yet.
This discussion points to a SO post which shows how to implement an extension method to add functionality to change line thickness and color. It should be possible to adapt this code to change the marker fill color. The property paths you will need for this are at the end of the discussion on codeplex (second link above).
Have researched outputting to Excel and can successfully do so. My question is more if I am missing something simpler.
Currently, if I want to set the font, cell color, size, etc. of a single cell, I am doing so like this:
range = (Range)ws.Cells[10, 12];
range.Formula = "=SUM(R10C10:R10C11)";
range.Calculate();
range.Font.Bold = true;
range.Font.Underline = true;
range.Style = wb.Styles["Currency"];
range.Font.Color = Color.Red;
range.Font.Name = "Arial";
range.Font.Size = 26;
borders = range.Borders;
borders.LineStyle = XlLineStyle.xlContinuous;
borders.Weight = 2d;
Did I miss something in the documentation that allows me to do this on a SINGLE cell without having to create a Range?
No, C# requires an object qualifier (see What's the C# equivalent to the With statement in VB?. So your current code is to protocol.
From a long time i am trying to generate graph like this
Codes i tried.
Excel.Range chartRange1;
Excel.ChartObjects xlCharts1 = (Excel.ChartObjects)worksheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart1 = (Excel.ChartObject)xlCharts1.Add(350, 500, 500, 350);
Excel.Chart chartPage1 = myChart1.Chart;
chartRange1 = worksheet.get_Range("A33", "b56");
chartPage1.SetSourceData(chartRange1, Type.Missing);
chartPage1.ChartType = Excel.XlChartType.xlBarStacked;
Excel.Range xValues = worksheet.Range["B33", "B56"];
Excel.Range values = worksheet.Range["a33", "a56"];
Excel.SeriesCollection seriesCollection = (Excel.SeriesCollection)chartPage1.SeriesCollection();
Excel.Series series1 = seriesCollection.NewSeries();
series1.XValues = xValues;
series1.Values = values;
Please help which chart type i should use or i am making any mistake. A
After changing chart type, it works perfectly fine but its not working for last row text. As shown in image below.
Yes, you need to change the chart type.
using Excel = Microsoft.Office.Interop.Excel;
chartPage1.ChartType = Excel.XlChartType.xlBarClustered
You may have to adjust gridlines depending on how you want them to appear. I can provide more code for that if needed.
Edit - Also, don't forget to do
chartPage1.PlotBy = Excel.XlRowCol.xlColumns;
I don't know how but the moment i commented chartPage1.SetSourceData(chartRange1, Type.Missing); in code it works fine, might be possible two data source are set one by commented line and one by series collection.
Could somebody tell me how to add borders around the outside of a range of cells in another colour? Ideally I would like to be able to do this with a single method I will have to do this multiple times. After searching for this I found two methods that would apparently do this - BorderAround and BorderAround2. I suppose my first question is what is the difference between these two methods? I tried using each of these and only BorderAround2 was recognised?
Anyway, `BorderAround2' almost does what I wanted. I used the following line of code which did put a border around the outside of the range, but it was black, rather than red:
ws.get_Range("B2", "E3").BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick, Excel.XlColorIndex.xlColorIndexNone, Color.FromArgb(255, 0, 0), Type.Missing);
The MSDN documentation for this method states:
You must specify either ColorIndex or Color, but not both.
How do I go about doing this? If I set the ColourIndex parameter to Type.Missing or to null or miss it out completely, it produces an error. Any help would be appreciated.
Finally I should point out that I found a workaround solution here where you set the set the various edges separately, but as I say, I was hoping to do this using a single method as it has to be repeated multiple times.
To add a border to one or more sides of an Excel Range (range of cells, which can normally be comprised of 1..many rows and 1..many columns, but for this specific scenario, we probably want to stick with one row and 1..many columns), you only need do three things:
0) Define the range
1) Get a reference to the Range's Borders array
2) Assign a border to one or more of the Border array's edges/sides (top, bottom, left, right)
First, define the range over which you want to operate on like so:
var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];
Next, obtain a reference to the Range's Borders array like this:
Borders border = rowToBottomBorderizeRange.Borders;
Finally, assign a border to one or more of the Border array's edges; for example, if you want to add a boder to the bottom, like so:
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
Putting it all together, the code could be:
var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];
Borders border = rowToBottomBorderizeRange.Borders;
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
If you do this in several places, you could make a method out of it:
private void AddBottomBorder(int rowToBottomBorderize)
{
var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBottomBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBottomBorderize, TOTALS_COL]];
Borders border = rowToBottomBorderizeRange.Borders;
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
}
The example above shows just adding a bottom border, but you can add top, left, or right border lines just as easily, replacing "xlEdgeBottom" with "xlEdgeTop", "xlEdgeRight", or "xlEdgeLeft"
Or, you could add borders all around a range like this:
private void Add360Borders(int rowToBorderize)
{
var rowToBottomBorderizeRange = _xlSheet.Range[_xlSheet.Cells[rowToBorderize, ITEMDESC_COL], _xlSheet.Cells[rowToBorderize, TOTALS_COL]];
Borders border = rowToBorderizeRange.Borders;
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
}
Note: You will need to define the sheet like this:
private Worksheet _xlSheet;
...and reference the Microsoft.Office.Interop.Excel assembly in your solution.
Try:
ws.get_Range("B2", "E3").Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
ws.get_Range("B2", "E3").Borders.Color = ColorTranslator.ToOle(Color.Red);
I am looking to apply a border to one cell using the Microsoft.Office.Interop.Excel library.
I am running a while-loop that is search for a empty cell within a certain column, once the cell is found I want to apply a border to it.
I know there many forums on this using Ranges, but I can't use the range functionality since I do not know what cell it is being applied to exactly.
My idea was:
(Excel.Range)xlWS.Cells[row,1].Borders.LineStyle = (Whatever Value);
Any advice? (besides links to other forums, I already looked through tons of forms)?
Excel.Range range = xlWS.UsedRange;
Excel.Range cell = range.Cells[row, column];
Excel.Borders border = cell.Borders;
border.LineStyle = Excel.XlLineStyle.xlContinuous;
border.Weight = 2d;
Hope this helps someone! Puts a thin line border around cell[row,column]!
Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange;
Microsoft.Office.Interop.Excel.Range cell = range.Cells[1][1];
Microsoft.Office.Interop.Excel.Borders border = cell.Borders;
border[XlBordersIndex.xlEdgeLeft].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeTop].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeBottom].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border[XlBordersIndex.xlEdgeRight].LineStyle =
Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
See this answer for more details.
To each cell in range
Microsoft.Office.Interop.Excel.Range chartRange;
chartRange = workSheet.get_Range("a1", "g7");
foreach (Microsoft.Office.Interop.Excel.Range cell in chartRange.Cells)
{
cell.BorderAround2();
}
To whole range
chartRange.BorderAround2();
This is based on jiverson's answer, but is much more concise for basic needs; simply create a range, get its Borders, and add a border to the bottom of the range:
var platypusRange = _xlSheet.Range[_xlSheet.Cells[1, 1], _xlSheet.Cells[1, 3]];
. . .
Borders border = platypusRange.Borders;
border[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;
Of course, you could add a border to the top and sides, too, using xlEdgeTop, xlEdgeLeft, and xlEdgeRight.