这段代码有什么问题吗?这给了我损坏的内容

时间:2011-05-05 17:14:05

标签: c# openxml

private static void UpdateFontSettings(WordprocessingDocument content, WordprocessingDocument template)

    {
        if (content != null && content.MainDocumentPart != null && content.MainDocumentPart.FontTablePart != null &&
            template != null && template.MainDocumentPart != null && template.MainDocumentPart.FontTablePart != null)
        {
            IEnumerable<OpenXmlPart> partsToDelete = from idPartPair in content.MainDocumentPart.FontTablePart.Parts
                                                     select idPartPair.OpenXmlPart;

            content.MainDocumentPart.FontTablePart.DeleteParts(partsToDelete);

            StreamReader streamReader = null;
            StreamWriter streamWriter = null;

            try
            {
                streamReader = new StreamReader(template.MainDocumentPart.FontTablePart.GetStream());
                streamWriter = new StreamWriter(content.MainDocumentPart.FontTablePart.GetStream(FileMode.Create));
            }
            catch
            {
                if (streamReader != null)
                {
                    streamReader.Close();
                }

                throw;
            }

            using (streamWriter)
            {
                streamWriter.Write(streamReader.ReadToEnd());
            }

            if (streamReader != null)
            {
                streamReader.Close();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我建议您在创建StreamWriter时设置编码类型。我相信大多数系统默认为ANSI,但您可能需要UTF8或其他东西。