C# 范围内的单元格样式对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11529870/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:10:23  来源:igfitidea点击:

Cell Style Alignment on a range

c#excelformatalignment

提问by kschieck

I'm having a problem fromatting cells in an excel sheet. For some reason my code seems to be changing the style of all cells when I just want to change the style of a few specified, or a specified range.

我在处理 Excel 工作表中的单元格时遇到问题。出于某种原因,当我只想更改一些指定或指定范围的样式时,我的代码似乎正在更改所有单元格的样式。

Here's some of the code that I am using:

这是我正在使用的一些代码:

app = new Microsoft.Office.Interop.Excel.Application();
workbook = app.Workbooks.Add(1);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];

//Change all cells' alignment to center
worksheet.Cells.Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

//But then this line changes every cell style back to left alignment
worksheet.Cells[y + 1, x + 2].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;

Why would it change the style of multiple cells when I set it to just work on one? Is it not supposed to work how I want it to? Is there another way of doing this?

当我将它设置为只处理一个单元格时,为什么它会改变多个单元格的样式?它不应该像我想要的那样工作吗?还有另一种方法吗?

采纳答案by DGH

Based on this comment from the OP, "I found the problem. apparentlyworksheet.Cells[y + 1, x + 1].HorizontalAlignment", I believe the real explanation is that all the cells start off sharing the same Style object. So if you change that style object, it changes all the cells that use it. But if you just change the cell's alignment property directly, only that cell is affected.

根据来自 OP 的评论,“我发现了问题。显然是worksheet.Cells[y + 1, x + 1].Horizo​​ntalAlignment”,我相信真正的解释是所有单元格都开始共享相同的 Style 对象。因此,如果您更改该样式对象,它会更改所有使用它的单元格。但是如果你只是直接改变单元格的对齐属性,那么只有那个单元格会受到影响。

回答by crackhaus

Maybe declaring a range might workout better for you.

也许声明一个范围可能更适合你。

// fill in the starting and ending range programmatically this is just an example. 
string startRange = "A1";
string endRange = "A1";
Excel.Range currentRange = (Excel.Range)excelWorksheet.get_Range(startRange , endRange );
currentRange.Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;

回答by Manikandan

This works good

这很好用

worksheet.get_Range("A1","A14").Cells.HorizontalAlignment = 
                 Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;

回答by horgh

Modifying styles directly in range or cells did not work for me. But the idea to:

直接在范围或单元格中修改样式对我不起作用。但想法是:

  1. create a separate style
  2. apply all the necessary style property values
  3. set the style's name to the Styleproperty of the range
  1. 创建一个单独的样式
  2. 应用所有必要的样式属性值
  3. 将样式的名称设置Style为范围的属性

, given in MSDN How to: Programmatically Apply Styles to Ranges in Workbooksdid the job.

,在MSDN How to: Programmatically Apply Styles to Ranges in Workbooks 中给出,完成了这项工作。

For example:

例如:

var range = worksheet.Range[string.Format("A{0}:C{0}", rowIndex++)];
range.Merge();
range.Value = "some value";

var style = workbook.AddStyle();
style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;

range.Style = style.Name;

回答by Reginardo Tribuzi Lula Júnior

  ExcelApp.Sheets[1].Range[ExcelApp.Sheets[1].Cells[1, 1], ExcelApp.Sheets[1].Cells[70, 15]].Cells.HorizontalAlignment =
                 Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

This works fine for me.

这对我来说很好用。

回答by Sheep_Sleep

Don't use "Style:

不要使用“样式:

worksheet.Cells[y,x].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;

回答by Developer

Something that works for me. Enjoy.

对我有用的东西。享受。

Excel.Application excelApplication =  new Excel.Application()  // start excel and turn off msg boxes
{
     DisplayAlerts = false,
     Visible = false
};

Excel.Workbook workBook = excelApplication.Workbooks.Open(targetFile);
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];

var rDT = workSheet.Range(workSheet.Cells[monthYearNameRow, monthYearNameCol], workSheet.Cells[monthYearNameRow, maxTableColumnIndex]);
rDT.Merge();
rDT.Value = monthName + " " + year;
var reportDateRowStyle = workBook.Styles.Add("ReportDateRowStyle");
reportDateRowStyle.HorizontalAlignment = XlHAlign.xlHAlignCenter;
reportDateRowStyle.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
reportDateRowStyle.Font.Bold = true;
reportDateRowStyle.Font.Size = 14;
rDT.Style = reportDateRowStyle;

回答by Ionut Radu

For SpreadSheetGear use workbook.Worksheets[0].Cells["B1:B4"].HorizontalAlignment = HAlign.Center;.This will align all the cells in your sheet from B1 to B4 (column 2- row 1 through 4). Hope this helps all SpreadSheetGear users who run into problems trying to format cells

对于 SpreadSheetGear 使用workbook.Worksheets[0].Cells["B1:B4"].HorizontalAlignment = HAlign.Center;。这将对齐工作表中从 B1 到 B4(第 2 列 - 第 1 行到第 4 行)的所有单元格。希望这可以帮助所有在尝试格式化单元格时遇到问题的 SpreadSheetGear 用户