I'm writing a list to an excel sheet generated using EPPlus
with .xlsx
extension. Then using worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
method I tried to fit the columns.
This is how I write data
using (ExcelPackage xlPackage = new ExcelPackage(newFile))
{
System.Data.DataTable dt = new System.Data.DataTable();
var ws = xlPackage.Workbook.Worksheets.FirstOrDefault(x => x.Name == language.Culture);
if (ws == null)
{
int i = 1, j = 0;
worksheet = xlPackage.Workbook.Worksheets.Add(language.Culture);
foreach (ExcelFields fieldValues in UnmatchedFieldList)
{
//code
}
else
{
int i = 0;
worksheet = xlPackage.Workbook.Worksheets[language.Culture];
colCount = worksheet.Dimension.End.Column;
rowCount = worksheet.Dimension.End.Row;
foreach (ExcelFields fieldValues in UnmatchedFieldList)
{
worksheet.Cells[rowCount + 1, count + 1].Value = itemName;
}
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
xlPackage.Save();
}
I read data as
string sheetName = language.Culture;
var excelFile = new ExcelQueryFactory(excelPath);
IQueryable<Row> excelSheetValues = from workingSheet in excelFile.Worksheet(sheetName) select workingSheet;
string[] headerRow = excelFile.GetColumnNames(sheetName).ToArray();
At headerRow it is throwing the below error
When I'm trying to read the data from excel it is throwing an exception
External table is not in the expected format
I found out,this is due to the columns are not formatted(width) correctly. When I manually set the columns width by double clicking the cell and run the code it is working fine
So I want to achieve this using code