احتجت في أحد تطبيقات ASP.NET التي عملت عليها مؤخراً أن أقوم بتصدير البيانات الناتجة (وهي على شكل شبكة من البيانات grid) إلى صيغة إكسل، بحيث يتمكن المستخدم من تحميل ملف الإكسل ذلك إلى جهازه.
بعد قراءة وبحث عن أفضل طريقة لتوليد ملف إكسل، وجدت طريقة سهلة للغاية أحببت أن أشارككم بها:
في البداية أذكر بعض الطرق الأخرى التي قرأت عنها:
1- استخدام automation باستخدام مكتبة الإكسل وهي من نوع com وهذه الطريقة بطيئة وينتج عنها مشاكل عديدة لأنها تعمل من جهة السيرفر وتؤدي إلى تشغيل الإكسل في كل مرة على السيرفر....
2- استخدام كريستال ريبورت وأوامر التصدير المتحة فيه
أما عن الطريقة التي استعملتها فمبدؤها هو كما يلي:
قم بإنشاء ملف نصي على جهازك وسمه مثلاً c:\1.txt
ثم انسخ النص التالي والصقه داخل ذلك الملف:
<table border="1"> <tr> <td width="50%">hello excel</td> <td width="50%">hello excel</td> </tr> <tr> <td width="50%">hello excel</td> <td width="50%">hello excel</td> </tr> </table>
قم بعدها بحفظ الملف ثم تغيير اسمه إلى c:\1.xls
ثم اعمل عليه double click
لاحظ أنه تم فتح الملف بوساطة الإكسل بشكل طبيعي!
إذاً الفكرة هي في تحويل البيانات الجدولية لديك والتي تريد تصديرها إلى إكسل إلى صيغة جدول html ثم تخزنها في ملف نصي امتداده xls بكل بساطة.
أما عن كيفية تنفيذ الفكرة في صفحة aspx، فيمكن استخدام الكود التالي مثلاً:
Imports System
Imports System.Data
Imports System.Configuration
Public Class DataToExcel
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Convert(CType(Session("the_data_set"), DataSet), Response)
End Sub
Sub Convert(ByVal ds As System.Data.DataSet, ByVal response As System.Web.HttpResponse)
'first let's clean up the response.object
response.Clear()
response.Charset = ""
'set the response mime type for excel
response.ContentType = "application/vnd.ms-excel"
'create a string writer
Dim stringWrite As New System.IO.StringWriter
'create an htmltextwriter which uses the stringwriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
'instantiate a datagrid
Dim dg As New System.Web.UI.WebControls.DataGrid
'set the datagrid datasource to the dataset passed in
dg.DataSource = ds.Tables(0)
'bind the datagrid
dg.DataBind()
dg.ShowHeader = False
'tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite)
'all that's left is to output the html
response.Write(stringWrite.ToString)
response.End()
End Sub
End Classحيث تقوم بحفظ البيانات في dataset ثم تخزنها في session ثم تستدعي الملف الذي أرفقت كوده وهو DataToExcel.aspx فتظهر شاشة التحميل عند المستخدم.