我如何在那些水平分割的单元格中拟合值?我正在动态克隆每一行。当前填充的值是一个合并字段,因为我正在使用这种方法,但我无法使用它使报告动态化。任何帮助,将不胜感激。
问问题
901 次
1 回答
1
好吧,您不能在 MS Excel 的单个单元格中输入两个值。我认为您可以通过合并/取消合并某些单元格并将值相应地输入相关单元格来完成您的任务。请参阅下面的示例代码以供参考,它涵盖并设计了附加表格/矩阵的某些部分。请参考它,您可以编写自己的代码(通过 Aspose.Cells API)来相应地完成您的任务:
var workbook = new Workbook();
var worksheet = workbook.Worksheets[0];
//Input header value to B1 cell (later we will merge it: B1:C1 --> B1
worksheet.Cells[0, 1].PutValue("header2");
//Input value to B2 cell that would be merged with B3 to become B2.
worksheet.Cells[1, 1].PutValue(1);
//Input value to C2 cell.
worksheet.Cells[1, 2].PutValue(2);
//Input value to C3 cell.
worksheet.Cells[2, 2].PutValue(3);
//Set row heights for 2nd and third rows for the cells accordingly.
worksheet.Cells.SetRowHeight(1, 25);
worksheet.Cells.SetRowHeight(2, 25);
//Merging cells.
//Merge B1:C1 --> B1
worksheet.Cells.Merge(0, 1, 1, 2);
//Merge B2:B3 --> B2
worksheet.Cells.Merge(1, 1, 2, 1);
//Formatting cells and ranges.
//Creating a range that spans over the all data cells of a worksheet
var range = worksheet.Cells.CreateRange("B1", "C3");
//Create a Style object.
Style colstyle = workbook.CreateStyle();
colstyle.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
colstyle.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
colstyle.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
colstyle.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
colstyle.HorizontalAlignment = TextAlignmentType.Center;
StyleFlag flag = new StyleFlag();
flag.Borders = true;
flag.HorizontalAlignment = true;
//Apply the style to the range
range.ApplyStyle(colstyle, flag);
workbook.Save("e:\\test2\\output__mergedcells.xlsx");
请参阅在 Ms Excel 中获取的输出 Excel 文件的屏幕截图供您参考: http: //prntscr.com/8rbc0i
我是 Aspose 的开发人员/传播者。
于 2015-10-14T19:04:25.443 回答