기본 스타일 "제목 1", "제목 2", "제목"등을 사용하여 OpenXML을 사용하여 문서를 작성하려고했기 때문에 저와 같은 독자라면이 게시물을 발견했습니다. Microsoft Word 몇 시간 후에 해결책을 찾았습니다.
먼저 일반 템플릿 "Normal.dotm"에서 스타일을 찾으려고했습니다. 이것은 스타일이 저장되는 곳이 아니며, 잘못된 장소를보고 있습니다. 기본 스타일은 실제로 QuickStyles라는 디렉토리의 "Default.dotx"파일에 정의됩니다.
경로는 사용중인 버전과 OS에 따라 변경 될 것입니다. 나는 C : \ Program Files (x86) \ Microsoft Office \ Office14 \ 1033 \ QuickStyles에있는 점을 발견했다.
void CreateWordDocumentUsingMSWordStyles(string outputPath, string templatePath)
{
// create a copy of the template and open the copy
System.IO.File.Copy(templatePath, outputPath, true);
using (var document = WordprocessingDocument.Open(outputPath, true))
{
document.ChangeDocumentType(WordprocessingDocumentType.Document);
var mainPart = document.MainDocumentPart;
var settings = mainPart.DocumentSettingsPart;
var templateRelationship = new AttachedTemplate { Id = "relationId1" };
settings.Settings.Append(templateRelationship);
var templateUri = new Uri("c:\\anything.dotx", UriKind.Absolute); // you can put any path you like and the document styles still work
settings.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", templateUri, templateRelationship.Id);
// using Title as it would appear in Microsoft Word
var paragraphProps = new ParagraphProperties();
paragraphProps.ParagraphStyleId = new ParagraphStyleId { Val = "Title" };
// add some text with the "Title" style from the "Default" style set supplied by Microsoft Word
var run = new Run();
run.Append(new Text("My Title!"));
var paragraph = new Paragraph();
paragraph.Append(paragraphProps);
paragraph.Append(run);
mainPart.Document.Body.Append(paragraph);
mainPart.Document.Save();
}
}
간단히 templatePath이 Default.dotx 파일을 가리키는이 메소드를 호출하고 기본 스타일을 사용할 수 있습니다 :
내가 만들고 템플릿에서 문서를 수정하는
this blog post에서 몇 가지 코드를 발견 Microsoft Word에 표시됩니다.
var path = System.IO.Path.GetTempFileName();
CreateWordDocumentUsingMSWordStyles(path, "C:\\Program Files (x86)\\Microsoft Office\\Office14\\1033\\QuickStyles\\Default.dotx");
그들이 원래의 질문에 따라 문서를 열 번이 Word에서 사용자 변경 "스타일 설정"을 할 수 없습니다.
기본 스타일은 '보통'입니다. 뭘 물어 보는 거냐? – jn1kk
"제목 1", "제목"등을 생각하고있었습니다. 템플릿 문서를 반영하고 코드에 수동으로 스타일을 추가하여이를 수행하는 방법을 찾았습니다. – ruffen