简体   繁体   English

"使用 OpenXML 和 C# 在 Excel 文档中设置文本居中对齐"

[英]Set text align to center in an Excel document using OpenXML with C#

I have a document that my asp.net page is creating and I need to align the text of certain columns to center.我有一个我的 asp.net 页面正在创建的文档,我需要将某些列的文本居中对齐。 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.在将 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 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. 我认为问题是你正在尝试设置列的样式 ,当它是需要格式化为使用特定水平对齐的单个单元格时。

I looked around and found the following MSDN documentation: 我环顾四周,发现了以下MSDN文档:

http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.cellformat_properties.aspx http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.cellformat_properties.aspx

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): 我在这里也找到了一个代码示例(虽然我自己没有测试过):

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

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. 您应该能够创建单个样式,并在创建单元格时将其多次应用于单元格。

Following approach works fine for me 以下方法对我来说很好

//using OfficeOpenXml;
//using OfficeOpenXml.Style;

workSheet.Cells[rowIndex, 22].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;

REFERENCES 参考

  1. Required basic formatting using open XML SDK 使用开放XML SDK所需的基本格式
  2. Coloring cells in excel sheet using openXML in C# 使用C#中的openXML在excel表中着色单元格
  3. Cell styles in OpenXML spreadsheet (SpreadsheetML) OpenXML电子表格中的单元格样式(SpreadsheetML)

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;

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");

In case you are looking how to aligne text in excel cell, you need to apply the alignement on the cellFormat<\/strong> objects如果您正在寻找如何对齐 excel 单元格中的文本,您需要在cellFormat<\/strong>对象上应用对齐

 CellFormat cellFormat = new CellFormat() { NumberFormatId = 0, FontId = 0, 
 FillId = 0, BorderId = 0, ApplyAlignment = true }; 
 Alignment alignment = new Alignment();
 alignment.Vertical = VerticalAlignmentValues.Center;
 alignment.Horizontal = HorizontalAlignmentValues.Center;
 CellFormat.Alignment = alignment;

Try this 试试这个

workSheet_range.HorizontalAlignment =
     Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM