Create excel file and auto expand column width

I have a regular html table:

<table>
<tr>hello</tr>
<tr>world</tr>
</table> 

and I create an XLS file from it:

string randomname = @"C:\attachmentsfolder\"  + System.IO.Path.GetRandomFileName() + ".xls";
System.IO.File.WriteAllText( randomname, message);

When I open the generated XLS file, I need to REVERSE expand the columns to see long data.

My question is: how can I generate this XLS file so that the columns are already properly configured?

+5
source share
3 answers

You can easily do this with EPPlus (Open Source.NET Excel 2007+ library) and you will have a valid excel file, here is a sample code:

public static void FitAndSaveToExcel(FileInfo excelFile, string sheetName)
{
  ExcelPackage pack = new ExcelPackage();
  ExcelWorksheet ws = pack.Workbook.Worksheets.Add(sheetName);
  ws.Cells[1, 1].Value = "Some Long text that needs fitting!";
  ws.Cells[1, 2].Value = "Short one";
  ws.Column(1).AutoFit();
  pack.SaveAs(excelFile);
}
+3
source

Excel VBA , Rng.Columns.AutoFit. , # Rng.Columns.AutoFit();.

Diodeus, html.

+1

You can also use a third-party tool, for example, for example. Aspose.Cells to create an Excel file.

I have successfully used this in many projects. It provides the unattended installation function that you need.

0
source

All Articles