2013-05-17 2 views
0

OpenXML을 사용하여 XLSX 파일을 만드는 C# 클래스를 작성했습니다. 필자는 정형화 된 비트를 얻을 수 있었지만 번호 매기기 형식 (특히 통화 형식)을 적용하지 않는 행운은 없었습니다.OpenXML의 번호 매기기 형식 C#

다른 사람들의 코드 예제를 복사/조정하려고 시도했지만 스프레드 시트를 열 때 두려운 '잘못된 콘텐츠'오류가 나타납니다. 아래는 현재 스타일 시트를 만들 때 사용하고있는 코드입니다. 나는 NumberingFormats 추가하려고 (아직 CellFormat에서 그것을 참조를 시도조차하지 않은하지만 이미 오류 점점)입니다!

/// <summary> A function to generate a style sheet which will allow the spreadsheet to use alternate fonts, fills, borders etc </summary> 
    private Stylesheet GenerateStyleSheet2() 
    { 
     // Create a new style sheet 
     Stylesheet newStyleSheet = new Stylesheet(); 

     newStyleSheet.Append(new Fonts(
       new Font(                // Index 0 - The default font. 
        new FontSize() { Val = 11 }, 
        new Color() { Rgb = new HexBinaryValue() { Value = "000000" } }, 
        new FontName() { Val = "Calibri" }), 
       new Font(                // Index 1 - The bold large font. 
        new Bold(), 
        new FontSize() { Val = 12 }, 
        new Color() { Rgb = new HexBinaryValue() { Value = "000000" } }, 
        new FontName() { Val = "Calibri" }), 
       new Font(                // Index 2 - The bold and italic font. 
        new Bold(), 
        new Italic(), 
        new FontSize() { Val = 11 }, 
        new Color() { Rgb = new HexBinaryValue() { Value = "000000" } }, 
        new FontName() { Val = "Calibri" }) 
        )); 

     newStyleSheet.Append(new Fills(
       new Fill(                // Index 0 - The default fill. 
        new PatternFill() { PatternType = PatternValues.None }), 
       new Fill(                // Index 1 - The default fill of gray 125 (required) 
        new PatternFill() { PatternType = PatternValues.LightGray }), 
       new Fill(                // Index 2 - The orange fill. 
        new PatternFill(
         new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "FDD5B2" } } 
        ) { PatternType = PatternValues.Solid }), 
       new Fill(                // Index 3 - The row highlight custom colour. 
        new PatternFill(
         new ForegroundColor() { Rgb = new HexBinaryValue() { Value = m_RowHighlightColour } } 
        ) { PatternType = PatternValues.Solid }) 
        )); 

     newStyleSheet.Append(new Borders(
       new Border(               // Index 0 - The default border. 
        new LeftBorder(), 
        new RightBorder(), 
        new TopBorder(), 
        new BottomBorder(), 
        new DiagonalBorder()), 
       new Border(               // Index 1 - Applies a Left, Right, Top, Bottom border to a cell 
        new LeftBorder(
         new Color() { Auto = true } 
        ) { Style = BorderStyleValues.Thin }, 
        new RightBorder(
         new Color() { Auto = true } 
        ) { Style = BorderStyleValues.Thin }, 
        new TopBorder(
         new Color() { Auto = true } 
        ) { Style = BorderStyleValues.Thin }, 
        new BottomBorder(
         new Color() { Auto = true } 
        ) { Style = BorderStyleValues.Thin }, 
        new DiagonalBorder()) 
        )); 


     // Add the numbering formats here 
     uint iExcelIndex = 0; 
     var numberingFormats = new NumberingFormats(); 
     var nformat = new NumberingFormat 
     { 
      NumberFormatId = UInt32Value.FromUInt32(iExcelIndex), 
      FormatCode = StringValue.FromString("£#,##0.00") 
     }; 
     numberingFormats.Append(nformat); 
     newStyleSheet.Append(numberingFormats); 



     newStyleSheet.Append(new CellFormats(

       // Index 0 - The default style. 
       new CellFormat() { FontId = 0, FillId = 0, BorderId = 0, ApplyAlignment = true }, 

       // Index 1 - The header style. 
       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) { FontId = 1, FillId = 2, BorderId = 0, ApplyFont = true, ApplyFill = true }, 

       // Index 2 - The boxed border style. 
       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) { FontId = 0, FillId = 0, BorderId = 1, ApplyBorder = true }, 

       // Index 3 - The default style (with left aligned). 
       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) { FontId = 0, FillId = 0, BorderId = 0, ApplyAlignment = true }, 

       // Index 4 - The footer style. 
       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) { FontId = 2, FillId = 0, BorderId = 0, ApplyFont = true, ApplyAlignment = true }, 

       // Index 5 - The custom highlight style. 
       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) { FontId = 0, FillId = 3, BorderId = 0, ApplyAlignment = true, ApplyFill = true } 
       )); 


     // Return the style sheet 
     return newStyleSheet; 
    } 

지금 시간이에 붙어 있었다 :(어떤 도움이나 포인터는 크게 감사를

+0

당신은 EPPlus (http://epplus.codeplex.com)를 들여다 보았습니까? 많은 것을 쉽게 할 수 있고 상당히 유용 할 것입니다. 예제 파일. –

+0

안녕하세요 John, 답변 해 주셔서 대단히 감사합니다. 불행히도 저는 실제로 현재 클래스에 많은 작업을 넣었습니다. (이미 많은 유용한 보고서를 생성하는 데 이미 사용하고 있습니다.) 그래서 다른 라이브러리를 사용하는 것은 실제로 선택 사항이 아닙니다. 어쨌든 고마워! –

+0

죄송합니다. 지금은 알아 냈습니다. 어떤 이유로 다른 요소보다 먼저 스타일 시트에 NumberingFormats 요소를 추가 한 것 같습니다. 다른 사람이 OpenXML XLSX 생성에 어려움을 겪는다면 SDK 설치 폴더에있는 'OpenXmlSdkTool'도구가 유용하다는 사실을 발견했습니다. –

답변

1

스타일 시트 클래스에 대해 msdn의 순서로 요소를 추가하려고 시도하십시오. 워크 시트에서 예를 들어 cols를 먼저 추가 한 다음 sheetdata를 추가해야합니다. 주문을 변경하면 문서가 열리지 않습니다.