使用 OpenXML 和 C# 将文本对齐设置为在 Excel 文档中居中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9787669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set text align to center in an Excel document using OpenXML with C#
提问by broguyman
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.
我有一个我的 asp.net 页面正在创建的文档,我需要将某些列的文本居中对齐。我在 openXML SDK 中手动将列居中后打开了文档,但反映的代码没有达到预期的结果。
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:
这就是我设置这些列的自定义宽度的方式,我想向此功能(方法,whatevs)添加将文本居中的功能:
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.
我对另一种方式持开放态度,但我认为解决方案必须非常简单,我似乎无法理解。如果有人可以提供帮助,那就太好了。
NOTE: Please keep in mind that I am using OpenXML and will not be using Microsoft.Office.Interop.Excel
注意:请记住,我使用的是 OpenXML,不会使用 Microsoft.Office.Interop.Excel
采纳答案by Killnine
I think the issue is that you are trying to style the column, when it is individual cells that need to be formattedto use a specific horizontal alignment.
我认为问题在于您正在尝试设置 column 的样式,当需要格式化单个单元格以使用特定的水平对齐方式时。
I looked around and found the following MSDN documentation:
我环顾四周,发现以下 MSDN 文档:
http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.alignment.aspx
http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.alignment.aspx
I also found a code example here (Though I didn't test it myself):
我还在这里找到了一个代码示例(虽然我没有自己测试过):
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.
我自己大部分时间都在使用 Interop,并且知道我在编写电子表格时设置了单元格的样式,而不是列或行的样式。
You should be able to create a single style and just apply it a bunch of times to cells as you create them.
您应该能够创建单个样式,并在创建单元格时将其多次应用于单元格。
回答by LCJ
Following approach works fine for me
以下方法对我来说很好用
//using OfficeOpenXml;
//using OfficeOpenXml.Style;
workSheet.Cells[rowIndex, 22].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
REFERENCES
参考
回答by Himanshu Shukla
Try this
尝试这个
workSheet_range.HorizontalAlignment =
Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
回答by Mehdi Benkirane
you can try this to have a merged cell, edit height and columns and align center horizontally and vertically
您可以尝试合并单元格,编辑高度和列并水平和垂直对齐中心
var worksheet = wb.Worksheets.Add("Overzicht");
// Adding text
worksheet.Cell("B2").Value = "Overzicht activiteit";
var rngMainTitle = worksheet.Range("B2:E3");
rngMainTitle.FirstCell().Style
.Font.SetBold()
.Fill.SetBackgroundColor(XLColor.CornflowerBlue)
.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center)
.Alignment.SetVertical(XLAlignmentVerticalValues.Center);
//Merge title cells
rngMainTitle.FirstRow().Merge();
worksheet.Column(2).Width = 31;
worksheet.Column(3).Width = 18;
worksheet.Column(4).Width = 18;
worksheet.Column(5).Width = 18;
worksheet.Row(2).Height = 25;
回答by Kate
You can just select your cell and set text align:
您只需选择您的单元格并设置文本对齐:
ws.Cell("A1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
ws is your worksheet:
ws 是您的工作表:
XLWorkbook workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Text align");

