NPOI WrapText isn't working? - c#

I am trying to create a column with WrapText set to true for an XLSX file using NPOI.
The following does not seem to work:
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);
Naturally, the documentation is a little light for this one.
Any suggestions on changing the code? Otherwise, it seems to work fine.
I just can't get the wraptext to work.
EDIT:
After posting the question, I revised my code to use Reflection to detach the code from the individual classes (there are several I work with). This uses a modified Kendo interface that has the Excel Export button on the grid.
The revised code includes modifications included in the answer:
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;
}
This code now produces the desired result. I also added the XSSRichTextString to the formatting just for good measure.

"CellStyle.WrapText" set After Binding CellStyle ,its working For Me.
Solution:
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;

I think you forgot to change the cell's style. You're setting it only on the header.
This:
row.CreateCell(0).SetCellValue(member.FullName);
row.CreateCell(3).SetCellValue(member.endowed_professorship);
Should be something like:
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;
There's probably a better way of doing this; I'm no NPOI specialist. =)

Related

Remove white space between Chart Area and Legends

How to remove this large blank space between my chart area and the legends? See that I even tried different docking styles but the gap is still there.
Any ideas?
I import the data from a CSV file, generated by another application. Every column is a new data series but they are all added into the same chart and chartArea element.
// Load data from CSV into a DataTable
DataTable dataTable = ReadCsvFile(System.IO.Path.GetFileName("mqlWeekRace.txt"), false);
Chart chart1 = new Chart();
chart1.Height = 1000;
chart1.Width = 1500;
chart1.ChartAreas.Add("ChartArea1");
chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineWidth = 1;
chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dot;
chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineWidth = 1;
chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dot;
chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
// chart1.ChartAreas["ChartArea1"].AxisY.IsStartedFromZero = false;
// Line colors
string[] myColors = new string[] { "Blue", "DarkOrange", "Firebrick", "DarkSeaGreen", "DarkViolet", "Sienna",
"DarkOrchid", "IndianRed", "ForestGreen", "Teal", "MidnightBlue", "DimGray" };
int colorIndex = 0;
foreach (DataColumn col in dataTable.Columns)
{
if (!col.ColumnName.StartsWith("Data") && col.ColumnName.Length > 1)
{
chart1.Series.Add(col.ColumnName.ToString());
chart1.Series[col.ColumnName].ChartArea = "ChartArea1";
chart1.Legends.Add(new Legend(col.ColumnName));
chart1.Legends[col.ColumnName].LegendStyle = LegendStyle.Column;
chart1.Legends[col.ColumnName].Docking = Docking.Top;
chart1.Legends[col.ColumnName].Font = new Font(FontFamily.GenericSansSerif, 15);
chart1.Series[col.ColumnName].ChartType = SeriesChartType.Line;
chart1.Series[col.ColumnName].XValueMember = "Data";
chart1.Series[col.ColumnName].YValueMembers = col.ColumnName.ToString();
chart1.Series[col.ColumnName].XValueType = ChartValueType.Date;
chart1.Series[col.ColumnName].BorderWidth = 3;
chart1.Series[col.ColumnName].Color = System.Drawing.Color.FromName(myColors[colorIndex]);
colorIndex++;
}
}
chart1.DataManipulator.IsStartFromFirst = true;
chart1.DataSource = dataTable;
chart1.DataBind();

Crossout whole row

I try crossout row in table. Line must be over all cells in center vertical.
So I add new shape to cell, like this:
var cell = node as Cell;
var width = cell.CellFormat.Width;
var line = new Shape(args.Document, ShapeType.Line);
line.Width = width;
line.HorizontalAlignment = HorizontalAlignment.Center;
line.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;
line.Top = 5;
line.BehindText = true;
line.WrapType = WrapType.None;
line.StrokeColor = Color.Black;
line.Stroke.LineStyle = ShapeLineStyle.Single;
line.StrokeWeight = 1;
_builder.MoveTo(cell.LastParagraph);
_builder.InsertNode(line);
But this work only when I have single text line in cell, if is two or more line text, my crossout line is not center:
How fix it?
Maybe is other solution for crossout whole row?
You can build logic on the following code that inserts a full-width Line Shape at the middle of each Cell in Table.
Document doc = new Document("E:\\temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
foreach (Table table in doc.FirstSection.Body.Tables)
{
foreach (Row row in table.Rows)
{
foreach (Cell cell in row.Cells)
{
enumerator.Current = collector.GetEntity(cell.FirstParagraph);
while (enumerator.Type != LayoutEntityType.Cell)
{
enumerator.MoveParent();
}
double top = enumerator.Rectangle.Top + (enumerator.Rectangle.Height / 2);
double left = enumerator.Rectangle.Left;
double width = enumerator.Rectangle.Width;
builder.MoveTo(table.NextSibling);
Shape line = builder.InsertShape(ShapeType.Line, width, 0);
line.Top = top;
line.Left = left;
line.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
line.RelativeVerticalPosition = RelativeVerticalPosition.Page;
line.BehindText = true;
line.WrapType = WrapType.None;
line.StrokeColor = Color.Blue;
line.Stroke.LineStyle = ShapeLineStyle.Single;
line.StrokeWeight = 1;
}
}
}
doc.Save("E:\\temp\\19.1.docx");
Hope, this help. I work with Aspose as Developer Evangelist.

NPOI apply font to entire row of cells

I'm using NPOI to export my data to excel. The problem is I found it really hard for any kind of graphical changes.
This is the method I'm using now to apply bold font to my cells.
//Create new Excel workbook
var workbook = new HSSFWorkbook();
//Create new Excel sheet
var sheet = workbook.CreateSheet();
//Create a header row
var headerRow = sheet.CreateRow(0);
var boldFont = workbook.CreateFont();
boldFont.FontHeightInPoints = 11;
boldFont.FontName = "Calibri";
boldFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
int cellCounter = 0;
//day
var cell = headerRow.CreateCell(cellCounter++);
cell.SetCellValue("Day");
cell.CellStyle = workbook.CreateCellStyle();
cell.CellStyle.SetFont(boldFont);
//month
cell = headerRow.CreateCell(cellCounter++);
cell.SetCellValue("Month");
cell.CellStyle = workbook.CreateCellStyle();
cell.CellStyle.SetFont(boldFont);
//year
cell = headerRow.CreateCell(cellCounter++);
cell.SetCellValue("Year");
cell.CellStyle = workbook.CreateCellStyle();
cell.CellStyle.SetFont(boldFont);
//machine name
cell = headerRow.CreateCell(cellCounter++);
cell.SetCellValue("Machine unique name");
cell.CellStyle = workbook.CreateCellStyle();
cell.CellStyle.SetFont(boldFont); //and so on
Is there a ,,cleaner" way to do this ? Now i have to manually add font for individual cells. I've tried many ways to do this on the internet and nothing seems to be working. Do you have a tested way to apply style to specific column or row ?
OffTopic: If not can you provide me with some good open source libraries with decent documentation and support that allow excel export (learning new dll is a pain but... :) what can you do)?
I'm doing something similar and modified my take on it closer for your use:
private string[] columnHeaders =
{
"Day",
"Month",
"Year",
"Machine Unique Name"
}
private void buildSheet(HSSFWorkbook wb, DataTable data, string sheetName)
{
var cHelp = wb.GetCreationHelper();
var sheet = wb.CreateSheet(sheetName);
HSSFFont hFont = (HSSFFont)wb.CreateFont();
hFont.FontHeightInPoints = 11;
hFont.FontName = "Calibri";
hFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
HSSFCellStyle hStyle = (HSSFCellStyle)wb.CreateCellStyle();
hStyle.SetFont(hFont);
IRow headerRow = sheet.CreateRow(1);
int cellCount = 1;
foreach (string str in columnHeaders)
{
HSSFCell cell = (HSSFCell)headerRow.CreateCell(cellCount);
cell.SetCellValue(cHelp.CreateRichTextString((str)));
cell.CellStyle = hStyle;
cellCount += 1;
}
This iterates over however many headers you want starting at the second cell (cellCount = 1) second row (sheet.CreateRow(1)).

Datagridview with variable columns how?

Im new to C# and want to use a datagridview where the two first colums is the same every time and the rest i dependent of the content of a string list. But how can i do that ?
My code looks like this right now and i can see the failure, but dont know what to do about it :(
Plz help me :)
View.Columns.Clear();
View.AutoGenerateColumns = false;
DataGridViewTextBoxColumn colTime = new DataGridViewTextBoxColumn();
colTime.DataPropertyName = "Time";
colTime.HeaderText = "Time";
colTime.Name = "Time";
colTime.Width = 55;
View.Columns.Add(colTime);
DataGridViewTextBoxColumn colPlace = new DataGridViewTextBoxColumn();
colPlace.DataPropertyName = "Place";
colPlace.HeaderText = "Place";
colPlace.Name = "Place";
colPlace.Width = 55;
View.Columns.Add(colPlace);
for(int i = 0; i < Liste.Length; i++)
{
DataGridViewCheckBoxColumn "Log"+List[i] = new DataGridViewCheckBoxColumn();
"Log"+List[i].DataPropertyName = "List[i]";
"Log"+List[i].HeaderText = "List[i]";
"Log"+List[i].Name = "List[i]";
"Log"+List[i].Width = 55;
View.Columns.Add("Log"+List[i]);
}
Thanks in advance
you can use this function for creating typical DataGridView automatically,
DataGridView CreateDataGridDynamically(int NumberOfCol)
{
DataGridView D = new DataGridView();
for (int i = 0; i < NumberOfCol; i++)
{
DataGridViewTextBoxColumn C1 = new DataGridViewTextBoxColumn();
C1.HeaderText = "Something";
D.Columns.Add(C1);
}
return D;
}
and with datagridview.Rows.Add() and the forth overload, you can simply add some rows with your constraint:
DataGridView D = CreateDataGridDynamically(3);
D.Rows.Add("Sth1", "Sth1", "Sth3");
D.Rows.Add("Sth1", "Sth1", "Sth3");
D.Rows.Add("Sth1", "Sth1", "Sth3");
Controls.Add(D);

How to set Validation for a cell in Excel created using NPOI

I have created an excell file using NPOI using following code
var workbook = new HSSFWorkbook();
var sheet = workbook.CreateSheet("Candidate");
// Add header labels
var rowIndex = 0;
var row = sheet.CreateRow(rowIndex);
row.CreateCell(0).SetCellValue("Name");
row.CreateCell(1).SetCellValue("1,2,3");
row.CreateCell(2).SetCellValue("4,5,6");
row.CreateCell(3).SetCellValue("7,8,9");
rowIndex++;
// Add data rows
for (int i = 1; i <= 5; i++)
{
row = sheet.CreateRow(rowIndex);
row.CreateCell(0).SetCellValue("Candidate" + i.ToString());
row.CreateCell(1).SetCellValue("");
row.CreateCell(2).SetCellValue("");
row.CreateCell(3).SetCellValue("");
rowIndex++;
}
I just wanted to add some validation in each cell.
For eg: restrict cell 2 with inputs only 1,2,3
In Excel we can Set Data Validation to whole number and can specify Min and Max Value.
Any idea for achieving this will be a great help.
I find out this and working greatly with following code.
var markConstraint = DVConstraint.CreateExplicitListConstraint(new string[]{"1","2","3"});
var markColumn = new CellRangeAddressList(1, 5, 1, 1);
var markdv = new HSSFDataValidation(markColumn, markConstraint);
markdv.EmptyCellAllowed = true;
markdv.CreateErrorBox("Wrong Value", "Please Enter a correct value");
sheet.AddValidationData(markdv);
Try this example
XSSFDataValidation dataValidation = null;
XSSFDataValidationConstraint dvConstraint = null;
XSSFDataValidationHelper validationHelper = null;
int DVRowLimit = (Int16.MaxValue);
XSSFCellStyle numberCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
XSSFDataFormat numberDataFormat = (XSSFDataFormat)workbook.CreateDataFormat();
numberCellStyle.SetDataFormat(numberDataFormat.GetFormat("#,###,###"));
CellRangeAddressList cellRangeFieldsType1 = new CellRangeAddressList(1, DVRowLimit, headerCount, headerCount);
dvConstraint = (XSSFDataValidationConstraint)validationHelper.CreateintConstraint(OperatorType.BETWEEN, "0", Int64.MaxValue.ToString());
dataValidation = (XSSFDataValidation)validationHelper.CreateValidation(dvConstraint, cellRangeFieldsType1);
dataValidation.ShowErrorBox = true;
dataValidation.SuppressDropDownArrow = true;
dataValidation.ErrorStyle = 0;
dataValidation.CreateErrorBox("InvalidValue", "Number Should be a integer.");
dataValidation.ShowErrorBox = true;
dataValidation.CreatePromptBox("Number Data Validation", "Enter Number.");
dataValidation.ShowPromptBox = true;
sheet.AddValidationData(dataValidation);
sheet.SetDefaultColumnStyle(column, numberCellStyle);
similarly for range validation try
dvConstraint = (XSSFDataValidationConstraint)validationHelper.CreateintConstraint(OperatorType.BETWEEN, "0", "10");
I have consolidated similar examples into a documentation for NPOI. it covers various types of data validation and constraints. Documentation also includes the case when total character count for constraints are more than 255 characters.

Categories