سؤال

I'm using excellibrary and I want to set a row's height or column's width. Sounds very simple, but I can't find a solution. How can I do that?

هل كانت مفيدة؟

المحلول

I guess you are referring to excellibrary? I don't have it loaded up in front of me at the moment, but if memory serves, try:

Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("First Sheet");

worksheet.Cells[0, 0] = new Cell("Cell 0,0");
worksheet.Cells[1, 0] = new Cell("Cell 1,0");

ushort currentHeight = worksheet.Cells.GetRowHeight(1); 
worksheet.Cells.GetRow(1).Height = (ushort)(currentHeight + 20);

workbook.Worksheets.Add(worksheet);
workbook.Save(filePath); // will need to set filepath

Edit: While in theory this is correct, I'm actually not sure this property works (after a quick test in Excel 2010 and Excel 2007).

If you follow the source code then apparently the Row class Height property public field does get written out by the WorksheetEncoder line 55, and in turn through the ROW class Encode method into the Stream writing out the workbook, but if I then open in Excel, it doesn't work. (I don't have Excel versions before 2007 to test in).

Unfortunately there is no other property to use to set the row height. This is the one that gets used by the BinaryWriter to generate the workbook.

Also note, if you are targeting Excel 2010 you will need to pad out a chunk of cells in the above sample to make sure the final file is big enough for Excel 2010 to be happy. e.g.:

Workbook workbook = new Workbook(); 
Worksheet worksheet = new Worksheet("First Sheet"); 

//  Excel 2010 hack - issue 102
for (int r = 0; r < 150; r++) {
    for (int c = 0; c < 10; c++) {
        worksheet.Cells[r, c] = new Cell(" ");
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top