如何让我的包装列标题水平对齐?
How can I get my wrapped column headings to horizontally align?
我正在 Excel 电子表格上创建一个 "column header row",最终看起来部分像这样:
请注意,跨越多个 "lines" 的列标题在其单元格中未水平对齐。最后一个"line"好像是well-centered,但是那些before/above是off-center。例如,对于 "Member Item Code","Member" 是 way 太靠左了,而 "Item" 也太靠左了(但不是这样) , 而 "Code" 看起来很完美。但是我的代码显然不是很完美。
我 am 将 "center" 的水平对齐应用于这些单元格:
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
,单词之间直接用space将列内容换行,根据列宽分多行显示.
在下面的代码中,我将范围内的所有单元格设置为换行并垂直和水平居中。
同样为了测试我设置了第 5 列的列宽。
//Using
using XL = Microsoft.Office.Interop.Excel;
//Code
XL.Application application = new XL.Application();
application.Visible = true;
var book = application.Workbooks.Add();
var sheet = book.Worksheets[1];
sheet.Cells[1, 1].Value2 = "Unit";
sheet.Cells[1, 2].Value2 = "Customer Number";
sheet.Cells[1, 3].Value2 = "Customer Name";
sheet.Cells[1, 4].Value2 = "Member Item Code";
sheet.Cells[1, 5].Value2 = "Qty";
sheet.Cells[1, 5].ColumnWidth = 30;
sheet.Cells[1, 6].Value2 = "Bad Price";
var range = sheet.Range("A1", "F1");
range.VerticalAlignment = XL.XlVAlign.xlVAlignCenter;
range.HorizontalAlignment = XL.XlHAlign.xlHAlignCenter;
range.WrapText = true;
这里是默认字体设置的结果:
这是使用自定义字体设置的结果:
range.Font.Name = "Tahoma";
range.Font.Bold = true;
range.Font.Size = 9;
我正在 Excel 电子表格上创建一个 "column header row",最终看起来部分像这样:
请注意,跨越多个 "lines" 的列标题在其单元格中未水平对齐。最后一个"line"好像是well-centered,但是那些before/above是off-center。例如,对于 "Member Item Code","Member" 是 way 太靠左了,而 "Item" 也太靠左了(但不是这样) , 而 "Code" 看起来很完美。但是我的代码显然不是很完美。
我 am 将 "center" 的水平对齐应用于这些单元格:
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
,单词之间直接用space将列内容换行,根据列宽分多行显示.
在下面的代码中,我将范围内的所有单元格设置为换行并垂直和水平居中。
同样为了测试我设置了第 5 列的列宽。
//Using
using XL = Microsoft.Office.Interop.Excel;
//Code
XL.Application application = new XL.Application();
application.Visible = true;
var book = application.Workbooks.Add();
var sheet = book.Worksheets[1];
sheet.Cells[1, 1].Value2 = "Unit";
sheet.Cells[1, 2].Value2 = "Customer Number";
sheet.Cells[1, 3].Value2 = "Customer Name";
sheet.Cells[1, 4].Value2 = "Member Item Code";
sheet.Cells[1, 5].Value2 = "Qty";
sheet.Cells[1, 5].ColumnWidth = 30;
sheet.Cells[1, 6].Value2 = "Bad Price";
var range = sheet.Range("A1", "F1");
range.VerticalAlignment = XL.XlVAlign.xlVAlignCenter;
range.HorizontalAlignment = XL.XlHAlign.xlHAlignCenter;
range.WrapText = true;
这里是默认字体设置的结果:
这是使用自定义字体设置的结果:
range.Font.Name = "Tahoma";
range.Font.Bold = true;
range.Font.Size = 9;