ASP.NET Write Excel Style & Format (Excel Application) |
ASP.NET Write Excel Style & Format (Excel Application) เป็นการเขียน ASP.NET กับ Excel ในการกำหนดรูปแบบของ Cell หรือ Font เช่น สี ขนาด หรือรูปแบบอื่น ๆ
Framework 1.1,2.0,3.0,4,0
Excel for C# อ่านและดัดแปลงได้จากบทความนี้
Syntax
Dim xlApp As New Excel.Application
AspNetCreateFormatStyle.aspx
<%@ Import Namespace="Excel"%>
<%@ Import Namespace="System.IO"%>
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim FileName As String = "MyXls/MyExcel.xls"
'*** Create Excel.Application ***'
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
xlBook = xlApp.Workbooks.Add()
xlBook.Application.Visible = False
'*** Create Sheet 1 ***'
xlBook.Worksheets(1).Name = "My Sheet1"
xlBook.Worksheets(1).Select()
'*** Width & Height (A1:A1) ***'
With xlApp.ActiveSheet.Range("A1:A1")
.ColumnWidth = 40.0
.RowHeight = 25.0
End With
'*** Write text to Row 1 Column 1 ***'
With xlApp.ActiveSheet.Cells(1, 1)
.Value = "ThaiCreate.Com "
.Font.Name = "Tahoma"
.Font.Bold = True
.VerticalAlignment = -4108 '*** Center Rows ***'
.HorizontalAlignment = -4108 '*** Center Column ***'
.Font.Size = 12
End With
'*** Width & Height (A1:B1) ***'
With xlApp.ActiveSheet.Range("A1:B1")
End With
'*** Write text to Row 1 Column 2 ***'
With xlApp.ActiveSheet.Cells(1, 2)
.Value = "Mr.Weerachai Nukitram "
.Font.Name = "Tahoma"
.Font.Size = 20
End With
'*** Width & Height (A2:A2) ***'
With xlApp.ActiveSheet.Range("A2:A2")
.BORDERS.Weight = 1 '*** Border ***'
End With
'*** Write text to Row 1 Column 2 ***'
With xlApp.ActiveSheet.Cells(2, 1)
.Value = "I Love ThaiCreate.Com "
.Font.Name = "Tahoma"
.Font.Size = 10
.HorizontalAlignment = 4
End With
'*** Width & Height (A3:D3) ***'
With xlApp.ActiveSheet.Range("A3:D3")
.BORDERS.Color = RGB(0, 0, 0) '*** Border Color ***'
.BORDERS.Weight = 1 '*** Border ***'
.MergeCells = True '*** Merge Cells ***'
End With
'*** Write text to Row 1 Column 2 ***'
With xlApp.ActiveSheet.Cells(3, 1)
.Value = "I Love My Live"
.Font.Name = "Tahoma"
.Font.Size = 10
.HorizontalAlignment = -4108
.Interior.ColorIndex = 44 '*** Background Color ***'
End With
'*** Write text to Row 4 Column 5 ***'
With xlApp.ActiveSheet.Cells(4, 5)
.Value = "My Live"
.Font.Name = "Tahoma"
.Font.Size = 10
.Font.Italic = True
.Font.ColorIndex = 4
.EntireColumn.AutoFit() '*** AutoFit Column ***'
End With
'*** If Files Already Exist Delete files ***'
Dim MyFile As New FileInfo(Server.MapPath(FileName))
If MyFile.Exists Then
MyFile.Delete()
End IF
MyFile = Nothing
'*** Save Excel ***'
'xlSheet1.PrintOut 1 '*** Print to printer ***'
xlBook.SaveAs(Server.MapPath(FileName))
xlApp.Application.Quit()
'*** Quit and Clear Object ***'
xlBook = Nothing
xlApp = Nothing
Me.lblText.Text = "Excel Created <a href="& FileName & ">Click here</a> to Download."
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Excel Application</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
เพิ่มเติม
xlApp.ActiveSheet.Cells(9,1).Characters(1, 13).Font.Bold = True
// ใช้กำหนดความหนาของตัวอักษีที่สามารถที่ระบุตำแหน่งภายใน Cell
Screenshot
|