Using C#, I am trying to take a csv file and create an Excel file from it using an EPPlus library. I am able to do so. After creating the Excel file, I pass it to a user who opens it in Excel. When he opens it, the file opens as a table, as opposed to a range. In Excel, one can go to Table Tools/Design, and click on Convert to Range. The user has requested that I create the Excel file in such a way that he will not have to go through the conversion step each time. I am unable to find to find a way to do it. I tried using different parameters for LoadFromText
without success. Here is my code:
using OfficeOpenXml;
using OfficeOpenXml.Table;
.
.
.
FileInfo txtFile = new FileInfo(textFileName);
FileInfo xlsxFile;
using (ExcelPackage package = new ExcelPackage())
{
var XLformat = new ExcelTextFormat
{
Delimiter = OutputDelimiter[0],
TextQualifier = textQualifier,
SkipLinesBeginning = 0,
Culture = CultureInfo.GetCultureInfo("en-US"),
Encoding = Encoding.UTF8,
SkipLinesEnd = 1
};
var sheet = package.Workbook.Worksheets.Add(sheetName);
sheet.Cells["A1"].LoadFromText(txtFile, XLformat, TableStyles.None, true);
sheet.Tables[0].ShowFilter = false;
package.SaveAs(xlsxFile);
}
Replace
sheet.Tables[0].ShowFilter = false;
with
sheet.Tables.Delete(0);
That way you remove the table but keep the data as range.
Edit:
There is a easier way, which is not create a table at all, just use the right overload:
sheet.Cells["A1"].LoadFromText(txtFile, XLformat);