ASP.NET Excel Draw Line (Excel Application) |
ASP.NET Excel Draw Line (Excel Application) เป็นการเขียนไฟล์ลงใน (*.xls) โดยทำการกำหนด Draw Line หรือการสร้างเส้นลงในชีท
Framework 1.1,2.0,3.0,4,0
Excel for C# อ่านและดัดแปลงได้จากบทความนี้
Syntax
Dim xlApp As New Excel.Application
AspNetExcelDrawLine.aspx
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="Microsoft.Office.Interop.Excel"%>
<%@ 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 Microsoft.Office.Interop.Excel.Application
Dim xlSheet1 As Microsoft.Office.Interop.Excel.Worksheet
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
xlBook = xlApp.Workbooks.Add()
'*** Create Sheet 1 ***'
xlSheet1 = xlBook.Worksheets(1)
xlSheet1.Name = "My Sheet1"
xlApp.Application.Visible = False
'*** Write text to Row 1 Column 1 ***'
With xlApp.ActiveSheet.Cells(1,1)
.Value = "ThaiCreate.Com"
End With
'*** Draw Line ***'
Dim x1, y1
Dim x2, y2
x1 = xlApp.ActiveSheet.Range("C2").Left
y1 = xlApp.ActiveSheet.Range("C2").Top
x2 = xlApp.ActiveSheet.Range("E2").Left
y2 = xlApp.ActiveSheet.Range("E2").Top
xlApp.ActiveSheet.Range("C2:E2").Select()
xlApp.ActiveSheet.Shapes.AddLine(x1, y1, x2, y2).Select()
'*** 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 ***'
xlSheet1.SaveAs(Server.MapPath(FileName))
xlApp.Application.Quit()
xlApp.Quit()
'*** Quit and Clear Object ***'
xlSheet1 = Nothing
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>
Screenshot
|