20250904 excel

更新於 發佈於 閱讀時間約 14 分鐘

太好了,下面把「標題置中、文字靠左、數字靠右且四捨五入為整數、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)

' 資料列 23948 → 顯示 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)

' 資料列 30 → 顯示 "-"
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 / 空白 → “-”:使用 四區段格式 正;負;零;文字,零與文字區段都設定為 -
  • 空白兩種做法
    1. 不寫任何值(真正空白),Excel仍顯示空白;
    2. 寫入空字串 ""(文字),就會被文字區段顯示為 -
      本範例採第2種,以符合你「空白顯示 -」的需求。
  • 字體統一為 11pt,已在樣式工廠設定。
  • 需要垂直置中已加上 VerticalAlignment.Center

需要我再幫你加上:

  • 邊框(細線、灰色表格)
  • 標題列加粗、背景色
  • 冻結標題列、首欄
  • 日期/金額等其他格式
    我可以直接在上述工廠再擴充給你。
留言
avatar-img
留言分享你的想法!
avatar-img
Pocheng Chiu的沙龍
0會員
3內容數
你可能也想看
Thumbnail
常常被朋友問「哪裡買的?」嗎?透過蝦皮分潤計畫,把日常購物的分享多加一個步驟,就能轉換成現金回饋。門檻低、申請簡單,特別適合學生與上班族,讓零碎時間也能創造小確幸。
Thumbnail
常常被朋友問「哪裡買的?」嗎?透過蝦皮分潤計畫,把日常購物的分享多加一個步驟,就能轉換成現金回饋。門檻低、申請簡單,特別適合學生與上班族,讓零碎時間也能創造小確幸。
Thumbnail
本法省去開啟EXCEL檔,轉存為CSV檔之手動作業,縮短作業時間,提高工作效率,尤其是對象為複數個檔案場合
Thumbnail
本法省去開啟EXCEL檔,轉存為CSV檔之手動作業,縮短作業時間,提高工作效率,尤其是對象為複數個檔案場合
Thumbnail
🎗️本次主題成果展示:人力資訊分析 上集回顧 🔗EXCEL儀表板 | 人力資訊分析儀表板 #1 | 上手等級:入門🔗 🔗EXCEL儀表板 | 人力資訊分析儀表板 #2 | 上手等級:入門🔗 🔗EXCEL儀表板 | 人力資訊分析儀表板 #3 | 上手等級:入門🔗 🔗E
Thumbnail
🎗️本次主題成果展示:人力資訊分析 上集回顧 🔗EXCEL儀表板 | 人力資訊分析儀表板 #1 | 上手等級:入門🔗 🔗EXCEL儀表板 | 人力資訊分析儀表板 #2 | 上手等級:入門🔗 🔗EXCEL儀表板 | 人力資訊分析儀表板 #3 | 上手等級:入門🔗 🔗E
Thumbnail
🎗️本次主題成果展示:人力資訊分析 上集回顧 🔗EXCEL儀表板 | 人力資訊分析儀表板 #1 | 上手等級:入門🔗 🔗EXCEL儀表板 | 人力資訊分析儀表板 #2 | 上手等級:入門🔗 🔗EXCEL儀表板 | 人力資訊分析儀表板 #3 | 上手等級:入門🔗
Thumbnail
🎗️本次主題成果展示:人力資訊分析 上集回顧 🔗EXCEL儀表板 | 人力資訊分析儀表板 #1 | 上手等級:入門🔗 🔗EXCEL儀表板 | 人力資訊分析儀表板 #2 | 上手等級:入門🔗 🔗EXCEL儀表板 | 人力資訊分析儀表板 #3 | 上手等級:入門🔗
Thumbnail
🎗️本次主題成果展示:人力資訊分析 上集回顧 🔗EXCEL儀表板 | 人力資訊分析儀表板 #1 | 上手等級:入門🔗 🔗EXCEL儀表板 | 人力資訊分析儀表板 #2 | 上手等級:入門🔗 ♐人力資訊儀表板分集 本次人力資訊儀錶板預計分成5集依循漸進逐步完成 資料整
Thumbnail
🎗️本次主題成果展示:人力資訊分析 上集回顧 🔗EXCEL儀表板 | 人力資訊分析儀表板 #1 | 上手等級:入門🔗 🔗EXCEL儀表板 | 人力資訊分析儀表板 #2 | 上手等級:入門🔗 ♐人力資訊儀表板分集 本次人力資訊儀錶板預計分成5集依循漸進逐步完成 資料整
Thumbnail
利用文字紀錄,明確寫下自己的採購項目......
Thumbnail
利用文字紀錄,明確寫下自己的採購項目......
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News