使用NPOI创建Excel文件

    Public Sub BuildExcel()
'写入内容到Excel
Dim hssfworkbook As HSSFWorkbook = WriteExcel() Dim destFileName As String = "D:\test.xls" Dim msfile As New MemoryStream() hssfworkbook.Write(msfile)
System.IO.File.WriteAllBytes(destFileName, msfile.ToArray())
End Sub Private Function WriteExcel() As HSSFWorkbook Dim hssfworkbook As New HSSFWorkbook() Dim excelSheet As HSSFSheet = hssfworkbook.CreateSheet("sheet1") Dim row0 As HSSFRow = excelSheet.CreateRow()
Dim cell0 As HSSFCell = row0.CreateCell()
cell0.SetCellValue("NUM") cell0.CellStyle = GetCellStyle(hssfworkbook, HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN _
, HSSFCellStyle.BORDER_THIN, HSSFColor.LIGHT_YELLOW.index, "#,##0") Dim i As Integer = , j As Integer =
While i <
Dim row As HSSFRow = excelSheet.CreateRow(j)
Dim cell As HSSFCell = row.CreateCell()
cell.SetCellValue(i)
cell.CellStyle = GetCellStyle(hssfworkbook, HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN _
, HSSFCellStyle.BORDER_THIN, HSSFColor.LIGHT_GREEN.index, "#,##0")
i +=
j +=
End While Return hssfworkbook
End Function Private Function GetCellStyle(ByVal hssfworkbook As HSSFWorkbook, ByVal borderLeft As Short, ByVal borderBottom As Short, _
ByVal borderRight As Short, ByVal borderTop As Short, ByVal fillforgeroundColor As Short, _
ByVal dataFormat As String) As HSSFCellStyle
Dim styleInfo As HSSFCellStyle = hssfworkbook.CreateCellStyle() styleInfo.BorderLeft = borderLeft
styleInfo.BorderBottom = borderBottom
styleInfo.BorderRight = borderRight
styleInfo.BorderTop = borderTop styleInfo.Alignment = HSSFCellStyle.ALIGN_CENTER
styleInfo.VerticalAlignment = HSSFCellStyle.VERTICAL_CENTER styleInfo.FillForegroundColor = fillforgeroundColor
'设置填充色
styleInfo.FillPattern = HSSFCellStyle.SOLID_FOREGROUND
'设置填充色的时候必须设置这个
styleInfo.DataFormat = HSSFDataFormat.GetBuiltinFormat(dataFormat)
' 当前日期格式的需要以下这样设置
'HSSFDataFormat format = (HSSFDataFormat)hssfworkbook.CreateDataFormat();
'styleInfo.DataFormat = format.GetFormat("yyyy年m月d日"); Return styleInfo
End Function
上一篇:NPOI 创建Excel 设置宽度 样式 颜色对比表


下一篇:为Eclipse安装主题插件