/// <summary>
/// Creates a new instance of the ExcelPackage class based on a existing file or creates a new file.
/// </summary>
/// <param name="newFile">If newFile exists, it is opened. Otherwise it is created from scratch.</param>
private ExcelPackage(FileInfo newFile)
{
_outputFolderPath = newFile.DirectoryName;
if (newFile.Exists)
{
// open the existing package
_package = Package.Open(newFile.FullName, FileMode.Open, FileAccess.ReadWrite);
}
else
{
// create a new package and add the main workbook.xml part
_package = Package.Open(newFile.FullName, FileMode.Create, FileAccess.ReadWrite);
// save a temporary part to create the default application/xml content type
var uriDefaultContentType = new Uri("/default.xml", UriKind.Relative);
var partTemp = _package.CreatePart(uriDefaultContentType, "application/xml");
var workbook = Workbook.WorkbookXml; // this will create the workbook xml in the package
// create the relationship to the main part
_package.CreateRelationship(Workbook.WorkbookUri,
TargetMode.Internal,
schemaRelationships + "/officeDocument");
// remove the temporary part that created the default xml content type
_package.DeletePart(uriDefaultContentType);
}
}