diff --git a/ooxml/POIXMLRelation.cs b/ooxml/POIXMLRelation.cs index 863fd5ebc..32f795126 100644 --- a/ooxml/POIXMLRelation.cs +++ b/ooxml/POIXMLRelation.cs @@ -140,7 +140,7 @@ public String GetFileName(int index) public int GetFileNameIndex(POIXMLDocumentPart part) { Regex regex = new Regex(_defaultName.Replace("#", "(\\d+)")); - return int.Parse(regex.Match(part.GetPackagePart().PartName.Name).Value); + return int.Parse(regex.Match(part.GetPackagePart().PartName.Name).Groups[1].Value); //return Integer.valueOf(part.getPackagePart().getPartName().getName().replaceAll(regex, "$1")); } /** diff --git a/ooxml/XSSF/UserModel/XSSFSheet.cs b/ooxml/XSSF/UserModel/XSSFSheet.cs index 1b072bc76..728476cd0 100644 --- a/ooxml/XSSF/UserModel/XSSFSheet.cs +++ b/ooxml/XSSF/UserModel/XSSFSheet.cs @@ -5121,6 +5121,8 @@ private void ShiftCommentsAndRows(int startRow, int endRow, int n, bool copyRowH SortedDictionary commentsToShift = new SortedDictionary(new ShiftCommentComparator(n)); + IEnumerable ctShapes = GetVMLDrawing(false)?.GetItems().OfType(); + foreach (KeyValuePair rowDict in _rows) { XSSFRow row = rowDict.Value; @@ -5143,11 +5145,22 @@ private void ShiftCommentsAndRows(int startRow, int endRow, int n, bool copyRowH .FindCellComment(cellAddress); if (oldComment != null) { + var ctShape = oldComment.GetCTShape(); + + if (ctShape == null && ctShapes != null) + { + ctShape = ctShapes.FirstOrDefault + (x => + x.ClientData[0].row[0] == cellAddress.Row && + x.ClientData[0].column[0] == cellAddress.Column + ); + } + XSSFComment xssfComment = new XSSFComment( sheetComments, oldComment.GetCTComment(), - oldComment.GetCTShape()); + ctShape); if (commentsToShift.ContainsKey(xssfComment)) { commentsToShift[xssfComment] = newrownum; diff --git a/ooxml/XSSF/UserModel/XSSFWorkbook.cs b/ooxml/XSSF/UserModel/XSSFWorkbook.cs index 9c52d820e..e7619bb20 100644 --- a/ooxml/XSSF/UserModel/XSSFWorkbook.cs +++ b/ooxml/XSSF/UserModel/XSSFWorkbook.cs @@ -2421,7 +2421,33 @@ public bool CellFormulaValidation public int AddPicture(byte[] pictureData, PictureType format) { - int imageNumber = GetAllPictures().Count + 1; + int imageNumber = 1; + List allPics = (List)GetAllPictures(); + + if (allPics.Any()) + { + List sortedIndexs = new List { 0 }; + + sortedIndexs.AddRange + ( + allPics + .Select(pic => XSSFPictureData.RELATIONS[(int)pic.PictureType].GetFileNameIndex(pic)) + .OrderBy(i => i) + .ToList() + ); + + int previous = sortedIndexs[0]; + for (int index = 1; index < sortedIndexs.Count; index++) + { + if (sortedIndexs[index] > previous + 1) + break; + + previous = sortedIndexs[index]; + } + + imageNumber = previous + 1; + } + XSSFPictureData img = (XSSFPictureData)CreateRelationship(XSSFPictureData.RELATIONS[(int)format], XSSFFactory.GetInstance(), imageNumber, true).DocumentPart; try { @@ -2434,8 +2460,9 @@ public int AddPicture(byte[] pictureData, PictureType format) throw new POIXMLException(e); } pictures.Add(img); - return imageNumber - 1; + // returns image Index + return allPics.Count - 1; } public XSSFWorkbookType WorkbookType