Free Support Forum - aspose.com

How to get the width of a Cell?

I see the way to get a columns' width, but is there a way to query a Cell and get it's required width?

In other words, if I have the text:

"The quick brown fox jumped over the fence" in Cell A1, and Column A's current width is X, how can I determine what the width of Cell A1 would need to be in order to know that if that was the only cell I was interested in for that column, that I could resize the column to Cell A1, even though, say A20 was twice as long.

Hi,

Well, you should try your own code your your task. You may extract the width of the string value of the cell with and compare the column width (in unit of pixels).

E.g..,

Workbook wb = new Workbook();
wb.Open(@"D:\test\Book1.xls");
Worksheet worksheet = wb.Worksheets[0];
Cells cells = worksheet.Cells;
Cell cell = cells["A1"];
string cellval = cell.StringValue;
SizeF sizeF = new SizeF();
int width = 0;
string fontName = "Arial";
int fontSize = 10;
FontStyle fontStyle = FontStyle.Regular;// "Regular";
.........
System.Drawing.Font f = new System.Drawing.Font(fontName, fontSize, fontStyle, GraphicsUnit.Point);
Bitmap bitmap = new Bitmap(10, 10);
Graphics g = Graphics.FromImage(bitmap);
sizeF = g.MeasureString(cellval, f);
double size = sizeF.Width;
............

Thank you.