Excel 2007 스프레드 시트의 셀에 주석을 추가하려고합니다. OpenXml SDK 2.0을 사용하고 있습니다.OpenXML SDK를 사용하여 Excel 스프레드 시트에서 셀 주석 만들기
사례 : OpenXML 문서를 처음부터 만들지 않고 필자가 복사하여 사용하는 템플릿 Excel 파일을 만들었습니다. 내 템플릿 파일에 A1 셀에 주석이있어서 Excel이 이미 나를 위해 WorksheetCommentPart를 만들었습니다.
내 문제는 주석 부분에 주석 노드를 추가 할 때 스프레드 시트가로드되지 않고 Excel에서 복구할지 묻는 것입니다.
내게 정말로 귀찮은 것은 A1의 원래 주석이 그대로 남아 있지만 프로그래밍 방식으로 추가 한 주석은 모두 사라졌습니다. 여기
내가 함께 일하고 있어요 코드입니다 :사용 (MemoryStream을 spreadsheetStream = 새로운 MemoryStream을()) { GetGradebookSpreadsheetTemplate (spreadsheetStream);
using (SpreadsheetDocument spDoc = SpreadsheetDocument.Open(spreadsheetStream, true))
{
WorkbookPart wbPart = spDoc.WorkbookPart;
WorksheetPart wsPart = wbPart.WorksheetParts.First();
SheetData sheet = wsPart.Worksheet.GetFirstChild<SheetData>();
Comments comments = wsPart.WorksheetCommentsPart.Comments;
comments.Descendants<Author>().First().Text = string.Format("{0}, {1}", instructor.LastName, instructor.FirstName);
comments.Descendants<Text>().First().Text = string.Format("{0}, {1}", instructor.LastName, instructor.FirstName);
List<DefinedName> definedNames = new List<DefinedName>();
definedNames.Add(CreateDefinedName("COLWeb_Gradebook", sheet.NamespaceURI, "Gradebook", "1", "A"));
uint index = 4;
foreach (User u in users)
CreateUserDataRow(index++, definedNames, comments.CommentList, sheet, u, coursesForUsers[u], assignments, submissions[u]);
Cell lastCell = sheet.Descendants<Cell>().Last();
OpenXmlElement dimensionsElement = wsPart.Worksheet.Elements().Where(x => x.LocalName == "dimension").First();
dimensionsElement.SetAttribute(new OpenXmlAttribute("ref", null, "A1:" + lastCell.CellReference));
comments.Save();
wsPart.Worksheet.Save();
wbPart.Workbook.Save();
}
return spreadsheetStream.ToArray();
}
그리고 "CreateUserDataRow는"새 행을 생성하지만, 관련 부분은가 (여기서 "주석"내 댓글 문자열과 "C"것은 내가 대한 코멘트를 작성하려는 내 셀입니다) :
if (!string.IsNullOrEmpty(comment))
{
List<OpenXmlElement> runs = new List<OpenXmlElement>();
foreach (string row in comment.Split(new string[] { "<p>", "</p>" }, StringSplitOptions.RemoveEmptyEntries))
{
string trimmed = row.Trim();
if (!string.IsNullOrEmpty(trimmed))
{
string escaped = System.Security.SecurityElement.Escape(trimmed);
runs.Add(new Run(new RunProperties(), new Text(escaped)));
}
}
Comment commentCell = new Comment();
commentCell.Reference = c.CellReference;
commentCell.AuthorId = 0;
commentCell.AppendChild(new CommentText(runs));
comments.AppendChild(commentCell);
}
지금까지 내 눈으로 볼 수있는, 그리고 KDiff3는 그 문제에 대한, 내 파일 내가 Excel을 열고 세포로 주석을 넣을 것 인 경우에 출력 할 파일을 거의 동일하다 Excel에서 손으로.
누구나 OpenXml을 사용하여 셀에 주석을 첨부하는 좋은 예가 있습니까? 어쩌면 관계에 대해 알아야 할 것이 있습니까? 그것은 내가 만든 Excel 파일을 사용하는 것과 관련이 있으며 템플릿으로 사용하고 있습니까 (아마도 일부 차원이 설정되지 않았을 수 있습니다)?
내가 도와 드릴 수있는 도움을 주셔서 감사합니다.
Yikes. 적어도 나는 지금 어디에서보아야하는지 알고 있습니다. 이제 내 스프레드 시트에 포함 된 VML 파일을보고 있지만 의미가 있습니다. 감사합니다. –
이것은 맞습니다. 주석을 추가하려면 VML을 추가해야합니다.또한 VML을 추가하고 * Excel *과 같은 항목에 * 번호를 붙이지 않거나 레이블을 지정하지 않으면 이름이 바뀌고 이름이 바뀌어 이후에 스프레드 시트가 손상되어 Excel = Yuk!이 (가) 손상됩니다. –