2012-04-03 7 views
0

Ich versuche herauszufinden, wie man Text in einer Tabellenkalkulationszelle in OpenXML seitwärts drucken kann. Ich denke, dass es irgendwie mit den ExtendedProperties der Zelle Klasse getan werden kann. hier ist, was ich habe.OpenXML SpreadsheetML Sideways Text

Cell cell = GetCell(worksheetPart.Worksheet, columnName, rowIndex); 
    cell.DataType = CellValues.InlineString; 
    cell.InlineString = new InlineString() { Text = new Text(textToInsert) }; 

    //fictitious method 
    cell.ExtendedAttributes.SpinSideways(); 
    worksheetPart.Worksheet.Save() 
+0

Duplizieren von http: // Stackoverflow .com/Fragen/605966 6/antwort/sende? S = 9219459d-a516-4000-9913-d6d5843fecb7? – emd

Antwort

3

Styles für Zellen im CellFormats Abschnitt eines Excel-Dokument behandelt werden. Man kann sagen, wenn eine Zelle ein Format hat, wenn Sie im XML schauen und sehen, dass das s Attribut auf eine ganze Zahl gesetzt ist:

<x:c r="A1" s="1" t="s" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> 
    <x:v>0</x:v> 
</x:c> 

Das Attribut für die StyleIndex steht, die der CellFormat Index in der CellFormats Liste ist, dass entspricht der Formatierung für diese Zelle. Hier ist der XML der CellFormats hoffentlich macht es ein wenig klarer:

<x:cellXfs count="2" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> 
    <x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" /> 
    <x:xf numFmtId="0" fontId="0" fillId="1" borderId="1" xfId="0" /> 
</x:cellXfs> 

In dem obigen XML haben wir das x:cellXfs Element, das ist das CellFormats Element und es hat zwei Kinder von x:xf oder CellFormat Elementen. Wir wissen aus der StyleIndex Attribut, das wir den ersten Index (oder ein zweites Element) unter dem CellFormats Element wollen, das heißt, wir wollen, dass diese CellFormat:

<x:xf numFmtId="0" fontId="0" fillId="1" borderId="1" xfId="0" /> 

Um nun eine Zelle Text zu haben, gedreht werden, müssen Sie Steuern Sie das über eine CellFormat. Hier ist der Code müssen Sie verwenden, um eine CellFormat mit 90-Grad-Drehung zu schaffen:

public CellFormat GenerateCellFormat() 
{ 
    CellFormat cellFormat1 = new CellFormat(){ NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyAlignment = true }; 
    Alignment alignment1 = new Alignment(){ TextRotation = (UInt32Value)90U }; 

    cellFormat1.Append(alignment1); 
    return cellFormat1; 
} 

Wenn Sie den Text wollen in einem anderen Winkel drehen, dann ersetzen Sie einfach den 90U mit dem, was Winkel Sie wollen - 90 bis 90.

Jetzt müssen Sie einfügen, dass CellFormat in die CellFormats und dann festgelegt, dass neue Index auf der StyleIndex der Zelle, die Sie gedreht haben wollen:

Cell cell = GetCell(worksheetPart.Worksheet, columnName, rowIndex); 
cell.StyleIndex = InsertCellFormat(workbookPart, GenerateCellFormat()); 

// Helper method to insert the cell format in the CellFormats 
public static uint InsertCellFormat(WorkbookPart workbookPart, CellFormat cellFormat) 
{ 
    CellFormats cellFormats = workbookPart.WorkbookStylesPart.Stylesheet.Elements<CellFormats>().First(); 
    cellFormats.Append(cellFormat); 
    return (uint)cellFormats.Count++; 
} 
+0

Ich erhalte eine Fehlermeldung, dass die Elemente Sequence keine Elemente enthält. [email protected] – Kulingar

+0

Dies bedeutet, dass das CellFormats-Element nicht existiert und Sie es hinzufügen müssen oder eine Excel-Datei verwenden müssen, für die bereits ein CellFormat definiert ist. – amurra

+0

Wie füge ich es hinzu? :( – Kulingar

1

Zuerst müssen Sie ein Stylesheet erstellen und dann müssen Sie es auf die Zelle anwenden.

Einige wichtige snippits:

Für das Stylesheet müssen Sie enthalten:

Alignment align = new Alignment(); 
align.TextRotation.Value = 90; 
CellFormat format = CellFormat(){Alignment= align}; 

Dann ist es auf die Zelle anwenden

cell.StyleIndex=INDEXOFYOURSTYLE;

Ressourcen:

Styling eine Kalkulationstabelle: http://blogs.msdn.com/b/chrisquon/archive/2009/11/30/stylizing-your-excel-worksheets-with-open-xml-2-0.aspx

MSDN- Ausrichtung: http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.alignment.aspx