将数据导出到Excel在vb.net [英] Exporting data to excel in vb.net

查看:299
本文介绍了将数据导出到Excel在vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法对我的数据导出到Excel中。结果
我曾尝试在S / O的建议,但没有任何运气。

I am unable to export my data into excel.
I have tried the suggestions on S/O, but have not had any luck.

        Dim sqlString As String = "spExportRateProfile" & Session("OfficeNumber") & "," & Session("SalesRepID")
        Dim conn As SqlConnection = New SqlConnection(Utils.GetConfigKey("ConnectionStringVimas"))
        conn.Open()
        Dim dt As DataTable = New DataTable()
        Dim da As SqlDataAdapter = New SqlDataAdapter(sqlString, conn)
        da.Fill(dt)

        Response.AddHeader("content-disposition", "attachment;filename=ReportExport.xlsx")
        Response.ContentType = "application/vnd.ms-excel"  

什么我需要在这之后做导出我的数据到excel?

What do I need do after this to export my data to excel?

推荐答案

您可以使用ExcelLibrary如 EPPlus (GPL),我诚心诚意推荐。

You could use a ExcelLibrary like EPPlus(GPL) which i can warmly recommend.

然后,它是那么容易,因为这从一个DataTable创建的Excel文件,并将其写入响应:

Then it is as easy as this to create Excel-Files from a DataTable and write it to the Response:

Dim pck = New ExcelPackage()
Dim ws = pck.Workbook.Worksheets.Add("Worksheet-Name")
ws.Cells("A1").LoadFromDataTable(dt, True, OfficeOpenXml.Table.TableStyles.Medium1)
Response.Clear()
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("content-disposition", "attachment;  filename=ExcelFileName.xlsx")
Response.BinaryWrite(pck.GetAsByteArray())

下面是另一个例子: HTTP://epplus.$c$cplex.com / Wiki页面?标题= WebapplicationExample

这篇关于将数据导出到Excel在vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆