2017-04-19 1 views
0

내 .xlsx 파일에서 "1,000.00"과 같은 10 진수 및 정수를 포맷하려고합니다.

발생 스타일의 코드 :C# OpenXML : 숫자 형식이 적용되지 않습니다.

private Stylesheet GenerateStylesheet() 
{ 
    //styling and formatting 
    var cellFormats = new CellFormats(); 
    uint iExcelIndex = 164; 

    //number formats 
    var numericFormats = new NumberingFormats(); 
    var nformat4Decimal = new NumberingFormat 
    { 
     NumberFormatId = UInt32Value.FromUInt32(iExcelIndex++), 
     FormatCode = StringValue.FromString("#,##0.00") 
    }; 
    numericFormats.Append(nformat4Decimal);  

    //cell formats 
    var cellFormat = new CellFormat 
    { 
     NumberFormatId = nformat4Decimal.NumberFormatId, 
     FontId = 0, 
     FillId = 0, 
     BorderId = 0, 
     FormatId = 0, 
     ApplyNumberFormat = BooleanValue.FromBoolean(true) 
    }; 
    cellFormats.Append(cellFormat); 

    numericFormats.Count = UInt32Value.FromUInt32((uint)numericFormats.ChildElements.Count); 
    cellFormats.Count = UInt32Value.FromUInt32((uint)cellFormats.ChildElements.Count); 

    var stylesheet = new Stylesheet(); 
    stylesheet.Append(numericFormats); 

    return stylesheet; 
} 

다음은 문서에 스타일 시트를 추가하는 코드 :

WorkbookStylesPart stylesPart = workbookpart.AddNewPart<WorkbookStylesPart>(); 
stylesPart.Stylesheet = GenerateStylesheet(); 
stylesPart.Stylesheet.Save(); 

그리고 이것은 세포를 생성하는 방법이다

var numberCell = new Cell 
{ 
    DataType = CellValues.Number, 
    CellReference = header + index, 
    CellValue = new CellValue(text), 
    StyleIndex = 0 
}; 

비아의 OpenXML 생산성 도구 나는 그 숫자 스타일이 거기에 있고 셀에 "적용된"것을 볼 수있다. 세포에서 생성 문서 값을 열 때 예상

enter image description here
enter image description here

그러나 포맷되지 않는다.

또한 #,##0.00은 ID가 4 인 기본 Excel 형식 중 하나임을 발견했습니다. 그러나 NumberFormatId = nformat4Decimal.NumberFormatIdNumberFormatId = 4으로 변경해도 아무런 영향이 없습니다.

답변

1

숫자 형식 만 지정할 수는 없습니다. 글꼴, 채우기, 테두리를 지정해야합니다. 해당 숫자 형식을 수행 한 후 finnaly가 적용되었습니다. 이와 끝까지 :

private static Stylesheet GenerateStylesheet2() 
{ 
    Stylesheet ss = new Stylesheet(); 

    Fonts fts = new Fonts(); 
    DocumentFormat.OpenXml.Spreadsheet.Font ft = new DocumentFormat.OpenXml.Spreadsheet.Font(); 
    FontName ftn = new FontName(); 
    ftn.Val = "Calibri"; 
    FontSize ftsz = new FontSize(); 
    ftsz.Val = 11; 
    ft.FontName = ftn; 
    ft.FontSize = ftsz; 
    fts.Append(ft); 
    fts.Count = (uint)fts.ChildElements.Count; 

    Fills fills = new Fills(); 
    Fill fill; 
    PatternFill patternFill; 
    fill = new Fill(); 
    patternFill = new PatternFill(); 
    patternFill.PatternType = PatternValues.None; 
    fill.PatternFill = patternFill; 
    fills.Append(fill); 
    fill = new Fill(); 
    patternFill = new PatternFill(); 
    patternFill.PatternType = PatternValues.Gray125; 
    fill.PatternFill = patternFill; 
    fills.Append(fill); 
    fills.Count = (uint)fills.ChildElements.Count; 

    Borders borders = new Borders(); 
    Border border = new Border(); 
    border.LeftBorder = new LeftBorder(); 
    border.RightBorder = new RightBorder(); 
    border.TopBorder = new TopBorder(); 
    border.BottomBorder = new BottomBorder(); 
    border.DiagonalBorder = new DiagonalBorder(); 
    borders.Append(border); 
    borders.Count = (uint)borders.ChildElements.Count; 

    CellStyleFormats csfs = new CellStyleFormats(); 
    CellFormat cf = new CellFormat(); 
    cf.NumberFormatId = 0; 
    cf.FontId = 0; 
    cf.FillId = 0; 
    cf.BorderId = 0; 
    csfs.Append(cf); 
    csfs.Count = (uint)csfs.ChildElements.Count; 

    uint iExcelIndex = 164; 
    NumberingFormats nfs = new NumberingFormats(); 
    CellFormats cfs = new CellFormats(); 

    cf = new CellFormat(); 
    cf.NumberFormatId = 0; 
    cf.FontId = 0; 
    cf.FillId = 0; 
    cf.BorderId = 0; 
    cf.FormatId = 0; 
    cfs.Append(cf); 

    NumberingFormat nf; 
    nf = new NumberingFormat(); 
    nf.NumberFormatId = iExcelIndex++; 
    nf.FormatCode = "dd/mm/yyyy hh:mm:ss"; 
    nfs.Append(nf); 
    cf = new CellFormat(); 
    cf.NumberFormatId = nf.NumberFormatId; 
    cf.FontId = 0; 
    cf.FillId = 0; 
    cf.BorderId = 0; 
    cf.FormatId = 0; 
    cf.ApplyNumberFormat = true; 
    cfs.Append(cf); 

    nf = new NumberingFormat(); 
    nf.NumberFormatId = iExcelIndex++; 
    nf.FormatCode = "#,##0.0000"; 
    nfs.Append(nf); 
    cf = new CellFormat(); 
    cf.NumberFormatId = nf.NumberFormatId; 
    cf.FontId = 0; 
    cf.FillId = 0; 
    cf.BorderId = 0; 
    cf.FormatId = 0; 
    cf.ApplyNumberFormat = true; 
    cfs.Append(cf); 

    // #,##0.00 is also Excel style index 4 
    nf = new NumberingFormat(); 
    nf.NumberFormatId = iExcelIndex++; 
    nf.FormatCode = "#,##0.00"; 
    nfs.Append(nf); 
    cf = new CellFormat(); 
    cf.NumberFormatId = nf.NumberFormatId; 
    cf.FontId = 0; 
    cf.FillId = 0; 
    cf.BorderId = 0; 
    cf.FormatId = 0; 
    cf.ApplyNumberFormat = true; 
    cfs.Append(cf); 

    // @ is also Excel style index 49 
    nf = new NumberingFormat(); 
    nf.NumberFormatId = iExcelIndex++; 
    nf.FormatCode = "@"; 
    nfs.Append(nf); 
    cf = new CellFormat(); 
    cf.NumberFormatId = nf.NumberFormatId; 
    cf.FontId = 0; 
    cf.FillId = 0; 
    cf.BorderId = 0; 
    cf.FormatId = 0; 
    cf.ApplyNumberFormat = true; 
    cfs.Append(cf); 

    nfs.Count = (uint)nfs.ChildElements.Count; 
    cfs.Count = (uint)cfs.ChildElements.Count; 

    ss.Append(nfs); 
    ss.Append(fts); 
    ss.Append(fills); 
    ss.Append(borders); 
    ss.Append(csfs); 
    ss.Append(cfs); 

    CellStyles css = new CellStyles(); 
    CellStyle cs = new CellStyle(); 
    cs.Name = "Normal"; 
    cs.FormatId = 0; 
    cs.BuiltinId = 0; 
    css.Append(cs); 
    css.Count = (uint)css.ChildElements.Count; 
    ss.Append(css); 

    DifferentialFormats dfs = new DifferentialFormats(); 
    dfs.Count = 0; 
    ss.Append(dfs); 

    TableStyles tss = new TableStyles(); 
    tss.Count = 0; 
    tss.DefaultTableStyle = "TableStyleMedium9"; 
    tss.DefaultPivotStyle = "PivotStyleLight16"; 
    ss.Append(tss); 

    return ss; 
} 

을 그리고 세포 생성을위한 :

var numberCell = new Cell 
{ 
    DataType = CellValues.Number, 
    CellReference = header + index, 
    CellValue = new CellValue(text), 
    StyleIndex = 3 
}; 

스타일 지수 3는 숫자 형식 "#,##0.00"와 셀 스타일을 참조한다.

관련 문제