OpenXML SpreadsheetML横排文本 [英] OpenXML SpreadsheetML Sideways text

查看:139
本文介绍了OpenXML SpreadsheetML横排文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使文本在OpenXML的电子表格单元格中横向打印.我在想可以通过Cell类的ExtendedProperties来完成.这就是我所拥有的.

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

推荐答案

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

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>

该属性代表StyleIndex,它是CellFormats列表中的CellFormat索引,与该单元格的格式相对应.这是CellFormats的XML,希望可以使它更清晰一些:

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>

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

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

现在,要旋转单元格的文本,您将需要通过CellFormat进行控制.这是创建90度旋转CellFormat所需的代码:

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

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

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.

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

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

这篇关于OpenXML SpreadsheetML横排文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆