Xslio : correct width of the columns

Hi,
I 'm creating an excel file based on a txt file.
The created file looks like :

I would like to see all the columns with the width of the largest rows width.
I tried with AutofitColumns :
foreach (string rowCell in row)
{
sheet.Range[i, j].Text = rowCell;
sheet.Range[i, j].HorizontalAlignment = ExcelHAlign.HAlignLeft;
sheet.Range[i, j].AutofitColumns();
j++;
}
but it didnt work.
Can you suggest me the best way to do that?
Thank you
Davide

4 Replies

PK Prakash Kumar D Syncfusion Team October 11, 2018 11:25 AM UTC

Hi Davide, 
 
Thank you for contacting Syncfusion support. 
 
We tried to reproduce the issue, but the reported scenario was working properly with .xlsx format. We suspect that you have been saving the file as CSV format. CSV format will not save formats (autofit) which is a behavior of Microsoft Excel, XlsIO does the same.  
 
kindly confirm us the save format type which will be helpful to investigate and provide you the prompt solution. 
 
Regards, 
Prakash Kumar 



DA Davide Azzaroni October 11, 2018 11:40 AM UTC

hi,

I am not opening the Excel file from a txt file.
I 'am reading a txt file and then create at runtime an Excel.
Here the code  :

 private static void ReadFile(string path, IWorksheet sheet)
        {
            using (StreamReader sr = new StreamReader(path))
            {
                char[] delimiter = { '\t' };
                string[] columnheaders = sr.ReadLine()?.Split(delimiter);
                int j = 1;
               // headers
                foreach (string colheader in columnheaders)
                {
                    sheet.Range[1, j].Text = colheader;
                    sheet.Range[1, j].HorizontalAlignment = ExcelHAlign.HAlignCenter;
                    sheet.Range[1, j].AutofitColumns();

                    j++;
                }

                int i = 3;
               // rows
                while (sr.Peek() > 0)
                {
                    j = 1;
                    var row = sr.ReadLine().Split(delimiter);
                    foreach (var rowCell in row)
                    {
                        sheet.Range[i, j].Text = rowCell;
                        sheet.Range[i, j].HorizontalAlignment = ExcelHAlign.HAlignLeft;
                        sheet.Range[i, j].AutofitColumns();

                        j++;
                    }

                    i++;

                }
                sheet.Range["A1:Z1"].AutofitColumns();
            }

        }


I tried to force the view with     :
 "sheet.Range["A1:Z1"].AutofitColumns();", 
but it didnt work.

Any suggestions?
thank you
Davide







PK Prakash Kumar D Syncfusion Team October 11, 2018 12:47 PM UTC

 
Hi Davide, 
 
Thank you for sharing the code snippet. 
 
To achieve your requirement, we suggest you to call the AutofitColumns() at the end of the manipulations. 
 
Code Snippet: 
  sheet.UsedRange.AutofitColumns(); 
 
 We have modified the code snippet and shared the sample for your reference. 
 
 
Regards, 
Prakash Kumar 



DA Davide Azzaroni October 11, 2018 12:57 PM UTC

It worked,
thank you

Davide


Loader.
Up arrow icon