توضیحات
بعد از برسی و خروجی گرفتن از GridView در فایل word با فرمت doc در این نمونه سورس تبدیل اطلاعات gridview به Excel را برسی میکنیم.پروژه به زبان سی شارپ که در محیط ASP.NET طراحی و برنامه نویسی شده است.برای ساخت دیتابیس نیز از کوئری زیر زیر استفاده کنید.
تبدیل اطلاعات gridview به Excel
CREATE TABLE [dbo].[Mspsoft]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NULL, [Family] [nvarchar](50) NULL, [Phone] [nvarchar](50) NULL, [Website] [nvarchar](50) NULL, CONSTRAINT [PK_Mspsoft] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET IDENTITY_INSERT [dbo].[Mspsoft] ON INSERT [dbo].[Mspsoft] ([ID], [Name], [Family], [Phone], [Website]) VALUES (1, N'مسعود', N'شریفی پور', N'09127746737', N'www.mspsoft.com') INSERT [dbo].[Mspsoft] ([ID], [Name], [Family], [Phone], [Website]) VALUES (2, N'پویا', N'قربانی', N'091200000', N'www.mspsoft.com') INSERT [dbo].[Mspsoft] ([ID], [Name], [Family], [Phone], [Website]) VALUES (3, N'علی', N'شفیعی', N'091222222', N'www.mspsoft.com') INSERT [dbo].[Mspsoft] ([ID], [Name], [Family], [Phone], [Website]) VALUES (4, N'عباس', N'ارجمند', N'0912222222', N'www.mspsoft.com') SET IDENTITY_INSERT [dbo].[Mspsoft] OFF USE [master] GO ALTER DATABASE [mspsoft.com] SET READ_WRITE GO
همچنین برای خروجی گرفتن در زبان Vb.NET میتوانید از کد زیر استفاده کنید.
Response.Clear() Response.Buffer = True Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls") Response.Charset = "" Response.ContentType = "application/vnd.ms-excel" Dim sw As New StringWriter() Dim hw As New HtmlTextWriter(sw) GridView1.AllowPaging = False GridView1.DataBind() 'Change the Header Row back to white color GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF") 'Apply style to Individual Cells GridView1.HeaderRow.Cells(0).Style.Add("background-color", "green") GridView1.HeaderRow.Cells(1).Style.Add("background-color", "green") GridView1.HeaderRow.Cells(2).Style.Add("background-color", "green") GridView1.HeaderRow.Cells(3).Style.Add("background-color", "green") For i As Integer = 0 To GridView1.Rows.Count - 1 Dim row As GridViewRow = GridView1.Rows(i) 'Change Color back to white row.BackColor = System.Drawing.Color.White 'Apply text style to each Row row.Attributes.Add("class", "textmode") 'Apply style to Individual Cells of Alternating Row If i Mod 2 <> 0 Then row.Cells(0).Style.Add("background-color", "#C2D69B") row.Cells(1).Style.Add("background-color", "#C2D69B") row.Cells(2).Style.Add("background-color", "#C2D69B") row.Cells(3).Style.Add("background-color", "#C2D69B") End If Next GridView1.RenderControl(hw) 'style to format numbers to string Dim style As String = " <style> .textmode { mso-number-format:@; } </style> " Response.Write(style) Response.Output.Write(sw.ToString()) Response.Flush() Response.End()
نقد و بررسی ها
هیچ دیدگاهی برای این محصول نوشته نشده است .