C#: 用底下轉成C#
http://www.developerfusion.com/tools/convert/vb-to-csharp/
VB :
記得要匯入NPOI的dll
Imports NPOI.HSSF.UserModel
Imports NPOI.HSSF.Util
Imports NPOI.SS.UserModel
Public Shared Sub ExportUseNOPI(ByVal Dtable As DataTable,
ByVal ColumnArray As Integer(), ByVal RowArray As Integer())
Dim ExcelBook As New HSSFWorkbook
Dim MS As New MemoryStream
Dim sheet As HSSFSheet = ExcelBook.CreateSheet("sheet1")
Dim Titlestyle As HSSFCellStyle = ExcelBook.CreateCellStyle()
Dim ExRowIndxe As Integer = 0
Titlestyle.FillForegroundColor = HSSFColor.GREEN.index
Titlestyle.FillPattern = FillPatternType.SOLID_FOREGROUND
'設定框線--------------------
setBorder(Titlestyle) '----------------------
'設定第3個欄位寬度
'sheet.SetColumnWidth(3, 13500)
Dim headRow As HSSFRow = sheet.CreateRow(ExRowIndxe)
ExRowIndxe += 1
'移除不需要的Columns
If Not ColumnArray Is Nothing Then
For cum = 0 To ColumnArray.Length - 1
Dtable.Columns.RemoveAt(ColumnArray(cum))
Next
End If
Dim list As New List(Of DataRow)
'int陣列轉 List集合
If Not RowArray Is Nothing Then
For rw = 0 To RowArray.Length - 1
Dim row As DataRow = Dtable.Rows(RowArray(rw))
list.Add(row)
Next
'移除不需要的Row
For r = 0 To list.Count - 1
Dtable.Rows.Remove(list.Item(r))
Next
End If
For K As Integer = 0 To Dtable.Columns.Count - 1
Dim cell As HSSFCell = headRow.CreateCell(K)
cell.SetCellValue(Dtable.Columns(K).ColumnName)
cell.CellStyle = Titlestyle
Next
'區別單數行與雙數行的style
Dim SingleRowStyle As HSSFCellStyle = ExcelBook.CreateCellStyle()
SingleRowStyle.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index
SingleRowStyle.FillPattern = FillPatternType.SOLID_FOREGROUND
'將資料格式改為Text
SingleRowStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@")
Dim EvenRowStyle As HSSFCellStyle = ExcelBook.CreateCellStyle()
EvenRowStyle.FillForegroundColor = HSSFColor.WHITE.index
EvenRowStyle.FillPattern = FillPatternType.SOLID_FOREGROUND
'將資料格式改為Text
SingleRowStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@")
'設定框線'----------------------
setBorder(SingleRowStyle) '----------------------
setBorder(EvenRowStyle) '----------------------
For i As Integer = 0 To Dtable.Rows.Count - 1
Dim SheetRow As HSSFRow = sheet.CreateRow(ExRowIndxe)
ExRowIndxe += 1
For j As Integer = 0 To Dtable.Rows(i).ItemArray.Length - 1
Dim cell As HSSFCell = SheetRow.CreateCell(j)
Dim cellString = Dtable.Rows(i).Item(j).ToString
If cellString.Equals(" ") Then
cellString = ""
End If
cell.SetCellValue(cellString)
If i Mod 2 = 0 Then
cell.CellStyle = EvenRowStyle
Else
cell.CellStyle = SingleRowStyle
End If
Next
Next
ExcelBook.Write(MS)
'AddHeader若沒寫,會直接print在本網頁上
HttpContext.Current.Response.AddHeader("Content-Disposition",
String.Format("attachment; filename=ExportExcel.xls"))
HttpContext.Current.Response.BinaryWrite(MS.ToArray())
ExcelBook = Nothing
MS.Close()
MS.Dispose()
End Sub
Public Shared Sub setBorder(ByVal style As HSSFCellStyle)
style.BorderBottom = NPOI.SS.UserModel.CellBorderType.THIN
style.BorderLeft = NPOI.SS.UserModel.CellBorderType.THIN
style.BorderRight = NPOI.SS.UserModel.CellBorderType.THIN
style.BorderTop = NPOI.SS.UserModel.CellBorderType.THIN
style.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.BLACK.index
style.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.BLACK.index
style.RightBorderColor = NPOI.HSSF.Util.HSSFColor.BLACK.index
style.TopBorderColor = NPOI.HSSF.Util.HSSFColor.BLACK.index
End Sub
請先 登入 以發表留言。