ASP.Net 使用 System.Web.HttpResponse 生成并保存 CSV

发布于 2024-12-02 18:18:55 字数 890 浏览 0 评论 0原文

您好,我正在使用以下代码生成包含数据集中数据的 CSV 文件。 CSV 生成正常并显示提示框,以便用户可以打开或保存 csv 文件。我想知道是否可以保存 CSV 文件?

感谢您

使用解决方案进行编辑的任何帮助(注意,我不需要打开提示框,因为我将编写额外的代码来生成和保存多个文件,然后显示它们的链接)。这会将数据集以 CSV 格式保存在我们的服务器上。

Protected Sub exportBtn_click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim ds1 As DataSet
    ds1 = csvdownload.ReturnNewPledges()
    Dim csv As New chartnew.csvhelper
    Dim strData1 As String = csv.Export(ds1, True)

    Dim todaydate As Date = Date.Now.Date
    Dim todaystr As String = todaydate.ToString("ddMMyyyy")
    Dim filename As String = todaystr & "_filename.csv"

    Dim saveDir As String = "writedir\files\"
    Dim appPath As String = Request.PhysicalApplicationPath
    Dim filePath As String = appPath + saveDir + filename

    Dim writer As New StreamWriter(filePath)
    writer.Write(strData1)
    writer.Close()

End Sub

Hi I'm using the following code to generate a CSV file containing data from a dataset. The CSV generates fine and a prompt box displays so that the user can either open or save the csv file. What I'm wondering is whether it's possible to save the CSV file?

Thanks for any help

Edited with solution (NB I don't need the prompt box to open as I'll be writing additional code to generate and save a number of files and then display links to them). This saves the dataset as a CSV on our server.

Protected Sub exportBtn_click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim ds1 As DataSet
    ds1 = csvdownload.ReturnNewPledges()
    Dim csv As New chartnew.csvhelper
    Dim strData1 As String = csv.Export(ds1, True)

    Dim todaydate As Date = Date.Now.Date
    Dim todaystr As String = todaydate.ToString("ddMMyyyy")
    Dim filename As String = todaystr & "_filename.csv"

    Dim saveDir As String = "writedir\files\"
    Dim appPath As String = Request.PhysicalApplicationPath
    Dim filePath As String = appPath + saveDir + filename

    Dim writer As New StreamWriter(filePath)
    writer.Write(strData1)
    writer.Close()

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗酒趁年少 2024-12-09 18:18:55

使用 StreamWriter:

    Dim SwFromFile As StreamWriter = New StreamWriter(fileName)
    SwFromFile.Write(strData1)
    SwFromFile.Flush()
    SwFromFile.Close()

Use a StreamWriter:

    Dim SwFromFile As StreamWriter = New StreamWriter(fileName)
    SwFromFile.Write(strData1)
    SwFromFile.Flush()
    SwFromFile.Close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文