太好了,下面把「標題置中、文字靠左、數字靠右且四捨五入為整數、0/空白顯示“-”、字體 11、欄寬自動」全部統整成可重用的 VB.NET + NPOI 範本。你只要複製貼上即可。
1) 共用樣式工廠
Imports NPOI.SS.UserModel
Imports NPOI.XSSF.UserModel
' 封裝常用樣式
Public Class ExcelStyles
Public Property Title As ICellStyle
Public Property TextLeft As ICellStyle
Public Property NumberInt As ICellStyle
End Class
Public Module ExcelStyleFactory
' 四區段格式:正數;負數;零;文字
' -> 整數+千分位;0與文字都顯示「-」
Private Const NUMBER_FORMAT As String = "#,##0;-#,##0;-;-"
Public Function CreateStyles(wb As IWorkbook) As ExcelStyles
Dim font As IFont = wb.CreateFont()
font.FontHeightInPoints = 11
' 標題 (置中)
Dim styleTitle = wb.CreateCellStyle()
styleTitle.Alignment = HorizontalAlignment.Center
styleTitle.VerticalAlignment = VerticalAlignment.Center
styleTitle.SetFont(font)
' 文字 (靠左)
Dim styleText = wb.CreateCellStyle()
styleText.Alignment = HorizontalAlignment.Left
styleText.VerticalAlignment = VerticalAlignment.Center
styleText.SetFont(font)
' 數字 (靠右 + 整數 + 千分位 + 0/空白顯示 "-")
Dim styleNumber = wb.CreateCellStyle()
styleNumber.Alignment = HorizontalAlignment.Right
styleNumber.VerticalAlignment = VerticalAlignment.Center
styleNumber.DataFormat = wb.CreateDataFormat().GetFormat(NUMBER_FORMAT)
styleNumber.SetFont(font)
Return New ExcelStyles With {
.Title = styleTitle,
.TextLeft = styleText,
.NumberInt = styleNumber
}
End Function
End Module
2) 安全設定數字欄位的輔助方法
規則:
- 有數字 → 保留原值,以格式顯示為整數(四捨五入) + 千分位
- 值為 0 → 顯示「-」
- 空白或 Nothing → 顯示「-」
- 負數 → 顯示負號與千分位
說明:空白顯示「-」是靠自訂格式的第4區段達成(文字→「-」)。
若你想讓「真的沒有值」也看起來是「-」,可寫入空字串
""
(成為文字,會套用第4區段)。
Public Module ExcelCellHelpers
' 自動判斷並套用數字格式
Public Sub SetNumberCell(cell As ICell, styles As ExcelStyles, value As Object)
cell.CellStyle = styles.NumberInt
If value Is Nothing Then
' 寫文字空字串 → 使用格式第4段顯示 "-"
cell.SetCellValue(String.Empty)
Exit Sub
End If
Dim s = TryCast(value, String)
If s IsNot Nothing Then
If String.IsNullOrWhiteSpace(s) Then
cell.SetCellValue(String.Empty) ' 空白 → 第4段顯示 "-"
Exit Sub
End If
' 文字但可轉數字 → 當數字寫入以保留原值
Dim d As Double
If Double.TryParse(s, d) Then
cell.SetCellValue(d) ' 例如 2.6 → 顯示 3(僅格式四捨五入)
Else
' 真文字:也讓它顯示為 "-"(格式第4段)
cell.SetCellValue(s)
End If
Exit Sub
End If
' 其他型別:若可轉數字就當數字寫入,否則當空白
If TypeOf value Is IConvertible Then
Try
Dim d = Convert.ToDouble(value)
cell.SetCellValue(d) ' 例如 3948 → 顯示 3,948
Catch
cell.SetCellValue(String.Empty)
End Try
Else
cell.SetCellValue(String.Empty)
End If
End Sub
End Module
3) 使用示例(含自動欄寬)
Imports NPOI.SS.UserModel
Imports NPOI.XSSF.UserModel
Imports System.IO
Module Demo
Sub Main()
Dim wb As IWorkbook = New XSSFWorkbook()
Dim sh = wb.CreateSheet("Sheet1")
Dim st = ExcelStyleFactory.CreateStyles(wb)
' 標題列
Dim r0 = sh.CreateRow(0)
r0.CreateCell(0).SetCellValue("名稱") : r0.GetCell(0).CellStyle = st.Title
r0.CreateCell(1).SetCellValue("說明") : r0.GetCell(1).CellStyle = st.Title
r0.CreateCell(2).SetCellValue("數量") : r0.GetCell(2).CellStyle = st.Title
' 資料列 1:文字靠左、數字靠右(2.6 → 顯示 3)
Dim r1 = sh.CreateRow(1)
r1.CreateCell(0).SetCellValue("A 物品") : r1.GetCell(0).CellStyle = st.TextLeft
r1.CreateCell(1).SetCellValue("測試項目") : r1.GetCell(1).CellStyle = st.TextLeft
r1.CreateCell(2) ' 數量
ExcelCellHelpers.SetNumberCell(r1.GetCell(2), st, 2.6)
' 資料列 2:3948 → 顯示 3,948
Dim r2 = sh.CreateRow(2)
r2.CreateCell(0).SetCellValue("B 物品") : r2.GetCell(0).CellStyle = st.TextLeft
r2.CreateCell(1).SetCellValue("有庫存") : r2.GetCell(1).CellStyle = st.TextLeft
r2.CreateCell(2)
ExcelCellHelpers.SetNumberCell(r2.GetCell(2), st, 3948)
' 資料列 3:0 → 顯示 "-"
Dim r3 = sh.CreateRow(3)
r3.CreateCell(0).SetCellValue("C 物品") : r3.GetCell(0).CellStyle = st.TextLeft
r3.CreateCell(1).SetCellValue("暫無") : r3.GetCell(1).CellStyle = st.TextLeft
r3.CreateCell(2)
ExcelCellHelpers.SetNumberCell(r3.GetCell(2), st, 0)
' 資料列 4:空白/Null → 顯示 "-"
Dim r4 = sh.CreateRow(4)
r4.CreateCell(0).SetCellValue("D 物品") : r4.GetCell(0).CellStyle = st.TextLeft
r4.CreateCell(1).SetCellValue("待確認") : r4.GetCell(1).CellStyle = st.TextLeft
r4.CreateCell(2)
ExcelCellHelpers.SetNumberCell(r4.GetCell(2), st, Nothing) ' 或 ""
' 自動欄寬
For col = 0 To 2
sh.AutoSizeColumn(col)
Next
Using fs As New FileStream("output.xlsx", FileMode.Create, FileAccess.Write)
wb.Write(fs)
End Using
End Sub
End Module
補充與注意
- 顯示為整數但不改變原值:我們把數字「原值」寫入,靠格式
#,##0
來四捨五入顯示。 - 0 / 空白 → “-”:使用 四區段格式
正;負;零;文字
,零與文字區段都設定為-
。 - 空白兩種做法
- 不寫任何值(真正空白),Excel仍顯示空白;
- 寫入空字串
""
(文字),就會被文字區段顯示為-
。
本範例採第2種,以符合你「空白顯示 -」的需求。
- 字體統一為 11pt,已在樣式工廠設定。
- 需要垂直置中已加上
VerticalAlignment.Center
。
需要我再幫你加上:
- 邊框(細線、灰色表格)
- 標題列加粗、背景色
- 冻結標題列、首欄
- 日期/金額等其他格式
我可以直接在上述工廠再擴充給你。