ASP.Net 使用 System.Web.HttpResponse 生成并保存 CSV
您好,我正在使用以下代码生成包含数据集中数据的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 StreamWriter:
Use a StreamWriter: