2010-05-26 8 views
7

LINQ to XML을 사용하여 Excel XML 파일을 만듭니다. 셀 안에 개조 문자를 포함하고 싶습니다. Excel에서는 
 리터럴을 사용하여 새 줄을 나타냅니다. 내가 XText 사용하여이를 추가하려고하면 :Excel의 XML 줄 바꾸기

XText newlineElement = new XText("Foo
Bar"); 

를 내가 얻을 : 엑셀에 표시

Foo
Bar

:

Foo
Bar

는 방식이 있나요 do

없이 XML 파일에 &#10을 작성하십시오.
String.Replace("
", "
") 

결과 파일 텍스트 위에?

EDIT 다음은 Excel 문서의 행을 만드는 방법을 보여주는 코드입니다. 좀 지저분하기 때문에 나는 그것을 피하고 있었다. :) 더 많은 코드가 도움이되는지 아니면 더 많은 혼란이 추가되는지 알려주십시오.

이 예제에서 위에 설명 된 newlineElement은 코드 샘플에서 XText로만 표현됩니다.

const string CELL = "{urn:schemas-microsoft-com:office:spreadsheet}Cell"; 
const string DATA = "{urn:schemas-microsoft-com:office:spreadsheet}Data"; 
const string STYLE = "{urn:schemas-microsoft-com:office:spreadsheet}StyleID"; 
const string TYPE= "{urn:schemas-microsoft-com:office:spreadsheet}Type"; 
const string HEIGHT = "{urn:schemas-microsoft-com:office:spreadsheet}Height"; 
const string ROW = "{urn:schemas-microsoft-com:office:spreadsheet}Row"; 

...

XElement rowElement = new XElement(Row, 
            new XElement(CELL, 
             new XAttribute(STYLE, "s0")), 
            new XElement(CELL, 
             new XElement(DATA, 
              new XText(description.Replace("\n", "
")), 
              new XAttribute(TYPE, "String")), 
             new XAttribute(STYLE, "sWrap")), 
            new XElement(CELL, 
             new XAttribute(STYLE, "s0")), 
            new XAttribute(HEIGHT, height)); 

이것은 생산 : 도움을

<ss:Row ss:Height="45"> 
    <ss:Cell ss:StyleID="s0" /> 
    <ss:Cell ss:StyleID="sWrap"> 
     <ss:Data ss:Type="String">Foo&amp;#10;Bar</ss:Data> 
    </ss:Cell> 
    <ss:Cell ss:StyleID="s0" /> 
    </ss:Row> 

감사합니다 지금까지!

+0

믿거 나 말거나 가능하지 않습니다. – SLaks

+0

Excel 파일에 어떻게 들어 오는지 예제 코드를 게시 할 수 있습니까? – CoderDennis

+0

기본적으로 newlineElement 객체를 만든 후에 어떻게 사용하고 있는지 보여주는 코드를 보면 좋을 것입니다. – CoderDennis

답변

1

이스케이프 코드가 아닌 문자열에 실제 문자 삽입 - LINQ가 변환을 처리합니다.

편집 :

좋아 - 이것은 완전히 잘못했지만 (내가 생각하는)이 작동하는 무언가를 얻기 위해 저를 도왔다. 그렇지 않으면 XML 렌더링에 의해 이스케이프되는 항목을 포함 시키려면 CDATA 블록에 포함해야합니다. C#에서 this related question을 확인하고 내 기억을 트 랩핑 한 XML을 확인하십시오.

+0

나는 그렇게 생각하지 않는다. – SLaks

+0

"\ n"을 사용하여 인라인을 삽입 할 수 있다고 생각합니다. –

+0

그렇지 않습니다. 나는 그것을 시도했다. – SLaks

1

이 도움이된다면 모르겠지만, 다음 코드 않음 다음 newlineElement를 사용하는 코드에서, 그래서

Foo$amp;#10;Bar

Foo&#10;Bar

:

var newlineElement = new XText("Foo&#10;Bar"); 
Console.WriteLine(newlineElement); 
Console.WriteLine(newlineElement.Value); 

이 출력을 생성합니다 개체, .Value 속성을 사용할 수 있습니까?