If a worksheet is replaced in a workbook, an InvalidOperationException "Error saving file ..." occurs. The inner exception is "Cannot add part for the specified URI because it is already in the package."
I am looking for any work around, patch or correction to the code.
Thanks in advance!
Steps to reproduce...
1) Existing workbook has a worksheet with comments.
2) Open workbook.
3) Delete existing comments for worksheet
4) Delete worksheet.
5) Add new worksheet with comments.
6) Save <- exception.
Exception occurs at
at System.IO.Packaging.Package.CreatePart(Uri partUri, String contentType, CompressionOption compressionOption)
at OfficeOpenXml.ExcelWorksheet.SaveComments()
at OfficeOpenXml.ExcelWorksheet.Save()
at OfficeOpenXml.ExcelWorkbook.Save()
at OfficeOpenXml.ExcelPackage.Save()
Test application
```
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using OfficeOpenXml;
namespace EPPlus_URI_issue
{
class Program
{
static void Main( string[] args )
{
string filepath = @"c:\temp\xyz.xlsx";
string worksheetName = "worksheet";
if ( System.IO.File.Exists( filepath ) )
System.IO.File.Delete( filepath );
using ( ExcelPackage package = new ExcelPackage( new FileInfo( filepath ) ) )
{
ExcelWorkbook workbook = package.Workbook;
ExcelWorksheet worksheet = workbook.Worksheets.Add( worksheetName );
worksheet.Cells["A1"].Value = "A1";
worksheet.Cells["A1"].AddComment( "comment...", "me" );
package.Save();
}
using ( ExcelPackage package = new ExcelPackage( new FileInfo( filepath ) ) )
{
ExcelWorkbook workbook = package.Workbook;
ExcelWorksheet worksheet = workbook.Worksheets[ worksheetName ];
while( worksheet.Comments.Count > 0 )
worksheet.Comments.RemoveAt( 0 );
workbook.Worksheets.Delete( worksheetName );
worksheet = workbook.Worksheets.Add( worksheetName );
worksheet.Cells["A1"].Value = "new";
worksheet.Cells["A1"].AddComment( "new comment...", "me" );
package.Save(); // error happens here!!!
}
}
}
}
```
Comments: I get this exact same issue, but only on some machines. I bypassed it by creating new worksheet first with a different name, then deleting the old one then renaming. =\
I am looking for any work around, patch or correction to the code.
Thanks in advance!
Steps to reproduce...
1) Existing workbook has a worksheet with comments.
2) Open workbook.
3) Delete existing comments for worksheet
4) Delete worksheet.
5) Add new worksheet with comments.
6) Save <- exception.
Exception occurs at
at System.IO.Packaging.Package.CreatePart(Uri partUri, String contentType, CompressionOption compressionOption)
at OfficeOpenXml.ExcelWorksheet.SaveComments()
at OfficeOpenXml.ExcelWorksheet.Save()
at OfficeOpenXml.ExcelWorkbook.Save()
at OfficeOpenXml.ExcelPackage.Save()
Test application
```
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using OfficeOpenXml;
namespace EPPlus_URI_issue
{
class Program
{
static void Main( string[] args )
{
string filepath = @"c:\temp\xyz.xlsx";
string worksheetName = "worksheet";
if ( System.IO.File.Exists( filepath ) )
System.IO.File.Delete( filepath );
using ( ExcelPackage package = new ExcelPackage( new FileInfo( filepath ) ) )
{
ExcelWorkbook workbook = package.Workbook;
ExcelWorksheet worksheet = workbook.Worksheets.Add( worksheetName );
worksheet.Cells["A1"].Value = "A1";
worksheet.Cells["A1"].AddComment( "comment...", "me" );
package.Save();
}
using ( ExcelPackage package = new ExcelPackage( new FileInfo( filepath ) ) )
{
ExcelWorkbook workbook = package.Workbook;
ExcelWorksheet worksheet = workbook.Worksheets[ worksheetName ];
while( worksheet.Comments.Count > 0 )
worksheet.Comments.RemoveAt( 0 );
workbook.Worksheets.Delete( worksheetName );
worksheet = workbook.Worksheets.Add( worksheetName );
worksheet.Cells["A1"].Value = "new";
worksheet.Cells["A1"].AddComment( "new comment...", "me" );
package.Save(); // error happens here!!!
}
}
}
}
```
Comments: I get this exact same issue, but only on some machines. I bypassed it by creating new worksheet first with a different name, then deleting the old one then renaming. =\