Skip to content

Commit

Permalink
Fixed bug when copying pivottables JanKallman#107
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKallman committed Jan 28, 2018
1 parent 34a195a commit 314b710
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
2 changes: 1 addition & 1 deletion EPPlus/ExcelWorksheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4195,7 +4195,7 @@ public ExcelTableCollection Tables
return _tables;
}
}
ExcelPivotTableCollection _pivotTables = null;
internal ExcelPivotTableCollection _pivotTables = null;
/// <summary>
/// Pivottables defined in the worksheet.
/// </summary>
Expand Down
57 changes: 30 additions & 27 deletions EPPlus/ExcelWorksheets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ public ExcelWorksheet Add(string Name, ExcelWorksheet Copy)
XmlDocument worksheetXml = new XmlDocument();
worksheetXml.LoadXml(Copy.WorksheetXml.OuterXml);
worksheetXml.Save(streamWorksheet);
//streamWorksheet.Close();
_pck.Package.Flush();


Expand Down Expand Up @@ -403,55 +402,55 @@ private void CopyPivotTable(ExcelWorksheet Copy, ExcelWorksheet added)
string xml = tbl.PivotTableXml.OuterXml;

string name;
if (prevName == "")
if(Copy.Workbook==added.Workbook || added.PivotTables._pivotTableNames.ContainsKey(tbl.Name))
{
name = Copy.PivotTables.GetNewTableName();
if (prevName == "")
{
name = added.PivotTables.GetNewTableName();
}
else
{
int ix = int.Parse(prevName.Substring(10)) + 1;
name = string.Format("PivotTable{0}", ix);
while (_pck.Workbook.ExistsPivotTableName(name))
{
name = string.Format("PivotTable{0}", ++ix);
}
}
}
else
{
int ix=int.Parse(prevName.Substring(10))+1;
name = string.Format("PivotTable{0}", ix);
while (_pck.Workbook.ExistsPivotTableName(name))
{
name = string.Format("PivotTable{0}", ++ix);
}
name = tbl.Name;
}
prevName=name;
XmlDocument xmlDoc = new XmlDocument();
//TODO: Fix save pivottable here
//Copy.Save(); //Save the worksheet first
xmlDoc.LoadXml(xml);
//xmlDoc.SelectSingleNode("//d:table/@id", tbl.NameSpaceManager).Value = Id.ToString();
xmlDoc.SelectSingleNode("//d:pivotTableDefinition/@name", tbl.NameSpaceManager).Value = name;
xml = xmlDoc.OuterXml;

int Id = _pck.Workbook._nextPivotTableID++;
//var uriTbl = new Uri(string.Format("/xl/pivotTables/pivotTable{0}.xml", Id), UriKind.Relative);
var uriTbl = GetNewUri(_pck.Package, "/xl/pivotTables/pivotTable{0}.xml", ref Id);
if (_pck.Workbook._nextPivotTableID < Id) _pck.Workbook._nextPivotTableID = Id;
var partTbl = _pck.Package.CreatePart(uriTbl, ExcelPackage.schemaPivotTable , _pck.Compression);
StreamWriter streamTbl = new StreamWriter(partTbl.GetStream(FileMode.Create, FileAccess.Write));
streamTbl.Write(xml);
//streamTbl.Close();
streamTbl.Flush();

xml = tbl.CacheDefinition.CacheDefinitionXml.OuterXml;
//var uriCd = new Uri(string.Format("/xl/pivotCache/pivotcachedefinition{0}.xml", Id), UriKind.Relative);
//while (_pck.Package.PartExists(uriCd))
//{
// uriCd = new Uri(string.Format("/xl/pivotCache/pivotcachedefinition{0}.xml", ++Id), UriKind.Relative);
//}
var uriCd = GetNewUri(_pck.Package, "/xl/pivotCache/pivotcachedefinition{0}.xml", ref Id);
var partCd = _pck.Package.CreatePart(uriCd, ExcelPackage.schemaPivotCacheDefinition, _pck.Compression);
StreamWriter streamCd = new StreamWriter(partCd.GetStream(FileMode.Create, FileAccess.Write));
streamCd.Write(xml);
streamCd.Flush();

added.Workbook.AddPivotTable(Id.ToString(), uriCd);

xml = "<pivotCacheRecords xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" count=\"0\" />";
var uriRec = new Uri(string.Format("/xl/pivotCache/pivotrecords{0}.xml", Id), UriKind.Relative);
var uriRec = new Uri(string.Format("/xl/pivotCache/pivotCacheRecords{0}.xml", Id), UriKind.Relative);
while (_pck.Package.PartExists(uriRec))
{
uriRec = new Uri(string.Format("/xl/pivotCache/pivotrecords{0}.xml", ++Id), UriKind.Relative);
uriRec = new Uri(string.Format("/xl/pivotCache/pivotCacheRecords{0}.xml", ++Id), UriKind.Relative);
}
var partRec = _pck.Package.CreatePart(uriRec, ExcelPackage.schemaPivotCacheRecords, _pck.Compression);
StreamWriter streamRec = new StreamWriter(partRec.GetStream(FileMode.Create, FileAccess.Write));
Expand All @@ -462,7 +461,9 @@ private void CopyPivotTable(ExcelWorksheet Copy, ExcelWorksheet added)
added.Part.CreateRelationship(UriHelper.ResolvePartUri(added.WorksheetUri, uriTbl), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/pivotTable");
partTbl.CreateRelationship(UriHelper.ResolvePartUri(tbl.Relationship.SourceUri, uriCd), tbl.CacheDefinition.Relationship.TargetMode, tbl.CacheDefinition.Relationship.RelationshipType);
partCd.CreateRelationship(UriHelper.ResolvePartUri(uriCd, uriRec), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/pivotCacheRecords");

}
added._pivotTables = null; //Reset collection so it's reloaded when accessing the collection next time.
}
private void CopyHeaderFooterPictures(ExcelWorksheet Copy, ExcelWorksheet added)
{
Expand Down Expand Up @@ -513,7 +514,7 @@ private void CloneCells(ExcelWorksheet Copy, ExcelWorksheet added)
added.MergedCells.Add(new ExcelAddress(r),false);
}

//Shared Formulas
//Shared Formulas
foreach (int key in Copy._sharedFormulas.Keys)
{
added._sharedFormulas.Add(key, Copy._sharedFormulas[key].Clone());
Expand Down Expand Up @@ -989,12 +990,14 @@ internal void ReindexWorksheetDictionary()
_worksheets = worksheets;
}

/// <summary>
/// Returns the worksheet at the specified position.
/// </summary>
/// <param name="PositionID">The position of the worksheet. 1-base</param>
/// <returns></returns>
public ExcelWorksheet this[int PositionID]
/// <summary>
/// Returns the worksheet at the specified position.
/// </summary>
/// <param name="PositionID">The position of the worksheet. Collection is zero based or one-base depending on the Package.Compatibility.IsWorksheets1Based propery. </param>
///
/// <seealso cref="ExcelPackage.Compatibility"/>
/// <returns></returns>
public ExcelWorksheet this[int PositionID]
{
get
{
Expand Down
18 changes: 17 additions & 1 deletion EPPlusTest/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,7 @@ public void Issue99()
}
}
}
[TestMethod]
[TestMethod, Ignore]
public void Issue94()
{
using (var package = new ExcelPackage(new FileInfo(@"c:\temp\bug\iss94\MergedCellsTemplate.xlsx")))
Expand All @@ -1938,5 +1938,21 @@ public void Issue94()
package.SaveAs(new FileInfo(@"c:\temp\bug\iss94\MergedCellsTemplateSaved.xlsx"));
}
}
[TestMethod,Ignore]
public void Issue107()
{
using (ExcelPackage epIN = new ExcelPackage(new FileInfo(@"C:\temp\sampleapp\sample12.xlsx")))
using (ExcelPackage epOUT = new ExcelPackage(new FileInfo(@"C:\temp\bug\pivotbug107.xlsx")))
{
foreach (ExcelWorksheet sheet in epIN.Workbook.Worksheets)
{
ExcelWorksheet newSheet = epOUT.Workbook.Worksheets.Add(sheet.Name, sheet);
}
epIN.Workbook.Worksheets.Add(epIN.Workbook.Worksheets[1].Name + "-2", epIN.Workbook.Worksheets[1]);
epIN.Workbook.Worksheets.Add(epIN.Workbook.Worksheets[2].Name + "-2", epIN.Workbook.Worksheets[2]);
epOUT.Save();
epIN.SaveAs(new FileInfo(@"C:\temp\bug\pivotbug107-SameWB.xlsx"));
}
}
}
}

0 comments on commit 314b710

Please sign in to comment.