Hi i am using EPPlus to populate a spreadsheet with data, so far so good text wise but i am having issues when trying to add a picture??
I can add a picture but its from the top left cell and its not contained with in a cell.
Please could someone show me how i can place a picture into a cell and fill the cell?
Bitmap image = new Bitmap(tempFolderPathAlt + "ExtractedFiles/" + ArrayNode[i].TagValue);
ExcelPicture picture = null;
var rowIndex = xlWorkSeet1[k].Dimension.Start.Row;
var columnIndex = xlWorkSeet1[k].Dimension.Start.Column;
picture = xlWorkSeet1[k].Drawings.AddPicture(System.Guid.NewGuid().ToString() + rowIndex.ToString() + columnIndex.ToString(), image);
picture.From.Column = columnIndex;
picture.From.Row = rowIndex;
I believe there is no way to add a picture into a cell.
The work around for this is to resize the cell to be the same size as your image and place it over the cell.
Related
I need to mark error cells in my output excel file in red. If in the original document those cells have no fill, all is fine, the color is red. But if the cell was already filled with Blue (or some kind of), in the output it become filled with pink, but not with red!
Here is my code:
var cell = worksheet.Cells[row, column];
cell.Reset();
cell.Value = string.Format("{0} Error:{1}", cell.Value, errortext);
cell.Style.Fill.BackgroundColor.SetColor(Color.Empty);
cell.Style.Fill.PatternType = ExcelFillStyle.Solid;
cell.Style.Fill.BackgroundColor.SetColor(Color.Red);
cell.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 0, 0));
This is how it should look:
This is how it is if the original fill was blue:
Will be very appreciated for any help.
cell.Style.Fill.BackgroundColor.Tint = 0;
that helped.
I have a PDF doc that I am trying to create, with about 20 columns, varying width. It gets about half of the columns on the first page and then cuts off the rest.I would like it to determine the page width and move the remaining columns onto the second page. Is there a way to specify this in rendering or PageSetup? I think I'll have to calculate the width, create the first page and then create the second.
Table table = new Table();
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
renderer.Document = doc;
doc.DefaultPageSetup.Orientation = MigraDoc.DocumentObjectModel.Orientation.Landscape;
//create the columns
for (int i = 1; i < tripReportGrid.Columns.Count; i++)
{
col = table.AddColumn(tripReportGrid.Columns[i].Width);
col.Format.Alignment = ParagraphAlignment.Center;
}
...fill the content same way
renderer.RenderDocument();
The width of the page is what you set - or A4 if you set nothing.
You can set the width of the page to any value. That will probably be OK when viewing the PDF file on the screen.
Or you can only add as many columns to one table as fit on one page. A4 in landscape format is 29.7 cm. Default margins are 2.5 cm left and right.
And BTW: you should never modify the DefaultPageSetup. Assign a Clone() of the DefaultPageSetup to the PageSetup of your Section and change that as needed.
I am trying to set the color of a given cell with the color of another cell (that is already colored in the template.
But worksheet.Cells[row, col].Style.Fill.BackgroundColor doesn't seem to have a getproperty.
Is it possible to do that or do I have to find the exact hexdecimal code of the color on the internet ?
EDIT
using the code in the answer, I get that error (it is written in French but it translate with what I wrote in my first comment)
How about something like this?
//get color from this cell
var rgb = ws.Cells[1, 2].Style.Fill.BackgroundColor.Rgb;
//Convert to system.drawing.color
var color = System.Drawing.ColorTranslator.FromHtml("#" + rgb);
//set color in this cell
ws.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(color);
Update:
Seems that if you select a color from system colors or the palette as your fill color it works fine. If you select one of the 'Theme colors' in the fill drop down .Rgb returns an empty string
//get style
var style = ws.Cells[400, 1].Style;
//If color from System colors or palette
if (!string.IsNullOrEmpty(style.Fill.BackgroundColor.Rgb))
{
//Convert to system.drawing.colow
var color = System.Drawing.ColorTranslator.FromHtml("#" + style.Fill.BackgroundColor.Rgb);
//set color in this cell
ws.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(color);
}
else if (!string.IsNullOrEmpty(style.Fill.BackgroundColor.Theme))
{
//No idea how to get color from Theme
}
I'm not sure it's supported... according to EPPlus Faqs :
What is NOT supported by the library (these are the most obvious features)?
[...]
* Themes
For anyone getting here, since I had my round of fun with EPPlus and I wasn't satisfied with the answers above:
cell.Style.Fill.BackgroundColor.LookupColor()
returns #AARRGGBB color (I.E. red is #FFFF0000). The part you're interesdted in is probably the last 6 digits.
I have a wide merged cell showing the title of the sheet. I also have a colored Textbox that I would like to position immediately to the right of the text in the merged cell.
The width of the title is variable so I have used a very wide merged cell.
sheet.Range["A2"].Value = title;
//Red Square
sheet.Shapes.Item("redSquare").Left += 0; // Position sheet.Range["A2"].TextWidth ???
Is there a property I can access on Range that will give me the actual text width in pixels of the text in merged cell?
As far as I know, there isn't anything in Interop that will tell you the width of text in pixels, but you can do it with the following. It seems to return a value reasonably similar to the column widths that Excel sets based on the same fonts.
Excel.Range xlRange = sheet.get_Range("A2");
Excel.Font xlFont = xlRange.Font;
string fontName = xlFont.Name;
double fontSize = xlFont.Size;
Font font = new Font(fontName, (float)fontSize);
float width = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(title, font).Width;
You might need to add a few pixels here and there to make sure your textbox clears the end of the text, but I think it's about as accurate as you're going to get.
I'm having a bit of trouble on solving this. Is there a way to set the width of a specific cell from a worksheet using epplus? Because when i use, for example:
ws.Column(1).Width = 40;
the whole column gets affected.
Thanks in advance!
As I know, there is no way to change the width of a specific cell. All cells in one column have same width. You can change the width of a cell by merging it with its neighbour. I have an example:
sheet.Cells["A1"].Value = "This is a cell";
sheet.Cells["A1:A2"].Merge = true;
Then you can set width by using
sheet.Column(1).Width = 40;
or
sheet.Cells["A1:A2"].AutoFitColumns();
Hi you can try like this
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("1232");
ws.Cells["A1"].AutoFitColumns();
//ws.Cells[""].AutoFitColumns(20);//overloaded
//ws.Cells[""].AutoFitColumns(20,40);//overloaded min and max lengths
}