VB.Net的Font对象转换成Excel的Font对象

使用VB.NET开发了一个小工具,能够通过选择字体和颜色,实现对Excel表格样式的自定义,包括字体、字号、字体颜色和单元格背景色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近用vb.net开发了一个小工具,可以选择字体和颜色,利用自带的FontDialog和ColorDialog实现,然后用获取到的字体及颜色来修改Excel表格样式,Excel的字体对象是Microsoft.Office.Interop.Excel.Font,而字体控件是System.Drawing.Font,颜色控件是System.Drawing.Color,所以在赋值的时候需要进行转换。

 

下面只列出了部分比较重要代码,供以后查阅。

精简后没有调试,不保证能正常运行。

 

 

Dim myFont As System.Drawing.Font		'字体
Dim myColor As System.Drawing.Color		'字体颜色
Dim myCellColor As System.Drawing.Color	'单元格颜色

'打开字体窗口,选择一个字体,设置字形和字号
Dim f As New FontDialog
f.AllowScriptChange = False		'不能选择字符集
f.AllowVerticalFonts = False	'不显示旋转90度的字体
f.ShowEffects = False			'不能设置特殊效果,如下划线、删除线
If f.ShowDialog=DialogResult.OK Then
	myFont=f.Font
End If

'打开颜色窗口,选择一种字体颜色
Dim c As New ColorDialog
If c.ShowDialog = DialogResult.OK Then
	myColor = c.Color
End If

'打开颜色窗口,选择一种单元格颜色
Dim cc As New ColorDialog
If cc.ShowDialog = DialogResult.OK Then
	myCellColor = cc.Color
End If

'设置Excel字体和单元格样式
Dim ExcelApp As New Excel.Application
Dim ExcelBook As Excel.Workbook
Dim ExcelSheet As ExcelWorkSheet
ExcelBook = ExcelApp.Workbooks.Add
ExcelSheet = ExcelBook.ActionSheet
With ExcelSheet.Range("A1","E1")
	.Font.Name = myFont.Name	'设置字体
	.Font.Size = myFont.Size	'设置字号
	If myFont.Style = 3 Then
		'字形是粗斜体
		.Font.FontStyle = Join(Split(myFont.Style.ToString, ", "))
	Else
		'字形是常规、粗体、斜体
		.Font.FontStyle = myFont.Style.ToString
	End If
	.Font.Color = ColorTranslator.ToOle(myColor)	'设置字体颜色
	.Interior.Color = ColorTranslator.ToOle(myCellColor)	'设置单元格填充色
	.Borders.LineStyle = 1
End With
ExcelBook.SaveAs("D:\Book1.xls")
ExcelBook.Close()
ExcelApp.Quit()
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing

 

Public Class Form1 Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged TextBox1.Font = New Font("宋体", TextBox1.Font.Size, TextBox1.Font.Style) End Sub Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged TextBox1.Font = New Font("隶书", TextBox1.Font.Size, TextBox1.Font.Style) End Sub Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged TextBox1.Font = New Font("黑体", TextBox1.Font.Size, TextBox1.Font.Style) End Sub Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) TextBox1.Font = New Font(TextBox1.Font.FontFamily, 12, TextBox1.Font.Style) End Sub Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) TextBox1.Font = New Font(TextBox1.Font.FontFamily, 18, TextBox1.Font.Style) End Sub Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) TextBox1.Font = New Font(TextBox1.Font.FontFamily, TextBox1.Font.Size, TextBox1.Font.Style) End Sub Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) TextBox1.Font = New Font(TextBox1.Font.FontFamily, TextBox1.Font.Size, TextBox1.Font.Style) End Sub Private Sub CheckBox1_CheckedChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged If (CheckBox1.Checked) Then TextBox1.Font = New Font(TextBox1.Font, TextBox1.Font.Style Or FontStyle.Bold) Else TextBox1.Font = New Font(TextBox1.Font, TextBox1.Font.Style Xor FontStyle.Bold) End If End Sub Private Sub Form1_Load(ByVal sender As System.Obje
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值