I am using the closed XML for generating the excel template. After filling the data in the generated excel, I am trying to import the excel with data. It returns error "The specified package is invalid. The main part is missing." . I am using XLSX file.
```
try
{
var workbook = new XLWorkbook(FILE_PATH);
}
catch (Exception ex)
{
throw;
}
```
Please provide a solution.
Comments: So... updated code... slightly different issue.
Use case #1: Use the code below to have the system create an Excel file, load it with data, and save the file... works.
Use case #2: Manually create an Excel file (using Excel), save it, close it. Then use the code below to edit that file (i.e. update data)... works.
Use case #3: Do use case #1 (i.e. create file using code). Then, use the code below to edit that file (i.e. update data)... FAILS.
The error I get for that now is: "Range is invalid."
Note about the code below:
* The top part of the if statement creates a new file
* The bottom part of the if statement edits an existing file
* When creating new, it loads and formats the data nicely
* Editing works, but the data is formatted poorly - that's why I'm trying to clean it up. It would be great if there were a way to simply load my DataTable into an existing worksheet to replace the data.
* I don't want to delete the worksheet and re-add a new worksheet as we want the sheet to drive charts and reporting and deleting the sheet would break the links. I really want to just refresh the data.
Code follows:
```
// Load data
if (rdoNew.Checked)
{
// Create new file
var wb = new XLWorkbook();
// Add worksheet - load datatable into sheet
wb.Worksheets.Add(dataTable);
// Save file
string fileName = txtFilePath.Text + @"\" + cleanFileName(); // Putting in this var for ease of troubleshooting
wb.SaveAs(fileName);
}
else
{
// Open existing file...
var wb = new XLWorkbook(txtFilePath.Text);
// Select first worksheet in workbook // TODO: Allow user to select worksheet
var ws = wb.Worksheet(1);
// Remove all data from worksheet
ws.Columns("A:BB").Delete(); // TODO: Better way to do this?
// Load column headers
for (int i = 0; i < dataTable.Columns.Count; i++)
{
if (i > 25)
{
break; // TODO: Enhance/improve this so we can have column headers past column Z. This will likely be rare so leaving it for now.
}
ws.Cell(alphabet.Substring(i, 1) + "1").Value = dataTable.Columns[i].ColumnName;
}
// Load data
var data = dataTable.AsEnumerable();
ws.Cell("A2").Value = data;
// Format data
ws.Columns().AdjustToContents(1,50);
ws.Cell("A1").Select();
// Save file
wb.Save();
}
```
The error happens on the last line of the edit: wb.Save();
If it helps, the stack trace is:
```
at ClosedXML.Excel.XLRangeAddress.get_FirstAddress()
at ClosedXML.Excel.XLRangeAddress.Equals(Object obj)
at ClosedXML.Excel.XLTable.get_FieldNames()
at ClosedXML.Excel.XLWorkbook.<>c__DisplayClasse3.<GenerateSharedStringTablePartContent>b__dd(IXLTable t)
at ClosedXML.Excel.EnumerableExtensions.ForEach[T](IEnumerable`1 source, Action`1 action)
at ClosedXML.Excel.XLWorkbook.<>c__DisplayClasse3.<GenerateSharedStringTablePartContent>b__dc(IXLWorksheet w)
at ClosedXML.Excel.EnumerableExtensions.ForEach[T](IEnumerable`1 source, Action`1 action)
at ClosedXML.Excel.XLWorkbook.GenerateSharedStringTablePartContent(SharedStringTablePart sharedStringTablePart, SaveContext context)
at ClosedXML.Excel.XLWorkbook.CreateParts(SpreadsheetDocument document)
at ClosedXML.Excel.XLWorkbook.CreatePackage(String filePath, SpreadsheetDocumentType spreadsheetDocumentType)
at ClosedXML.Excel.XLWorkbook.Save()
at Desktop_API.frmMain.btnGetData_Click(Object sender, EventArgs e) in c:\Users\...\frmMain.cs:line 91
```
Thanks for any assistance!