首页 文章

OpenXML SpreadsheetML Sideways文本

提问于
浏览
1

我试图弄清楚如何在OpenXML中的电子表格单元格中侧面打印文本 . 我认为它可以通过Cell类的ExtendedProperties以某种方式完成 . 这就是我所拥有的 .

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

2 回答

  • 1

    单元格的样式在Excel文档的 CellFormats 部分中处理 . 当您查看XML并查看 s 属性设置为整数时,您可以判断单元格何时具有格式:

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

    该属性代表 StyleIndex ,它是 CellFormats 列表中的 CellFormat 索引,对应于此单元格的格式 . 这是 CellFormats 的XML,希望能让它更清晰一些:

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

    在上面的XML中,我们有 x:cellXfs 元素,它是 CellFormats 元素,它有两个子元素 x:xfCellFormat 元素 . 我们从 StyleIndex 属性知道我们想要 CellFormats 元素下的第一个索引(或第二个元素),这意味着我们想要这个 CellFormat

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

    现在,为了让单元格的文本被旋转,您需要通过 CellFormat 来控制它 . 以下是为了创建90度旋转的CellFormat而需要使用的代码:

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

    如果您希望文本以不同的角度旋转,那么只需将90U替换为您想要的任何角度-90到90 .

    现在,您需要将 CellFormat 插入 CellFormats ,然后在要旋转的单元格的 StyleIndex 上设置新索引:

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

    首先,您需要创建样式表,然后需要将其应用于单元格 .

    一些重要的嗅觉:

    对于样式表,您需要包括:

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

    然后将其应用于细胞

    cell.StyleIndex=INDEXOFYOURSTYLE;

    资源:

    样式化电子表格:http://blogs.msdn.com/b/chrisquon/archive/2009/11/30/stylizing-your-excel-worksheets-with-open-xml-2-0.aspx

    MSDN-对齐方式:http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.alignment.aspx

相关问题