如何在vb.net 2010中导出到客户端系统上的excel。 [英] How to export to excel on client system in vb.net 2010.

查看:91
本文介绍了如何在vb.net 2010中导出到客户端系统上的excel。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我有一个Web应用程序,我需要将数据导出到客户端系统上的excel。我试过,但文件是在服务器而不是客户端系统上创建的。请帮帮我。

该应用程序在vb.net 2010和sql server 2012. office 2007.



问候,

Mohinder Singh

Dear,

I have a web application i need to export the data to the excel on the client system. I tried but the file gets created on the server instead of client system. Please help me.
The application is in vb.net 2010 and sql server 2012. office 2007.

Regards,
Mohinder Singh

推荐答案

当然它发生在服务器系统上,就像ASP.NET中的一切一样。能怎样?而你真的应该在服务器端做得更好。如果您提供URL和资源,客户端将能够直接从HTTP响应下载创建的文件或资源。 (再说一遍,怎么回事?)



在文件方法中,您只需在服务器上创建一个文件,并在某个HTML页面上生成带有指向该文件的URL的行:

Of course it happens on server system, as everything in ASP.NET. How else? And you really should better do it on the server side. The client will be able to download the file created, or the resource directly from the HTTP response, if you provide the URL and the resource for that. (Again, how else?)

In file approach, you just create a file on the server and generate the line with URL pointing to the file on some HTML page:
<a href="myDocument.xlsx">Download the document</a>





如果要避免创建文件,则应该能够将文档直接写入HTTP请求的网络流。首先,您需要生成适当的内容类型标题:

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.headers%28v=vs.110%29.aspx [ ^ ]。



标题应为application / vnd.ms-excel。您可以在这里找到大多数类型:

http://en.wikipedia.org/wiki/Content-type [ ^ ]。



这些内容类型在此标准化: http://www.iana.org/assignments/media-types [ ^ ]。



在所有情况下,都会给出用户并选择通过默认应用程序打开文件(如果安装了其他应用程序,它将起作用,这在许多平台上都是可行的,不仅仅是Windows ,比如,感谢Libre Office)或保存它(如果没有安装的应用程序可以使用excel文件,它将是唯一的版本。)



-SA



If you want to avoid creation of a file, you should be able to write the document directly to the network stream of your HTTP request. First of all, you need to generate appropriate content-type header:
http://msdn.microsoft.com/en-us/library/system.web.httpresponse.headers%28v=vs.110%29.aspx[^].

The headers should be ""application/vnd.ms-excel". You can find most types here:
http://en.wikipedia.org/wiki/Content-type[^].

Those content types are standardized here: http://www.iana.org/assignments/media-types[^].

In all cases, the user will be given and option to open the file by a default application (it will work if such other application is installed, which is possible on many platforms, not only Windows, say, thanks to Libre Office) or save it (it will be the only version if none of the installed applications can work with excel files).

—SA


您可以在ASP.net应用程序的系统中创建excel文件。你需要添加一些简单的代码步骤来创建一个excel文件。



更多细节请参考此链接



http://sharmanuj.blogspot.in/2010/06/create-excel-file-with-save-dialog-in.html
You can create excel file at system from ASP.net application. you need to add some simple steps of code to create an excel file.

for more details kindly refer this link

http://sharmanuj.blogspot.in/2010/06/create-excel-file-with-save-dialog-in.html


这是获得的解决方案不同行和单元格中的所有值。



Here is the solution to get all the values in different rows and cells.

Protected Sub GenerateXls(ByVal fileName As String, ByVal dtData As DataTable)
        Try
            HttpContext.Current.Response.Clear()
            HttpContext.Current.Response.ContentType = "application/ms-excel"
            HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName + "")
            HttpContext.Current.Response.Flush()
            Dim colCount, rowCount As Integer
            HttpContext.Current.Response.Write("<Table>")
            HttpContext.Current.Response.Write("<TR>")

            For colCount = 0 To dtData.Columns.Count - 1
                HttpContext.Current.Response.Write("<TD>")
                HttpContext.Current.Response.Write(dtData.Columns(colCount).ColumnName.ToString())
                'HttpContext.Current.Response.Write("\t")
                HttpContext.Current.Response.Write("</TD>")
            Next
            HttpContext.Current.Response.Write("</TR>")

            For rowCount = 0 To dtData.Rows.Count - 1
                'HttpContext.Current.Response.Write("\n")
                HttpContext.Current.Response.Write("<TR>")
                For colCount = 0 To dtData.Columns.Count - 1
                    HttpContext.Current.Response.Write("<TD>")
                    HttpContext.Current.Response.Write(dtData.Rows(rowCount)(colCount).ToString())
                    'HttpContext.Current.Response.Write("\t")
                    HttpContext.Current.Response.Write("</TD>")
                Next
                HttpContext.Current.Response.Write("</TR>")
            Next
            HttpContext.Current.Response.Write("</Table>")
            HttpContext.Current.Response.End()

        Catch ex As Exception

        End Try

    End Sub


这篇关于如何在vb.net 2010中导出到客户端系统上的excel。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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