'使用NPOI写入新创建的excel文件,导出文件:
Private Sub Sub_WriteXls()
Dim XlsBook As XSSFWorkbook
Dim XlsSheet As XSSFSheet
Dim XlsRow As XSSFRow
'Dim Path As FileStream
Dim PathSave As FileStream
Dim A As Integer
Try
'Path = File.OpenRead(Application.StartupPath & "\oms导入模版.xls")
XlsBook = New XSSFWorkbook()
XlsSheet = XlsBook.CreateSheet("进出库存流量日报表")
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
''建立表头
XlsRow = XlsSheet.CreateRow(0)
''读取表格列表头数据,并填充到xls
For A = 0 To GridView1.Columns.Count - 1
XlsRow.CreateCell(A).SetCellValue(GridView1.Columns(A).ToString)
Next
For A = 0 To GridView1.RowCount - 1
'建立新行
XlsRow = XlsSheet.CreateRow(A + 1)
For B = 0 To GridView1.Columns.Count - 1
If B < 10 Then
XlsRow.CreateCell(B).SetCellValue(GridView1.GetRowCellValue(A, GridView1.Columns(B)).ToString)
ElseIf GridView1.GetRowCellValue(A, GridView1.Columns(B)).ToString <> "" Then
XlsRow.CreateCell(B).SetCellValue(Convert.ToInt16(GridView1.GetRowCellValue(A, GridView1.Columns(B)).ToString))
End If
Next
Next
PathSave = File.OpenWrite(SaveFileDialog1.FileName)
XlsBook.Write(PathSave)
PathSave.Close()
MsgBox("数据导出完毕!")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub