使用带有C#的OpenXML设置文本对齐方式使其在Excel文档中居中 [英] Set text align to center in an Excel document using OpenXML with C#
问题描述
我有一个我的asp.net页面正在创建的文档,我需要将某些列的文本居中对齐.在openXML SDK中手动将列居中之后,我已经打开了文档,但是所反映的代码无法达到预期的效果.
I have a document that my asp.net page is creating and I need to align the text of certain columns to center. I have opened the document after manually center the columns in openXML SDK but the code that is reflected does not achieve the desired outcome.
这是我设置这些列的自定义宽度的方式,我想在此功能(方法,Whatevs)中添加使文本居中的功能:
This is how I am setting the custom widths of these columns and I would like to add to this function (method, whatevs) the capability to center the text:
private static Column CreateColumnData(UInt32 StartColumnIndex, UInt32 EndColumnIndex, double ColumnWidth)
{
Column column;
column = new Column();
column.Min = StartColumnIndex;
column.Max = EndColumnIndex;
column.Width = ColumnWidth;
column.CustomWidth = true;
//the SDK says to add this next line to center the text but it doesn't work
column.Style = (UInt32Value)6U;
return column;
}
我愿意接受另一种方式,但是我认为解决方案必须非常简单,我似乎无法理解.如果有人可以帮助,那就太好了.
I'm open to another way but I think that the solution has got to be very simple I just can't seem to get it. If anyone can help that would be great.
注意:请记住,我使用的是OpenXML,不会使用Microsoft.Office.Interop.Excel
推荐答案
我认为问题在于,当需要单独设置单元格时,您正在尝试设置列的样式格式以使用特定的水平对齐方式.
I think the issue is that you are trying to style the column, when it is individual cells that need to be formatted to use a specific horizontal alignment.
我环顾四周,发现以下MSDN文档:
I looked around and found the following MSDN documentation:
http://msdn.microsoft.com/zh-cn/library/documentformat.openxml.spreadsheet.alignment.aspx
我还在这里找到了一个代码示例(尽管我自己没有对其进行测试):
I also found a code example here (Though I didn't test it myself):
我大部分时间都是自己使用Interop,并且知道在编写电子表格时我设置了单元格的样式,而不是列或行的样式.
I use the Interop most of the time myself, and know that I styled cells and not columns or rows when I wrote up my spreadsheets.
您应该能够创建一个样式,并在创建单元格时将其多次应用到单元格上.
You should be able to create a single style and just apply it a bunch of times to cells as you create them.
这篇关于使用带有C#的OpenXML设置文本对齐方式使其在Excel文档中居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!