【发布时间】:2016-03-25 10:13:15
【问题描述】:
我正在 Excel 电子表格上创建一个“列标题行”,最终看起来像这样:
请注意,跨越多条“行”的列标题在其单元格内是如何不水平对齐的。最后一条“线”似乎很好地居中,但它之前/之上的那些是偏离中心的。例如,对于“Member Item Code”,“Member”way 离左边太远,“Item”也离左边太远(但不太严重),而“Code”看起来很完美.但是我的代码显然不是很完美。
我am将“中心”的水平对齐应用到这些单元格:
var unitHeaderCell = (Excel.Range)_xlSheet.Cells[rowToPopulate, DETAIL_UNIT_COL];
unitHeaderCell.Value2 = "Unit";
var custNumHeaderCell = (Excel.Range)_xlSheet.Cells[rowToPopulate, DETAIL_CUSTNUM_COL];
custNumHeaderCell.Value2 = String.Format("Customer{0}Number", Environment.NewLine);
custNumHeaderCell.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
custNumHeaderCell.WrapText = true;
var custNameHeaderCell = (Excel.Range)_xlSheet.Cells[rowToPopulate, DETAIL_CUSTNAME_COL];
custNameHeaderCell.Value2 = String.Format("Customer{0}Name", Environment.NewLine);
custNameHeaderCell.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
custNameHeaderCell.WrapText = true;
var memberItemCodeHeaderCell = (Excel.Range)_xlSheet.Cells[rowToPopulate, DETAIL_MEMBERITEMCODE_COL];
memberItemCodeHeaderCell.Value2 = String.Format("Member{0}Item{0}Code", Environment.NewLine);
memberItemCodeHeaderCell.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
memberItemCodeHeaderCell.WrapText = true;
memberItemCodeHeaderCell.ColumnWidth = DETAIL_MEMBERITEMCODE_COL_WIDTH;
var qtyHeaderCell = (Excel.Range)_xlSheet.Cells[rowToPopulate, DETAIL_QTY_COL];
qtyHeaderCell.Value2 = "Qty";
var bidPriceHeaderCell = (Excel.Range)_xlSheet.Cells[rowToPopulate, DETAIL_BIDPRICE_COL];
bidPriceHeaderCell.Value2 = "Bid Price";
我必须怎么做才能使这些词的所有居中(不仅仅是最后一个词)?
【问题讨论】:
-
字与字之间不用加
Environment.NewLine,字与字之间可以用空格包裹列内容,根据列宽分多行显示。 -
但它会居中吗?如果没有,那对我没有任何好处。实际上,我现在正在测试它,因为我们输入...不,没有工作 - 它不会换行文本,即使我将 WrapText 设置为 true。
-
是的,它将使用
tahoma和calibri字体居中。但是当您使用Environment.NewLine时,它只使用calibri而不是tahoma -
是的,它居中(我使用的是 Tahoma),但它不换行,因此使列太宽。
-
我发布了我测试的代码。让我知道结果。
标签: c# centering excel-interop