Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > ASP.NET > ASP.NET Charts/Graph (Excel.Application) > ASP.NET Charts/Graph - Excel Application


ASP.NET Charts/Graph - Excel Application

ASP.NET Charts/Graph - Excel Application ถ้าหากต้องการใช้งาน Excel ในการ สร้าง Charts หรือ Graph ก็สามารถทำได้เช่นเดียวกันครับ โดยเรียกใช้คุณสมบัติของ Excel ในการสร้างกราฟต่าง ๆ ซึ่งถ้าเรารู้ Syntax เราจะสามารถสร้างกราฟได้อย่างง่ายดาย และในบทเรียนนี้ผมได้ยกตัวอย่างการสร้างกราฟหลายๆ รูปแบบ (สามารถดูชนิดของกราฟได้ที่ Graph Type ID)

Framework 1.1,2.0,3.0,4,0

Excel for C# อ่านและดัดแปลงได้จากบทความนี้



ASP.NET Chart - asp:Chart



ASP Charts/Graph Type ID (Excel.Application)

ASP.NET Excel

ตัวอย่างกราฟบน Excel ซึ่ง ASP.NET สามารถเรียกใช้งานได้หลังจากที่ Add Reference


ASP.NET Config Excel (Excel Application)

ตัวอย่างแรกจะเป็นการสร้างกราฟแท่งแบบง่าย ๆ

AspNetExcelCharts1.aspx

001.<%@ Import Namespace="System.Data"%>
002.<%@ Import Namespace="System.Data.OleDb"%>
003.<%@ Import Namespace="Excel"%>
004.<%@ Import Namespace="System.IO"%>
005.<%@ Page Language="VB" %>
006.<script runat="server">
007.    Sub Page_Load(sender As Object, e As EventArgs)    
008.        Dim objConn As New OleDbConnection
009.        Dim dtAdapter As OleDbDataAdapter
010.        Dim dt As New System.Data.DataTable
011. 
012.        Dim strConnString As String
013.        strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/mydatabase.mdb") & ";"
014.        objConn = New OleDbConnection(strConnString)
015.        objConn.Open()
016. 
017.        Dim strSQL As String
018.        strSQL = "SELECT * FROM customer"
019. 
020.        '*** Create DataTable ***' 
021.        dtAdapter = New OleDbDataAdapter(strSQL, objConn)
022.        dtAdapter.Fill(dt)
023. 
024.        '*** Export To Excel ***'
025.        Dim FileName As String = "MyXls/MyExcel.xls"
026. 
027.        '*** Create Exce.Application ***'
028.        Dim xlApp As New Excel.Application
029.        Dim xlSheet As Excel.Worksheet
030.        Dim xlBook As Excel.Workbook
031.        Dim i, intStartRows, intEndRows As Integer
032. 
033. 
034.        xlBook = xlApp.Workbooks.Add()
035.        xlSheet = xlBook.Worksheets(1)
036. 
037.        xlBook.Application.Visible = False
038. 
039.        xlBook.Sheets.Add()
040.        xlBook.ActiveSheet.Name = "Customer"
041. 
042.        With xlBook.Sheets("Customer").Cells(1, 1)
043.            .Value = "My Customer"
044.            .Font.Bold = True
045.            .Font.Name = "Tahoma"
046.            .Font.Size = 16
047.        End With
048. 
049.        With xlBook.Sheets("Customer").Cells(2, 1)
050.            .Value = "Customer Name"
051.            .Font.Name = "Tahoma"
052.            .BORDERS.Weight = 1
053.            .Font.Size = 10
054.            .MergeCells = True
055.        End With
056. 
057.        With xlBook.Sheets("Customer").Cells(2, 2)
058.            .Value = "Used"
059.            .BORDERS.Weight = 1
060.            .Font.Name = "Tahoma"
061.            .Font.Size = 10
062.            .MergeCells = True
063.        End With
064. 
065.        intStartRows = 3
066.        intEndRows = intStartRows + dt.Rows.Count - 1
067.        For i = 0 To dt.Rows.Count - 1
068.            xlBook.Sheets("Customer").Cells(intStartRows + i, 1).Value = dt.Rows(i)("Name")
069.            xlBook.Sheets("Customer").Cells(intStartRows + i, 2).Value = dt.Rows(i)("Used")
070.            xlBook.Sheets("Customer").Cells(intStartRows + i, 2).NumberFormat = "$#,##0.00"
071.        Next
072. 
073.        '*** Creating Chart ***'
074.        xlBook.Charts.Add()
075.        xlBook.ActiveChart.Name = "ExcelGraph"
076.        xlBook.Charts("ExcelGraph").SetSourceData(xlBook.Sheets("Customer").Range("A" & intStartRows & ":B" & intEndRows & ""))
077. 
078.        '*** Chart 3D Columns ***'
079.        With xlBook.Charts("ExcelGraph")
080.            .ChartType = 54
081.            .PlotBy = 1
082.            .HasAxis(1) = 0
083.        End With
084. 
085.        '*** If Files Already Exist Delete files ***'
086.        Dim MyFile As New FileInfo(Server.MapPath(FileName))
087.        If MyFile.Exists Then
088.            MyFile.Delete()
089.        End IF
090.        MyFile = Nothing
091. 
092.        '*** Save Excel ***'
093.        'xlSheet.PrintOut 1 '*** Print to printer ***'
094.        xlSheet.SaveAs(Server.MapPath(FileName))
095.        xlApp.Quit()
096. 
097.        '*** Quit and Clear Object ***'
098.        xlSheet = Nothing
099.        xlBook = Nothing
100.        xlApp = Nothing
101. 
102.        '*** End Export To Excel ***'
103. 
104.        Me.lblText.Text = "Charts Created <a href=" & FileName & ">Click here</a> to Download."
105. 
106.        dtAdapter = Nothing
107.        objConn.Close()
108.        objConn = Nothing
109. 
110.    End Sub
111.</script>
112.<html>
113.<head>
114.<title>ThaiCreate.Com ASP.NET - Excel Application</title>
115.</head>
116.<body>
117.    <form id="form1" runat="server"
118.    <asp:Label id="lblText" runat="server"></asp:Label>
119.    </form>
120.</body>
121.</html>


Screenshot

ASP.NET Excel




ตัวอย่างที่ 2 สร้างข้อมูลและกราฟอยู่ใน Sheet เดียวกัน

AspNetExcelCharts2.aspx

001.<%@ Import Namespace="System.Data"%>
002.<%@ Import Namespace="System.Data.OleDb"%>
003.<%@ Import Namespace="Excel"%>
004.<%@ Import Namespace="System.IO"%>
005.<%@ Page Language="VB" %>
006.<script runat="server">
007.    Sub Page_Load(sender As Object, e As EventArgs)    
008.        Dim objConn As New OleDbConnection
009.        Dim dtAdapter As OleDbDataAdapter
010.        Dim dt As New System.Data.DataTable
011. 
012.        Dim strConnString As String
013.        strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/mydatabase.mdb") & ";"
014.        objConn = New OleDbConnection(strConnString)
015.        objConn.Open()
016. 
017.        Dim strSQL As String
018.        strSQL = "SELECT * FROM customer"
019. 
020.        '*** Create DataTable ***' 
021.        dtAdapter = New OleDbDataAdapter(strSQL, objConn)
022.        dtAdapter.Fill(dt)
023. 
024.        '*** Export To Excel ***'
025.        Dim FileName As String = "MyXls/MyExcel.xls"
026. 
027.        '*** Create Exce.Application ***'
028.        Dim xlApp As New Excel.Application
029.        Dim xlSheet As Excel.Worksheet
030.        Dim xlBook As Excel.Workbook
031.        Dim i, intStartRows, intEndRows As Integer
032. 
033.        xlBook = xlApp.Workbooks.Add()
034.        xlSheet = xlBook.Worksheets(1)
035. 
036.        xlApp.Application.Visible = False
037. 
038.        '*** Delete Sheet (2,3) - Sheet Default ***'
039.        xlBook.Worksheets(2).Select()
040.        xlBook.Worksheets(2).Delete()
041.        xlBook.Worksheets(2).Select()
042.        xlBook.Worksheets(2).Delete()
043. 
044.        '*** Sheet Data Rows ***'
045.        xlBook.Worksheets(1).Name = "MyReport"
046.        xlBook.Worksheets(1).Select()
047. 
048.        With xlBook.ActiveSheet.Cells(1, 1)
049.            .Value = "My Customer"
050.            .Font.Bold = True
051.            .Font.Name = "Tahoma"
052.            .Font.Size = 16
053.        End With
054. 
055.        With xlBook.ActiveSheet.Cells(2, 1)
056.            .Value = "Customer Name"
057.            .Font.Name = "Tahoma"
058.            .BORDERS.Weight = 1
059.            .Font.Size = 10
060.            .MergeCells = True
061.        End With
062. 
063.        With xlBook.ActiveSheet.Cells(2, 2)
064.            .Value = "Used"
065.            .BORDERS.Weight = 1
066.            .Font.Name = "Tahoma"
067.            .Font.Size = 10
068.            .MergeCells = True
069.        End With
070. 
071.        intStartRows = 3
072.        intEndRows = intStartRows + dt.Rows.Count - 1
073.        For i = 0 To dt.Rows.Count - 1
074.            xlBook.ActiveSheet.Cells(intStartRows + i, 1).Value = dt.Rows(i)("Name")
075.            xlBook.ActiveSheet.Cells(intStartRows + i, 2).Value = dt.Rows(i)("Used")
076.            xlBook.ActiveSheet.Cells(intStartRows + i, 2).NumberFormat = "$#,##0.00"
077.        Next
078. 
079.        '*** Creating Chart ***'
080.        xlBook.Charts.Add()
081.        xlBook.ActiveChart.ChartType = 54
082.        xlBook.ActiveChart.SetSourceData(xlBook.Sheets("MyReport").Range("A" & intStartRows & ":B" & intEndRows & ""))
083.        'xlBook.ActiveChart.SeriesCollection(1).Name = "Series1"
084.        'xlBook.ActiveChart.SeriesCollection(2).Name = "Series1"
085.        xlBook.ActiveChart.Location(2, "MyReport")
086. 
087.        '*** Sheet Properties ***'
088.        With xlBook.ActiveChart
089.            .HasTitle = True
090.            '.PlotBy = 1
091.            .HasAxis(1) = 1
092.            .ChartTitle.Text = "Customer Report"
093.            .ChartTitle.Characters.Text = "Customer Report"
094.            .ChartTitle.Font.Name = "Tahoma"
095.            .ChartTitle.Font.FontStyle = "Bold"
096.            .ChartTitle.Font.Size = 20
097.            .ChartTitle.Font.ColorIndex = 3
098.            .Axes(1, 1).HasTitle = True
099.            .Axes(1, 1).AxisTitle.Characters.Text = "X"
100.            .Axes(2, 1).HasTitle = True
101.            .Axes(2, 1).AxisTitle.Characters.Text = "Y"
102.            .HasDataTable = False '*** DataTable (False/True) ***'
103.        End With
104. 
105.        '*** Set Area ***'
106.        xlBook.ActiveSheet.Shapes("Chart 1").IncrementLeft(20)
107.        xlBook.ActiveSheet.Shapes("Chart 1").IncrementTop(-97.5)
108. 
109. 
110.        '*** Set Height & Width ***'
111.        xlBook.ActiveSheet.Shapes("Chart 1").ScaleHeight(1.0, 0, 0)
112.        xlBook.ActiveSheet.Shapes("Chart 1").ScaleWidth(1.0, 0, 0)
113. 
114.        '*** If Files Already Exist Delete files ***'
115.        Dim MyFile As New FileInfo(Server.MapPath(FileName))
116.        If MyFile.Exists Then
117.            MyFile.Delete()
118.        End IF
119.        MyFile = Nothing
120. 
121.        '*** Save Excel ***'
122.        'xlSheet.PrintOut 1 '*** Print to printer ***'
123.        xlSheet.SaveAs(Server.MapPath(FileName))
124.        xlApp.Quit()
125. 
126.        '*** Quit and Clear Object ***'
127.        xlSheet = Nothing
128.        xlBook = Nothing
129.        xlApp = Nothing
130. 
131.        '*** End Export To Excel ***'
132. 
133.        Me.lblText.Text = "Charts Created <a href=" & FileName & ">Click here</a> to Download."
134. 
135.        dtAdapter = Nothing
136.        objConn.Close()
137.        objConn = Nothing
138.    End Sub
139.</script>
140.<html>
141.<head>
142.<title>ThaiCreate.Com ASP.NET - Excel Application</title>
143.</head>
144.<body>
145.    <form id="form1" runat="server"
146.    <asp:Label id="lblText" runat="server"></asp:Label>
147.    </form>
148.</body>
149.</html>


Screenshot

ASP.NET Excel


สำหรับการ Save ในรูปแบบไฟล์ .JPG หรือ .GIF สามาถอ่านได้จากหัวข้อถัดไปครับ



   
Hate it
Don't like it
It's ok
Like it
Love it
Total Votes: 639Overall Rating: 4.1 / 5
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-10-26 21:23:14 / 2017-03-29 11:48:43
  Download : Download  ASP.NET Charts/Graph - Excel Application
 Sponsored Links / Related

 
ASP.NET Charts/Graph - Export to XML (Excel Application)
Rating :

 
ASP.NET Charts/Graph - Export to Gif,Jpg (Excel Application)
Rating :

 
ASP.NET Charts/Graph - Clustered Column 3D
Rating :

 
ASP.NET Charts/Graph - Exploded pie 3D
Rating :

 
ASP.NET Charts/Graph - Column Stacked
Rating :

 
ASP.NET Charts/Graph - Line Markers Stacked
Rating :

 
ASP.NET Charts/Graph - Cylinder Column Clustered
Rating :

 
ASP.NET Charts/Graph - Cylinderical Shape 3D
Rating :

 
ASP.NET Charts/Graph - Create Charts and Send Email Attachment
Rating :


ThaiCreate.Com Forum
Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่