为什么将单元格内容设置为左alignment(Aspose Cells)?

我有这个代码,它应该左alignment特定列的内容:

Cell memberItemCodeCell = customerWorksheet.Cells[rowToPopulate, MEMBERITEMCODE_COL]; memberItemCodeCell.PutValue(frbdbc.MemberItemCode, true); var micStyle = memberItemCodeCell.GetStyle(); micStyle.Font.Name = fontForSheets; micStyle.Font.Size = 11; micStyle.HorizontalAlignment = TextAlignmentType.Left; micStyle.IsTextWrapped = false; memberItemCodeCell.SetStyle(micStyle, flag); 

它有效…有时:

在这里输入图像说明

为什么有时会发生右alignment呢?

任何可以被视为int的值(不包含字母字符或破折号)右alignment; 但为什么它不尊重明确的左alignment,无论它是否看起来像一个int?

在所有列特定的代码之后,有一些适用于整行的“常规”格式:

 CellsFactory cf = new CellsFactory(); Style style4 = cf.CreateStyle(); if (shipVarDbl >= 1.0) // fewer were shipped than were ordered { style4.ForegroundColor = Color.LightGreen; } else if (shipVarDbl < 0.0) // more were shipped than were ordered { style4.ForegroundColor = Color.PaleVioletRed; } style4.Font.Name = fontForSheets; style4.Font.Size = 11; style4.Pattern = BackgroundType.Solid; rowRange.SetStyle(style4); 

…但这不应该影响alignment。

上面首先显示的代码作为PopulateCustomerSheet()方法的一部分运行后:

 private void PopulateCustomerSheet() { try { if (null == _fillRateByDistributorByCustomerList) return; foreach (FillRateByDistributorByCustomer frbdbc in _fillRateByDistributorByCustomerList) { AddCustomerRow(frbdbc); } AutoFitterOptions options = new AutoFitterOptions { OnlyAuto = true }; customerWorksheet.AutoFitColumns(options); } catch (Exception ex) { RoboReporterConstsAndUtils.HandleException(ex); } } 

…它是borderized,configuration为打印,最后该表被写入磁盘:

 BorderizeDataPortionOfCustomerSheet(); ConfigureCustomerSheetForPrinting(); // Write the file to disk string fromAsYYYYMMDD = DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss"); RoboReporterConstsAndUtils.SetUniqueFolder(_unit); String _uniqueFolder = RoboReporterConstsAndUtils.uniqueFolder; var sharedFolder = String.Format(@"\\storageblade\cs\REPORTING\RoboReporter\{0}", _uniqueFolder); RoboReporterConstsAndUtils.ConditionallyCreateDirectory(sharedFolder); var filename = String.Format(@"{0}\{1} - Fill Rate - {2}.xlsx", sharedFolder, _unit, fromAsYYYYMMDD); if (File.Exists(filename)) { File.Delete(filename); } workBook.Save(filename, SaveFormat.Xlsx); 

我无法想象borderizing或打印configuration更改表格上的特定列的alignment方式,但以防万一这是可能的,这里是这些方法:

 private void BorderizeDataPortionOfCustomerSheet() { int rowsUsed = customerWorksheet.Cells.Rows.Count; int colsUsed = SHIPVARIANCE_COL; string bottomRightRange = string.Format("P{0}", rowsUsed); var range = customerWorksheet.Cells.CreateRange("A1", bottomRightRange); //Setting border for each cell in the range var style = workBook.CreateStyle(); style.SetBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black); style.SetBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black); style.SetBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black); style.SetBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black); for (int r = range.FirstRow; r < range.RowCount; r++) { for (int c = range.FirstColumn; c < range.ColumnCount; c++) { Cell cell = customerWorksheet.Cells[r, c]; cell.SetStyle(style, new StyleFlag() { TopBorder = true, BottomBorder = true, LeftBorder = true, RightBorder = true }); } } //Setting outline border to range range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black); range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black); range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black); range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black); customerWorksheet.FreezePanes(FIRST_DATA_ROW, SHORTNAME_COL, rowsUsed, colsUsed); } private void ConfigureCustomerSheetForPrinting() { const double INCHES_TO_CENTIMETERS_FACTOR = 2.54; string lastColumn = GetExcelTextColumnName(customerWorksheet.Cells.Columns.Count); string printArea = String.Format("A1:{0}{1}", lastColumn, customerWorksheet.Cells.Rows.Count); customerWorksheet.PageSetup.PrintArea = printArea; customerWorksheet.PageSetup.Orientation = PageOrientationType.Landscape; // I don't know if this does anything; I would like to set it to 54%... customerWorksheet.PageSetup.IsPercentScale = true; customerWorksheet.PageSetup.FitToPagesWide = 1; customerWorksheet.PageSetup.FitToPagesTall = 0; customerWorksheet.PageSetup.LeftMargin = 0.5 * INCHES_TO_CENTIMETERS_FACTOR; customerWorksheet.PageSetup.RightMargin = 0.5 * INCHES_TO_CENTIMETERS_FACTOR; customerWorksheet.PageSetup.TopMargin = 0.5 * INCHES_TO_CENTIMETERS_FACTOR; customerWorksheet.PageSetup.BottomMargin = 0.5 * INCHES_TO_CENTIMETERS_FACTOR; customerWorksheet.PageSetup.HeaderMargin = 0; customerWorksheet.PageSetup.FooterMargin = 0; // Repeat rows string repeatableRowRange = "$1:$1"; customerWorksheet.PageSetup.PrintTitleRows = repeatableRowRange; } 

这怎么可能导致列alignment不尊重,违反,并忽略明确的和现在的指令来左alignment其内容?

UPDATE

基于我所理解的答案,我改变了这个:

 rowRange.SetStyle(style4); 

对此:

 var flag = new StyleFlag { CellShading = true, FontName = true, FontSize = true, FontColor = true, FontBold = true, NumberFormat = true }; rowRange.ApplyStyle(style4, flag); 

…但没有区别。

更新2

这是明确设置问题列的代码:

 Cell memberItemCodeCell = customerWorksheet.Cells[rowToPopulate, MEMBERITEMCODE_COL]; memberItemCodeCell.PutValue(frbdbc.MemberItemCode, true); var micStyle = memberItemCodeCell.GetStyle(); micStyle.Font.Name = fontForSheets; micStyle.Font.Size = 11; micStyle.HorizontalAlignment = TextAlignmentType.Left; micStyle.IsTextWrapped = false; memberItemCodeCell.SetStyle(micStyle, flag); 

所以我没有在这里使用范围,而是一个单元格。 我应该使用范围,以便我可以使用ApplyStyle()? 我试过这样做,但似乎并不想接受PutValue()等。

此外,提供的值(frbdbc.MemberItemCode)是一个string,所以不应该这样防止Excel将它视为一个int? 我还得做些什么来让Excel知道:“嘿,这是一个string,只是按照原样呈现”。

更新3

我试过这个由Aspose寄给我的代码:

 customerWorksheet.Cells.CreateRange(rangeBegin, rangeEnd).ApplyStyle(style4, new StyleFlag() { Font = true, CellShading = true }); 

在上下文中:

 string rangeBegin = RoboReporterConstsAndUtils.GetRangeLettersNumbersAsStr(SHORTNAME_COL + 1, rowToPopulate); string rangeEnd = RoboReporterConstsAndUtils.GetRangeLettersNumbersAsStr(SHIPVARIANCE_COL + 1, rowToPopulate); CellsFactory cf = new CellsFactory(); Style style4 = cf.CreateStyle(); if (shipVarDbl >= 1.0) // fewer were shipped than were ordered { style4.ForegroundColor = Color.LightGreen; } else if (shipVarDbl < 0.0) // more were shipped than were ordered { style4.ForegroundColor = Color.PaleVioletRed; } style4.Font.Name = fontForSheets; style4.Font.Size = 11; style4.Pattern = BackgroundType.Solid; customerWorksheet.Cells.CreateRange(rangeBegin, rangeEnd).ApplyStyle(style4, new StyleFlag() { Font = true, CellShading = true }); 

…并没有什么区别。

更新4

我认为这可能工作,我发现在这里 :

 micStyle.NumberFormat = 2; 

(用任何数字代表“文本”replace“2”); 但“NumberFormat”不被识别。 这是一个过时的例子吗?

更新5

好吧,为什么这是不可能发生的。 以下是我对该专栏的操作:

首先,标题行被写入:

 private static readonly int MEMBERITEMCODE_COL = 4; . . . private void AddCustomerSheetHeaderRow() { var flag = new StyleFlag { CellShading = true, FontName = true, FontSize = true, FontColor = true, FontBold = true, NumberFormat = true }; . . . CellsFactory cfMemberItemCode = new CellsFactory(); Cell MemberItemCodeCell = customerWorksheet.Cells[0, MEMBERITEMCODE_COL]; MemberItemCodeCell.PutValue("Member Item Code"); var styleMemberItemCode = cfMemberItemCode.CreateStyle(); styleMemberItemCode.HorizontalAlignment = TextAlignmentType.Left; styleMemberItemCode.Font.Name = fontForSheets; styleMemberItemCode.Font.IsBold = true; styleMemberItemCode.Font.Size = 11; styleMemberItemCode.ForegroundColor = Color.LightBlue; styleMemberItemCode.Pattern = BackgroundType.Solid; MemberItemCodeCell.SetStyle(styleMemberItemCode, flag); . . . } 

所以我alignment标题行的列; 它是索引4,IOW列“E”

接下来,多次调用AddCustomerRow(),填充工作表的“数据”部分(标题行下面的所有内容):

  private void AddCustomerRow(FillRateByDistributorByCustomer frbdbc) { var flag = new StyleFlag { CellShading = true, FontName = true, FontSize = true, FontColor = true, FontBold = true, NumberFormat = true }; . . . // This is sometimes seen as an int by Excel, and sports the green warning triangle // Fixed that with the second (true) arg to PutValue(), but it right-aligns int-like vals...?!@? Cell memberItemCodeCell = customerWorksheet.Cells[rowToPopulate, MEMBERITEMCODE_COL]; memberItemCodeCell.PutValue(frbdbc.MemberItemCode, true); var micStyle = memberItemCodeCell.GetStyle(); micStyle.Font.Name = fontForSheets; micStyle.Font.Size = 11; micStyle.HorizontalAlignment = TextAlignmentType.Left; micStyle.IsTextWrapped = false; memberItemCodeCell.SetStyle(micStyle, flag); . . . string rangeBegin = RoboReporterConstsAndUtils.GetRangeLettersNumbersAsStr(SHORTNAME_COL + 1, rowToPopulate); string rangeEnd = RoboReporterConstsAndUtils.GetRangeLettersNumbersAsStr(SHIPVARIANCE_COL + 1, rowToPopulate); CellsFactory cf = new CellsFactory(); Style style4 = cf.CreateStyle(); if (shipVarDbl >= 1.0) // fewer were shipped than were ordered { style4.ForegroundColor = Color.LightGreen; } else if (shipVarDbl < 0.0) // more were shipped than were ordered { style4.ForegroundColor = Color.PaleVioletRed; } style4.Pattern = BackgroundType.Solid; style4.Font.Name = fontForSheets; style4.Font.Size = 11; customerWorksheet.Cells.CreateRange(rangeBegin |rangeEnd).ApplyStyle(style4, new StyleFlag() { Font = true, CellShading = true }); } 

在这里,alignment方式也设置为左侧。 设置每一列后,将创build“通用行样式”,以有条件地对整行进行着色。 这工作正常 – 适当的行是彩色的。

然后,因为标题行的着色不能像以前那样工作(将其设置为LightBlue不起作用),所以我会这样做:

 private void RecolorizeTopRowOfCustomerSheet() { . . . CellsFactory cfMemberItemCode = new CellsFactory(); Cell MemberItemCodeCell = customerWorksheet.Cells[0, MEMBERITEMCODE_COL]; var styleMemberItemCode = cfMemberItemCode.CreateStyle(); styleMemberItemCode.HorizontalAlignment = TextAlignmentType.Left; styleMemberItemCode.Font.Name = fontForSheets; styleMemberItemCode.Font.IsBold = true; styleMemberItemCode.Font.Size = 11; styleMemberItemCode.ForegroundColor = Color.LightBlue; styleMemberItemCode.Pattern = BackgroundType.Solid; MemberItemCodeCell.SetStyle(styleMemberItemCode); . . . // Give it borders Range _range; _range = customerWorksheet.Cells.CreateRange("A1", "P1"); //Set the borders with hair lines style. _range.SetOutlineBorders(CellBorderType.Hair, Color.Black); } 

…边界也被添加到该行。 最后,将边框添加到数据行(第1行之后的所有内容):

 private void BorderizeDataPortionOfCustomerSheet() { int rowsUsed = customerWorksheet.Cells.Rows.Count; int colsUsed = SHIPVARIANCE_COL; string bottomRightRange = string.Format("P{0}", rowsUsed); var range = customerWorksheet.Cells.CreateRange("A1", bottomRightRange); //Setting border for each cell in the range var style = workBook.CreateStyle(); style.SetBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black); style.SetBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black); style.SetBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black); style.SetBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black); for (int r = range.FirstRow; r < range.RowCount; r++) { for (int c = range.FirstColumn; c < range.ColumnCount; c++) { Cell cell = customerWorksheet.Cells[r, c]; cell.SetStyle(style, new StyleFlag() { TopBorder = true, BottomBorder = true, LeftBorder = true, RightBorder = true }); } } //Setting outline border to range range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black); range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black); range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black); range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black); customerWorksheet.FreezePanes(FIRST_DATA_ROW, SHORTNAME_COL, rowsUsed, colsUsed); } 

然后文件被保存到磁盘 – 在MemberItemCode列左alignment和其他右alignment一些项目杂乱无章。 为什么没有像这样的电话:

 Column[3].Alignment = AlignLeft; 

…可以根据需要轻松alignment特定列中的所有内容; 还是有? 既然我所尝试的都不起作用,或者是非常激动的,那么这样的东西肯定会得心应手。

我相信所说的问题是由于你用从头开始创build的对象覆盖了样式的所有方面而引起的。 请注意,当您将样式应用于单元格时,您首先会从该单元格中获取样式,从而允许您保留应用于其中的数字格式。 但是,当您从头开始创buildStyle对象并在使用Range.SetStyle时将样式应用于范围时,新创build的Style对象的每个方面都将被覆盖到该范围,导致格式更改为General范围。 我build议你使用Range.ApplyStyle方法,它允许你将StyleFlag的一个实例传递给前面提到的方法。 这样,您可以控制要覆盖的Style对象的各个方面。

注意:我在Aspose作为Developer Evangelist工作。

我能做到这一点的唯一方法就是通过明确设置该列中的所有单元格,在完成其他所有操作之后进行左alignment,从而“暴力破解”:

 private static readonly int MEMBERITEMCODE_COL = 4; . . . SetAllCustomerSheetColValsLeftAligned(MEMBERITEMCODE_COL); BorderizeDataPortionOfCustomerSheet(); . . . private void SetAllCustomerSheetColValsLeftAligned(int colIndex) { int rowsUsed = customerWorksheet.Cells.Rows.Count; CellsFactory cf = new CellsFactory(); Cell currentCell = null; Style currentStyle = null; for (int i = 2; i <= rowsUsed; i++) { currentCell = customerWorksheet.Cells[i, colIndex]; currentStyle = cf.CreateStyle(); currentStyle.HorizontalAlignment = TextAlignmentType.Left; currentStyle.Font.Name = fontForSheets; currentStyle.Font.Size = 11; currentCell.SetStyle(currentStyle); } } 
Interesting Posts