Display cell with vertical text using EPPlus - c#

Using EPPlus, how can i change a cells text to display as a vertical text like this,
In excel, you can do this by clicking on this button when setting cell orientation,
I'm trying to play around with the .TextRotation but this does not achieve what I want, setting it to something like 180 degrees will give me something like this,
ws.Cells[row, 2].Style.TextRotation = 180;, .TextRotation only accepts an integer value so I was wondering how I can get the "Text" buttons value,

Its definitely a bug you found. There is a way but it is pretty ugly. You can use the StyleID created by the cell when you change it to anything other than the default:
[TestMethod]
public void Text_Rotate_Test()
{
//https://stackoverflow.com/questions/57603348/display-cell-with-vertical-text-using-epplus
var fileInfo = new FileInfo(#"c:\temp\Text_Rotate_Test.xlsx");
if (fileInfo.Exists)
fileInfo.Delete();
using (var pck = new ExcelPackage(fileInfo))
{
var workbook = pck.Workbook;
var worksheet = workbook.Worksheets.Add("Sheet1");
var cell = worksheet.Cells[1, 1];
cell.Value = "Test Text Value";
//Trigger epplus to create a new style specific for the cell.
//This needs to be done even thought it will be overridden in
//order to ref by index. But have to be careful not to step
//on other styles so make it as unique as it needs to be.
cell.Style.TextRotation = 180;
//Make sure the update the xml before looking up by index
workbook.Styles.UpdateXml();
workbook.Styles.CellXfs[cell.StyleID].TextRotation = 255;
pck.Save();
}
}
Which gives this:

Related

How to get the row number of the last row when you are printing an excel sheet (EPPlus)

I want to add one picture (displaying "DRAFT") by printable Excel worksheet in C# EPPlus.
I need to know if there is a way to find the last visible row of each page of a worksheet when you are printing it. I can't pretend that it will always be a fix number of row per page because it depends on the content of the cells.
Here is my current code that use a fix number of row per page (30) to insert image. This result in approximately one image per printable page except that in each new page the image is not at the same place. (Slightly off, depending on content of cells.)
public void InsertDraftImage(ExcelWorksheet worksheet, FileInfo draft_image)
{
int maxRowNumber = worksheet.Dimension.End.Row;
int rowByPage = 30;
int numberOfPage = (maxRowNumber / rowByPage) + 1;
ExcelPicture picture = null;
for(int i = 0; i < numberOfPage; i++)
{
if(draft_image != null)
{
picture = worksheet.Drawings.AddPicture(i.ToString(), draft_image);
picture.SetSize(609, 545); //original image size
picture.SetPosition(i * rowByPage, 0, 1, 0);
picture.EditAs = eEditAs.Absolute;
}
}
After trying to implement the missing code in 'ExcelHeaderFooter.cs' from the EPPlus with a workmate without success, we finally did it by following Ernie suggestion!!
There is my final code to insert a picture into each page of a printable excel file generate with EPPlus in C#.
It is done by adding the picture in the footer and setting the Boolean ScaleWithDoc to false (default = true).
public void InsertDraftImage(ExcelWorksheet worksheet, FileInfo draft_image)
{
ExcelHeaderFooterText footer = worksheet.HeaderFooter.OddFooter; //all page have same footer
footer.InsertPicture(draft_image, PictureAlignment.Centered);
}
Added this code in my method to create the ExcelWorksheet (all the other excel style, populate, settings).
XmlAttribute temp = worksheet.WorksheetXml.CreateAttribute("scaleWithDoc");
temp.Value = "0";
worksheet.WorksheetXml.GetElementsByTagName("headerFooter")[0].Attributes.Append(temp);
package.Save();

Add cell having a table to outer table using itextsharp

I'm trying to add a cell having a table to outer table.
I'm getting following error.
"Adding a cell at the location (6,0) with a colspan of 1 and a rowspan
of 1 is illegal (beyond boundaries/overlapping)"
This seems to be like a bug in itextsharp.
I find a each control in html table. If this control is Table i call
it 'tblinner'.
I'm finding a control(in this case all controls are labels) inside
'tblinner' and adding it to itextsharp table.
How may i overcome this?
for (int i = 0; i < tblInner.Rows.Count; i++)
{
iTextSharp.text.Table tblnested = new iTextSharp.text.Table(4);
iTextSharp.text.Table tblnestedbig = new iTextSharp.text.Table(1);
iTextSharp.text.Cell pdfTCell = new iTextSharp.text.Cell();
iTextSharp.text.Cell pdfTCellbig = new iTextSharp.text.Cell();
foreach (TableCell tCellInner in tblInner.Rows[i].Cells)
{
pdfTCell = new iTextSharp.text.Cell();
int cc = tblInner.Rows[i].Cells.Count;
Control tControlInnerLabel = tCellInner.Controls[0];
pdfTCell = AddControlBGToPDFTable(tControlInnerLabel, tblnested);//This function adds the control such as lable or image to tblnested
tblnestedbig.AddCell(pdfTCell);
}
pdfTCellbig.AddElement(tblnestedbig);
pdfTable.AddCell(pdfTCellbig);//i get error here
}
Here is what 'AddControlBGToPDFTable' does.
private static iTextSharp.text.pdf.PdfPCell AddControlBGToPDFTable(Control tControl, iTextSharp.text.pdf.PdfPTable pdfTable)
{
(tControl is Label)
{
Label lbl = (Label)tControl.FindControl(tControl.ID);
// add some style to control
iTextSharp.text.pdf.PdfPCell pdfTable2Cell2 = null;
pdfTable2Cell2.Colspan = 1;
pdfTable2Cell2 = pdfOrigTCell;
pdfTable2Cell2.AddElement(new iTextSharp.text.Phrase(25f,lbl.Text.Replace("<br>", "\r\n").Replace("<br />", "\r\n"), new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 12, style)));
pdfTable.AddCell(pdfTable2Cell2);
return pdfTCell;
}
}
It takes a control passed to it.
Create a cell and put this control to cell and add the cell to table
that is passed as param to it.
you should post also code for method AddControlBGToPDFTable, that we can see what properties are you setting to cell.
Instead of creating cell with label you can use PdfPCell and PdfPTable.
iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell();
cell.Phrase = new iTextSharp.text.Phrase("some text");

How to increase the font size of a particular cell in excell using c# [duplicate]

I am working on a project that writes data to an Excel file.
Everything is finished now, however I need a few cells with a bigger size than the rest (title, etc).
I have read about this about the internet, but I keep having the same problem: when I execute my code (see below for what I have tried), everything in the worksheet becomes larger.
What I already have tried:
worksheet.Rows[1].Cells[7].Style.Font.Size = 20;
worksheet.get_Range("A7", "A7").Style.Font.Size = 20;
None of this seems to work; what is the correct way to increase a cell's font size?
I had to use:
worksheet.get_Range("A7", "A7").Cells.Font.Size = 20;
If the data is consistent and will always be written to the same cells then this is the simplest solution - works well for product details / contact info type exporting
// set cell A7
worksheet.get_Range("A7", "A7").Font.Size = 20;
// set cells A7, A8
worksheet.get_Range("A7", "A8").Font.Size = 20;
// set cells A7, B7
worksheet.get_Range("A7", "B7").Font.Size = 20;
// set cells A7, A8, B7, B8
worksheet.get_Range("A7", "B8").Font.Size = 20;
If the data varies and will sometimes be written to multiple rows/columns then something like this is more simple - works well for dataset / shopping list type exporting
int RowNum;
int ColNum;
// some code to set variables
worksheet.Cells[RowNum, ColNum].Font.Size = 20;
I would just use:
worksheet.Range["A7"].Style.Font.Size = 20;
edit: sorry, wrong brackets
When working with interop excel, try not to write your code with "two dots" in order to clean interop excel objects.
This also helps having your code more readable.
Anyway, to answer your question, and using what I have pointed out... all you have to do is:
//Declare your variables
Application excel = null;
Workbook excelworkBook = null;
Range excelCellrange = null;
Worksheet worksheet = null;
Font excelFont =null;
//start your application
excel = new Application();
try
{
...
//your code goes here...
excelCellrange = worksheet.Range[worksheet.Cells[1,7],worksheet.Cells[1,7]];
excelFont = excelCellrange.Font;
excelfont.Size = 20;
...
...
}
catch(Exception ex){
}
finally{
//here put something to clean the interop objects as the link above.
...
Marshal.ReleaseComObject(excelfont);
...
}

How to use no fill for background color of a excel cell using c#?

With this:
using Excel = Microsoft.Office.Interop.Excel;
I'm opening the excel and after I'm setting the color of the first cell to transparent like this:
xlRange = xlWorkSheet.get_Range("A1");
xlRange.Interior.Color = System.Drawing.Color.Transparent;
The problem is that it puts white and the "borders" disappear. I want to put the "No Fill" option and it's not working.
I've also tried this:
xlRange.Interior.Color = System.Drawing.Color.Empty;
but then it changed the cell color to black.
How can I solve this?
Assuming that you want to achieve the same state as a cell's initial state (in a new worksheet), use this:
xlRange.Interior.ColorIndex = 0;
Try this:
xlRange.Interior.Pattern = Excel.Constants.xlNone;
xlRange.Interior.TintAndShade = 0;
xlRange.Interior.PatternTintAndShade = 0;
Maybe this is just partly related to this question; but I noticed by using
xlRange.Interior.Pattern = Excel.XlPattern.xlPatternNone;;
Also all cell fill color disappears. This is not the case when using:
xlRange.Interior.Pattern = Excel.XlPattern.xlPatternAutomatic;
I was searching for this and came upon this question, so I add this for extra info.

how to change background color of table cell of ppt using c# code?

I have written below piece of code to change background color of table cell of ppt using c# code but nothing is happening:
//creating powerpoint aaplication
PowerPoint.Application pptApp = new PowerPoint.Application();
pptApp.Visible = Office.MsoTriState.msoTrue;
var pptPresent = pptApp.Presentations;
var fileOpen = pptPresent.Open(#file, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, Office.MsoTriState.msoTrue);
// getting first slide
PowerPoint.Slide objSlide = fileOpen.Slides[1];
PowerPoint.Shapes item = objSlide.Shapes;
// getting first shape
var shape1 = item[1];
// check if shape is table
if (shape1.HasTable == Office.MsoTriState.msoTrue)
{
// change the table cell to red color
shape1.Table.Cell(2, 5).Shape.Fill.BackColor.RGB = System.Drawing.Color.Red.ToArgb();
// make it visible
shape1.Fill.Visible = Office.MsoTriState.msoTrue;
}
// saving the ppt
fileOpen.SaveAs(openFolder + subStr + ".pptx",PowerPoint.PpSaveAsFileType.ppSaveAsDefault,Office.MsoTriState.msoTrue);
// close the ppt
fileOpen.Close();
Above piece of code is not working as expected, can someone help me?
To change the cell color ForeColor needs to be used instead of BackColor. Drawing colors may not work as expected. To change that use ColorTranslator.
shape1.Table.Cell(2, 5).Shape.Fill.ForeColor.RGB = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
Great question! The Fill property is also available for the Shape object, wether it's a Cell or a another type of Shape.
Another example for the Fill property is mentioned in this thread:
https://stackoverflow.com/a/26253177/20337158.
Also from the Microsoft learning sites:
https://learn.microsoft.com/en-us/previous-versions/office/office-12/ff763049(v=office.12)

Categories