OpenXML SpreadsheetML Sideways text

你离开我真会死。 提交于 2019-12-11 07:28:37

问题


I'm trying to figure out how to get text to print sideways in a spreadsheet cell in OpenXML. I'm thinking it can be done somehow with the ExtendedProperties of the Cell class. here's what I've got.

  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()

回答1:


Styles for cells are handled in the CellFormats section of an Excel document. You can tell when a cell has a format when you look at the XML and see that the s attribute is set to an integer:

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

That attribute stands for the StyleIndex which is the CellFormat index in the CellFormats list that corresponds to the formatting for this cell. Here is the XML of the CellFormats to hopefully make it a little clearer:

<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 the above XML we have the x:cellXfs element which is the CellFormats element and it has two children of x:xf or CellFormat elements. We know from the StyleIndex attribute that we want the first index (or second element) under the CellFormats element, which means we want this CellFormat:

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

Now in order to have a cell's text be rotated you will need to control that via a CellFormat. Here is the code you will need to use in order to create a CellFormat with 90 degree rotation:

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;
}

If you want the text to rotate at a different angle then just replace the 90U with whatever angle you want from -90 to 90.

Now you will need to insert that CellFormat into the CellFormats and then set that new index on the StyleIndex of the cell you want to have rotated:

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++;
}



回答2:


First you need to create a style sheet and then you need to apply it to the cell.

Some important snippits:

For the stylesheet you will need to include:

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

Then apply it to the cell

cell.StyleIndex=INDEXOFYOURSTYLE;

Resources:

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

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



来源:https://stackoverflow.com/questions/9998122/openxml-spreadsheetml-sideways-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!