setting cell width with epplus affects the whole column - c#

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
}

Related

How set height row in excel with NPOI?

How set height row in c# with NPOI?
To specify the width of the columns I'm using XSSFSheet.SetColumnWidth, but what does the command for the height of the cells look like?
try below approach
var row = sheet.CreateRow(0);
row.Height = 10 ;
//Or
sheet.GetRow(1).Height = 10;
The height of the row is the same:
XSSFSheet.GetRow(index).Heigh {get;set;}
In addition to #kumar answer, you can set it like this
row.HeightInPoints = 16.5F;

How can I control table column width in Word documents using DocX?

I am trying to recreate a table like this:
I am using the DocX library to manipulate Word files, but I'm having trouble getting the widths right. Trying to set the widths of cells only seems to work when it's not set to the window autofit mode, and it only seems to resize when the specified width is greater than half of the table width, or rather, I can make a cell bigger than half the width but not smaller.
What would be the simplest way to reproduce the intended table?
I found the answer to this myself. In order to properly set the width, you have to loop through each cell in a column and set every width. This will not work with any autofit options selected.
Try this :
Table table = doc.AddTable(2, 2);
table.SetColumnWidth(0, 500);
//first is column index, the second is column width
Bit of an old post to tag to, but after having the same issue it would appear that none of the widths on either the cells or columns actually work, so as a dirty workaround, you can loop through each column and cell adding text to each of the cells, make the text white and finally use the autofit option to autofit to contents eg.
Table t2 = doc.AddTable(2, 8);
for (int i = 0; i < t2.RowCount; i ++)
{
for(int x = 0; x < t2.ColumnCount; x++)
{
t2.Rows[i].Cells[x].Paragraphs.First().Append("12").Color(Color.White);
}
}
t2.AutoFit = AutoFit.Contents;
doc.InsertTable(t2);
This is the way:
Table t = doc.AddTable(1, 5);
t.SetWidthsPercentage(new[] { 20f, 20f, 40f, 10f, 10f }, 500);
The float array sets width percentage for each of the columns, second parameter is the total width of the table.

Excel Interop get width in pixels of text inside cell

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.

Place picture into spread sheet to fill the whole cell (Merged cells)

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.

How to fill the color in cells A1:A5 and B1:B5 and C1:C5 with the same color using C#?

I am currently using the following code to fill cells in the range A1 to A5, and B1 to B5, with yellow:
chartRange1 = xlWorkSheet.get_Range("A1", "A5");
chartRange1.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
Excel.Range chartRange2;
chartRange2 = xlWorkSheet.get_Range("B1", "B5");
chartRange2.Interior.Color= System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
But it takes 2 chartRange objects in order to do this. What if I want to set the same color over a wide range of cells?
Is there a way to do this using a single statement that sets the same color for a larger range of cells?
In your case (A1:A5, B1:B5, C1:C5) you can merge the cells into a contiguous range A1:C5:
xlWorkSheet.get_Range("A1:C5");
But a range does not have to be contiguous. You can also use code like the following:
xlWorkSheet.get_Range("A1:A5,C1:C5,F10:F15");
Have you tried
chartRange = xlWorkSheet.get_Range("A1", "B5");
Are you able to just set the chartRange1 object to null, then reinitialize and set the next range?

Categories