我正在尝试使用NPOI为XLSX文件创建一个WrapText设置为true的列。
以下似乎不起作用:
var workbook = new XSSFWorkbook();
var headerRow = sheet.CreateRow(0);
var cellType = CellType.String;
var colStyle = sheet.GetColumnStyle(3);
colStyle.WrapText = true;
sheet.SetDefaultColumnStyle(3, colStyle);
headerRow.CreateCell(0, cellType).SetCellValue("Name");
headerRow.CreateCell(3, cellType).SetCellValue("Comments");
var font = workbook.CreateFont();
font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
XSSFCellStyle style = (XSSFCellStyle)workbook.CreateCellStyle();
style.SetFont(font);
style.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground;
style.FillForegroundColor = IndexedColors.Grey25Percent.Index;
style.IsLocked = false;
style.WrapText = true;
for (int i = 0; i < 6; i++)
{
var cell = headerRow.Cells[i];
cell.CellStyle = style;
}
headerRow.RowStyle = style;
sheet.SetColumnWidth(0, 30 * 256);
sheet.SetColumnWidth(1, 20 * 256);
sheet.SetColumnWidth(2, 20 * 256);
sheet.SetColumnWidth(3, 50 * 256);
sheet.SetColumnWidth(4, 30 * 256);
sheet.SetColumnWidth(5, 50 * 256);
int rowNumber = 1;
style = (XSSFCellStyle)workbook.CreateCellStyle();
style.IsLocked = false;
style.WrapText = true;
foreach (MemberListViewModel member in members)
{
var row = sheet.CreateRow(rowNumber++);
row.CreateCell(0).SetCellValue(member.FullName);
row.CreateCell(3).SetCellValue(member.endowed_professorship);
}
workbook.Write(output);
当然,文档对于这个文档来说有点亮。
有关更改代码的任何建议吗?否则,它似乎工作正常。
我无法让wraptext工作。
修改
在发布问题之后,我修改了我的代码以使用Reflection从各个类中分离代码(我使用了几个代码)。这使用修改后的Kendo界面,该界面在网格上有Excel Export按钮。
修订后的代码包括答案中包含的修改:
rowNumber = 1;
var cellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
cellStyle.WrapText = true;
foreach (var member in members)
{
String value = "";
var aType = member.GetType();
var real = aType.GetProperty(headings[i]);
if (real != null)
{
value = (real.GetValue(member) != null) ? String.Format(format, real.GetValue(member)) : "";
}
var row = sheet.GetRow(rowNumber++);
cell = row.CreateCell(i);
cell.SetCellValue(new XSSFRichTextString(value));
cell.CellStyle = cellStyle;
}
此代码现在可以生成所需的结果。我还将XSSRichTextString添加到格式中,以便进行良好的测量。
答案 0 :(得分:5)
&#34; CellStyle.WrapText&#34;设置After Binding CellStyle后,它正在为我工作。
解决方案:
ICellStyle CellCentertTopAlignment = workbook.CreateCellStyle();
CellCentertTopAlignment = workbook.CreateCellStyle();
CellCentertTopAlignment.SetFont(fontArial16);
CellCentertTopAlignment.Alignment = HorizontalAlignment.Center;
CellCentertTopAlignment.VerticalAlignment = VerticalAlignment.Top;
ICell Row1 = Row1.CreateCell(1);
Row1.SetCellValue(new HSSFRichTextString("I find a solution for this Problem.."));
Row1.CellStyle = CellCentertTopAlignment;
Row1.CellStyle.WrapText = true;
答案 1 :(得分:1)
我想你忘了改变细胞的风格。您只在标题上设置它。
此:
row.CreateCell(0).SetCellValue(member.FullName);
row.CreateCell(3).SetCellValue(member.endowed_professorship);
应该是这样的:
var cell0 = row.CreateCell(0);
cell0.SetCellValue(member.FullName);
cell0.Style = style;
var cell3 = row.CreateCell(3);
cell3.SetCellValue(member.endowed_professorship);
cell3.Style = style;
这可能是一种更好的方法;我不是NPOI专家。 =)