NPOI WrapText不工作?

Tim*_*ing 4 c# npoi asp.net-mvc-5

我正在尝试使用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);
Run Code Online (Sandbox Code Playgroud)

当然,文档对于这个文档来说有点轻松.

有关更改代码的任何建议吗?否则,似乎工作正常.

我只是不能让wraptext工作.

编辑:

在发布问题之后,我修改了我的代码以使用Reflection从各个类中分离代码(我使用了几个代码).这使用修改后的Kendo界面,该界面在网格上具有Excel导出按钮.

修订后的代码包括答案中包含的修改:

                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;
                }
Run Code Online (Sandbox Code Playgroud)

此代码现在可以生成所需的结果.我还将XSSRichTextString添加到格式中,以便进行良好的测量.

Boo*_*net 5

"CellStyle.WrapText"设置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;
Run Code Online (Sandbox Code Playgroud)