在一系列单元格样式对齐 [英] Cell Style Alignment on a range

查看:158
本文介绍了在一系列单元格样式对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,在Excel工作表fromatting细胞。出于某种原因,我的code似乎正在改变所有单元格的风格时,我只是想改变少数人的风格指定或指定的范围。

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.

下面是一些我现在用的是code:

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?

推荐答案

根据此评论从OP,我发现这个问题。apparentlyworksheet.Cells [Y + 1,X + 1] .Horizo​​ntalAlignment,我相信,真实的解释是,所有细胞开始共享相同风格的对象。所以,如果你改变这种风格的对象,它改变了所有使用它的细胞。但如果你只是直接改变细胞的排列特性,只有细胞受到影响。

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.

这篇关于在一系列单元格样式对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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