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.
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).
I opened a new question, because none of the answers I found here or elsewhere work for me.
I create a table using Word from C# (Office 16.0 Interop).
My goal: To make the width of a certain column smaller, i.e. fit to its content.
This is how I create a table:
var table = doc.Tables.Add(menuParagraph.Range, rowCount, 2, ref _objMiss, ref _objMiss);
table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
// Label
table.Cell(1, 1).Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;
table.Cell(1, 1).Range.Text = "Label";
table.Cell(1, 2).Range.Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;
table.Cell(1, 2).Range.Text = $"{recordItem.TextId.EnglishTranslation} ({recordItem.TextId.GermanTranslation})";
// Type and length
table.Cell(2, 1).Range.Text = "DataType";
table.Cell(2, 2).Range.Text = $"{recordItem.DataType} (Bit length: {recordItem.BitLength})";
// Byte offset
table.Cell(3, 1).Range.Text = "Byte offset";
table.Cell(3, 2).Range.Text = $"{recordItem.ByteOffset}";
// Default value
table.Cell(4, 1).Range.Text = "Default value";
table.Cell(4, 2).Range.Text = $"{recordItem.DefaultValue}";
None of the solutions I found so far, solved my problem.
In fact, none of it has any effect on my table.
table.Columns[0].AutoFit(); gets ignored, has no effect whatsoever.
Same goes for table.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
Even if I set the width directly, it gets ignored:
table.Columns[0].Width = app.PixelsToPoints(100f);
The output is always the same. A table where each column has the same width.
How do I force a table to make one column adjust its width to its contents (and still use the whole width of a page in total).
For auto-fitting the first column, something like this should work. It uses the Column.SetWidth Method.
table.AllowAutoFit = true;
Word.Column firstCol = table.Columns[1];
firstCol.AutoFit(); // force fit sizing
Single firstColAutoWidth = firstCol.Width; // store autofit width
table.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitWindow); // fill page width
firstCol.SetWidth(firstColAutoWidth, Word.WdRulerStyle.wdAdjustFirstColumn); // reset width keeping right table margin
Have you tried with : table.Columns[0].PreferredWidth ?
Maybe that would work.
Here is some documentation about it :
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.table.preferredwidth.aspx
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:
Hi I am currently trying to get the bars to be aligned over the the numbers in the x axis. As you can see in the image below, my bars in the chart seem to align to the left or right.
[URL=http://imageshack.us/photo/my-images/528/stackoverflowexample.png/][IMG=http://img528.imageshack.us/img528/19/stackoverflowexample.png][/IMG][/URL]
Uploaded with [URL=http://imageshack.us]ImageShack.us[/URL]
I was wondering if there was a way using lines of code to make them align directly over the numbers 1, 2 and 3?
I also wondering if it is possible to change 1, 2 and 3 into the name temprature, vcc and light??
I have done some looking into it and am unable to find how to correct this problem (as you can see from the commented out lines of code were i have tried) and this is why i have turned to you guys.I have posted my code below ( slightly modified so it will work in any c# without my variables).
thanks for takin the time to read this
chart1.ChartAreas.Add("area");
//chart1.ChartAreas["area"].AxisX.Minimum = 0;
//chart1.ChartAreas["area"].AxisX.Maximum = 2;
//chart1.ChartAreas["area"].AxisX.Interval = 1;
//chart1.ChartAreas["area"].AxisX.IntervalAutoMode;
chart1.ChartAreas["area"].AxisY.Minimum = 0;
chart1.ChartAreas["area"].AxisY.Maximum = sumLight + 100;
chart1.ChartAreas["area"].AxisY.Interval = 50;
chart1.ChartAreas["area"].AxisY.Title = "Average Value";
//chart1.ChartAreas["area"].AxisX.Title = "Speed (m/s)";
chart1.Series.Add("Temprature");
chart1.Series.Add("VCC");
chart1.Series.Add("Light");
chart1.Series["Temprature"].Color = Color.Red;
chart1.Series["VCC"].Color = Color.Green;
chart1.Series["Light"].Color = Color.Yellow;
chart1.Series["Temprature"].Points.AddXY(1, 78.32);
chart1.Series["VCC"].Points.AddXY(2, 3.92);
chart1.Series["Light"].Points.AddXY(3, 333);
chart1.Series["Temprature"].IsValueShownAsLabel = true;
chart1.Series["VCC"].IsValueShownAsLabel = true;
chart1.Series["Light"].IsValueShownAsLabel = true;
chart1.Legends.Add("legend");
chart1.Titles.Add("Average for Temperature, Light & VCC using timestamp");
chart1.Visible = true;
I guess you might come to something by inserting empty datapoints.
So this block :
chart1.Series["Temprature"].Points.AddXY(1, 78.32);
chart1.Series["VCC"].Points.AddXY(2, 3.92);
chart1.Series["Light"].Points.AddXY(3, 333);
Should become
chart1.Series["Temprature"].Points.AddXY(1, 78.32);
chart1.Series["Temprature"].Points.Add(new DataPoint(2,0){IsEmpty=true});
chart1.Series["Temprature"].Points.Add(new DataPoint(3,0){IsEmpty=true});
chart1.Series["VCC"].Points.Add(new DataPoint(1,0){IsEmpty=true});
chart1.Series["VCC"].Points.AddXY(2, 3.92);
chart1.Series["VCC"].Points.Add(new DataPoint(3,0){IsEmpty=true});
chart1.Series["Light"].Points.Add(new DataPoint(1,0){IsEmpty=true});
chart1.Series["Light"].Points.Add(new DataPoint(2,0){IsEmpty=true});
chart1.Series["Light"].Points.AddXY(3, 333);
Also, you might try having only one serie with 3 colored DataPoints :
chart1.Series[0].Points.AddXY("Temprature", 78.32);
chart1.Series[0].Points[chart1.Series[0].Points.Count - 1].Color = Color.Red;
chart1.Series[0].Points.AddXY("VCC", 3.92);
chart1.Series[0].Points[chart1.Series[0].Points.Count - 1].Color = Color.Green;
chart1.Series[0].Points.AddXY("Light", 333);
chart1.Series[0].Points[chart1.Series[0].Points.Count - 1].Color = Color.Yellow;
I have to write text with coefficient value like C1, C2, C3 on label text, so please tell me how can i write ???
thanks
Shashi Jaiswal
You need a font that comes with glyphs for Unicode codepoints U+2080 to U+2089:
label1.Font = new Font("DejaVu Sans", 10);
label1.Text = "C₁"; // or "C\u2081"
(assuming WinForms)
In WinForms you need to emulate that with a RichTextBox
// Appearance as a label
var subscriptFont = new System.Drawing.Font(
richTextBox1.Font.FontFamily,
richTextBox1.Font.Size - 2);
richTextBox1.BackColor = System.Drawing.SystemColors.Control;
richTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
richTextBox1.ReadOnly = true;
richTextBox1.Text = "C1, C2, C3";
// subscript 1
richTextBox1.Select(1, 1);
richTextBox1.SelectionCharOffset = -3;
richTextBox1.SelectionFont = subscriptFont;
// subscript 2
richTextBox1.Select(5, 1);
richTextBox1.SelectionCharOffset = -3;
richTextBox1.SelectionFont =subscriptFont;
// subscript 3
richTextBox1.Select(9, 1);
richTextBox1.SelectionCharOffset = -3;
richTextBox1.SelectionFont = subscriptFont;
subscriptFont.Dispose();
You could try using a different font that haves subindexes...
You can't. Plain and simple.
(But you could use two labels, positioned and sized accordingly, or use a label that supports complex markup... Or use UTF-8, which allows them...)
But a stock C# Winforms project? Nah.