NPOI set cell style "HSSFFont.BOLDWEIGHT_BOLD" is not working - asp.net

I'm using NPOI to output excel from Asp.Net. I want to set bold and normal style to my cell but it is working for few cell and not for remaining cell.Please have look on following example:
Dim hssfworkbook As New HSSFWorkbook()
Dim sheetOne As HSSFSheet = hssfworkbook.CreateSheet("Sheet1")
hssfworkbook.CreateSheet("Sheet2")
hssfworkbook.CreateSheet("Sheet3")
Dim cellStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
cellStyle.Alignment = HSSFCellStyle.ALIGN_CENTER
Dim font As HSSFFont = _hssfworkbook.CreateFont()
font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
cellStyle.SetFont(font)
For i = 0 To 9 Step 1
'I want to add cell style to these cells
If i Mod 2 = 0 Then
Sheet1.CreateRow(i).CreateCell(1).SetCellValue(i)
font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
cellStyle.SetFont(font)
Sheet1.GetRow(i).GetCell(1).CellStyle = cellStyle
Else
Sheet1.CreateRow(i).CreateCell(1).SetCellValue(i)
font.Boldweight = HSSFFont.BOLDWEIGHT_NORMAL
cellStyle.SetFont(font)
Sheet1.GetRow(i).GetCell(1).CellStyle = cellStyle
End If
Next
Actually code is working fine but i don't know the particular situation from where and why its stops working for remaining few rows. Its not working properly for all cells and from that particular cell the bold and normal property stops working on whole sheet like sheet2 and sheet3.

You have to use multiple styles because a change to a style affects everywhere that style is referenced in the sheet.
Dim hssfworkbook As New HSSFWorkbook()
Dim sheetOne As HSSFSheet = hssfworkbook.CreateSheet("Sheet1")
hssfworkbook.CreateSheet("Sheet2")
hssfworkbook.CreateSheet("Sheet3")
Dim BoldFont As HSSFFont = hssfworkbook.CreateFont()
BoldFont.Boldweight = HSSFFont.BOLDWEIGHT_BOLD
Dim NormalFont As HSSFFont = hssfworkbook.CreateFont()
NormalFont.Boldweight = HSSFFont.BOLDWEIGHT_NORMAL
Dim cellStyleBold As HSSFCellStyle = hssfworkbook.CreateCellStyle()
With cellStyleBold
.setFont(BoldFont)
.Alignment = HSSFCellStyle.ALIGN_CENTER
End With
Dim cellStyleNormal As HSSFCellStyle = hssfworkbook.CreateCellStyle()
With cellStyleNormal
.setFont(NormalFont)
.Alignment = HSSFCellStyle.ALIGN_CENTER
End With
For i - 0 To 9 Step 1
If i Mod 2 = 0 Then
With Sheet1.CreateRow(i).CreateCell(1)
.SetCellValue(i)
.cellStyle = cellStyleBold
End With
Else
With Sheet1.CreateRow(i).CreateCell(1)
.SetCellValue(i)
.cellStyle = cellStyleNormal
End With
End If
Next

Related

get values of dynamic checkboxes

I am dynamically creating checkboxes in VB.Net and an .aspx page, based on values in my db. I'm placing them in a two column table for ease of alignment. this part works fine.
Private Async Function InitForEditAsync() As Task
Dim docList = Await GetLoanApplicationConfigurationDocs()
Dim row = New HtmlTableRow()
Dim cell = New HtmlTableCell()
Dim i = 0
For Each doc In docList
Dim chkBox = New HtmlInputCheckBox()
Dim lbl = New Label()
Dim remainder = i Mod 2
chkBox.ID = "chkDocId" + doc.Id.ToString
lbl.Text = doc.DisplayName
cell.Controls.Add(chkBox)
cell.Controls.Add(lbl)
row.Cells.Add(cell)
cell = New HtmlTableCell()
If remainder <> 0 OrElse i = docList.Count() - 1 Then
tblEdit.Rows.Add(row)
row = New HtmlTableRow()
End If
i += 1
Next
End Function
Now I need to retrieve the values without knowing the id's but am not having any luck. I tried this:
For Each chkBox As HtmlInputCheckBox In pnlEdit.Controls.OfType(Of HtmlInputCheckBox)
but the checkboxes are not returned in the list of controls. The table is, but there are no rows in the table object when I explored it in the control collection and when I tried this:
For Each row As HtmlTableRow In tblEdit.Rows.OfType(Of HtmlTableRow)
If it will help, here is a Snip of the UI and the HTML that is created:
Any suggestions are appreciated. Thanks in advance.
Based on some ideas I got from another site, I'm going to rewrite this using the asp:CheckBoxList. apparently it binds like a datagrid and you can enumerate through it. Seems like what i need.
UPDATE: Everything I posted to start was resolved with five lines of code! "cblDocList is my asp CheckboxList and docList is my ienumerable of objects.
cblDocList.RepeatColumns = 2
cblDocList.DataSource = docList
cblDocList.DataTextField = "DisplayName"
cblDocList.DataValueField = "Id"
cblDocList.DataBind()
It’s something you can do through a loop for each row and each cell or using Linq to have only cells that have controls of type HtmlInputCheckBox inside.
I have simplified your code to be able run that here also shows you an example to achieve your task. Obviously you must change following your exigences .
Hope I well understood :)
Dim tblEdit As New HtmlTable
For k As Integer = 0 To 10
Dim cell = New HtmlTableCell()
Dim row = New HtmlTableRow()
Dim chkBox = New HtmlInputCheckBox()
Dim lbl = New Label()
Dim remainder = k Mod 2
chkBox.ID = "chkDocId_" + k.ToString
chkBox.Checked = remainder = 0
lbl.Text = "Text indicator of CheckBox nr:" + k.ToString
cell.Controls.Add(chkBox)
cell.Controls.Add(lbl)
row.Cells.Add(cell)
cell = New HtmlTableCell()
tblEdit.Rows.Add(row)
Next
Dim checkBoxes As IEnumerable(Of HtmlInputCheckBox) =
(From mRow In tblEdit.Rows).Select(Function(mr)
Dim cb = (From cc In CType(mr, HtmlTableRow).Cells
Where CType(cc, HtmlTableCell).Controls.OfType(Of HtmlInputCheckBox).Count > 0
Select CType(cc, HtmlTableCell).Controls.OfType(Of HtmlInputCheckBox)()(0)).FirstOrDefault
Return CType(cb, HtmlInputCheckBox)
End Function).ToList
For Each checkBox In checkBoxes
Debug.WriteLine("CheckBox ID: {0} Checked: {1} ", checkBox.ID, checkBox.Checked)
Next

Add Two Controls in a Table Cell not working

Hi I am trying to add two controls into a table cell the code is below. Unfortunately the hyperlink is always outside the table cell, whilst the label is inside. Individually they are working fine but together not so good, could somebody help me please.
Dim iCell As New TableCell
Dim lbl As New Label
lbl.Height = Unit.Pixel(100)
lbl.Width = Unit.Pixel(150)
lbl.Style.Add("padding", "5px")
lbl.Style.Add("border-radius", "10px")
lbl.BorderStyle = BorderStyle.Solid
lbl.BorderWidth = Unit.Pixel(1)
lbl.BorderColor = Color.Black
lbl.Font.Size = 9
lbl.Text = "Event: " & dt.Rows(y).Item("eventname")
lbl.BackColor = ColorTranslator.FromHtml(dt.Rows(y).Item("roomcolor"))
Dim hyp As New HyperLink
hyp.Font.Size = 9
hyp.Text = "Modify"
hyp.NavigateUrl = "modify.aspx"
iCell.Controls.Add(lbl)
iCell.Controls.Add(hyp)
iCell.VerticalAlign = VerticalAlign.Top
tRow.Cells.Add(iCell)

Trouble applying format to number list in openxml word document

I have been able to create simple word document using OpenXML. I am trying to add multi-level numbered list as follows:
1. Point 1
a. Sub Point 1
I have tried following code
Step 1:
Dim numberid As New NumberingId
numberid.Val = 1
Dim numlevelref As New NumberingLevelReference
numlevelref.Val = 0
Dim numberprop As New NumberingProperties
numberprop.NumberingId = numberid
numberprop.NumberingLevelReference = numlevelref
Dim pp As New ParagraphProperties
pp.NumberingProperties = numberprop
Dim p As Paragraph = New Paragraph
p.Append(pp)
Dim run As New Run
run.Append(New Text("Point 1"))
p.Append(run)
Step 2:
Dim numberid As New NumberingId
numberid.Val = 1
Dim numlevelref As New NumberingLevelReference
numlevelref.Val = 1 '(Only this has changed from Step 1)
Dim numberprop As New NumberingProperties
numberprop.NumberingId = numberid
numberprop.NumberingLevelReference = numlevelref
Dim pp As New ParagraphProperties
pp.NumberingProperties = numberprop
Dim p As Paragraph = New Paragraph
p.Append(pp)
Dim run As New Run
run.Append(New Text("Sub Point 1"))
p.Append(run)
This gives me following output:
1. Point 1
1. Sub Point 1
Please let me know how to apply number format. I have understood following is the class to use:
Dim numformat As New NumberingFormat
numformat.Val = NumberFormatValues.LowerLetter
But where to use numformat in Step 2?

Creating a label inside Gridview Cell from VB

I have some Code in VB that is looping through a gridview's rows and is checking the values of a certain collumn, I have written some code inside the if statement that creates a clickable label inside the cell for when the cell is "", I have already written a bit of code that could create my label but I'm not too sure how it would create it inside row.Cells(8)
I was wondering if could get some assistance with how I should be doing this?...
Here's my Code:
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row = GridView1.Rows(i)
Dim rowHeaderCell = row.Cells(8)
If rowHeaderCell.Text = " " Then
Dim lbl As New Label
lbl.Size = New System.Drawing.Size(159, 23)
lbl.Location =
lbl.Text = "label text goes here"
Me.Controls.Add(lbl)
End If
Next
Thankyou in advance!
To make a clickable label change the type of the column to : [DataGridViewLinkColumn]
here is the code to replace the Empty cell value by the value of LBL:
For i As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(i).Cells(8).Value.Trim = "" Then
Dim lbl As String
lbl = "label text goes here "
GridView1.Rows(i).Cells(8).Value = lbl
End If
Next

treeview conditional formatting

I have a treeview populating from a data set like this
Dim PrSet As New DataSet()
If lblemail.Text.ToString().Equals("ali.saleem#shakarganj.com.pk") Then
PrSet = PDataset("select distinct PEND,to_char(BPV_DTE,'DD MON YYYY') BPV_DTE,BPV_DTE BPV_DTE1,COUNT from chq_dir order by 3 desc")
Else
PrSet = PDataset("select distinct PEND,to_char(BPV_DTE,'DD MON YYYY') BPV_DTE,BPV_DTE BPV_DTE1,COUNT from chq_dte order by 3 desc")
End If
TreeView2.Nodes.Clear()
For Each dr As DataRow In PrSet.Tables(0).Rows
Dim tnParent As New TreeNode()
tnParent.Text = dr("PEND").ToString()
tnParent.Value = dr("BPV_DTE")
tnParent.PopulateOnDemand = True
tnParent.SelectAction = TreeNodeSelectAction.Select
tnParent.ToolTip = tnParent.Text
tnParent.Expand()
TreeView2.Nodes.Add(tnParent)
If dr("COUNT").ToString() = "0" Then
TreeView2.Font.Bold= True
End If
Next dr
There is Column COUNT in which there is 0 in some dates.Problem is that i am trying to bold the treeview where COUNT is 0 but it is not working Any one can give me clear idea to do so.
I have solved My Question Like this
If Not dr("COUNT") = 0 Then
tnParent.Text = "<b>" & dr("PEND").ToString() & "</b>"
End If
I hope I've gethered what you meant by this question...
For Each tn As TreeNode In treeView2.Nodes
If tn.Text = "0" Then
Dim index As Integer
index = tn.Index
treeView2.Nodes(index).NodeFont = New Font(treeView2.Font, FontStyle.Bold)
End If
Next
Apply the style to tnparent and do this before adding the node to parent in treeview, and also from your code it looks like you are changing the fonts to italic not bold.

Resources