将 xls 转换为 csv 或终止 EXCEL.exe 进程
我有一个应用程序,可以自动执行最初基于 DTS 包的报告系统。基于 Web 的应用程序当前正在将输出 xls 文件写入网络上的另一台服务器。我发现即使网站关闭后,仍有一个 excel 实例在正在写入的服务器上运行。我希望能够更改现有例程以输出 csv 文件而不是 xls 文件,或者终止联网计算机上的 EXCEL.exe 进程。请帮忙!
编辑:如果我更改文件扩展名以另存为 csv,则文件如下所示
如果该图片太小,它基本上显示行是分开的,但列不是。在应该用逗号分隔列的地方,有一些带有问号的小框,
这是我当前编写 .xls 文件的例程
Public Sub DisplayandConvertToXLS()
Dim i As Integer
Dim ds As New DataSet
Dim da As SqlDataAdapter
Dim objFileStream As FileStream
Dim objStreamWriter As StreamWriter
Dim fs As Object, myFile As Object
Dim cnn As SqlConnection = New SqlConnection("DataSource=dpsdb;InitialCatalog=productionservicereminder;User Id=id;Password=pass;")
Dim strLine, filePath, fileExcel As String
'Create a file name.
fileExcel = FileNameTxt.Text & ".xls"
'Set a virtual folder to save the file.
'Make sure that you change the application name to match your folder.
filePath = "\\10.1.2.4\SRS Shortcuts\Export\SQL Output\CSV Reports\"
fileName = filePath & FolderNameTXT.Text & "/" & fileExcel
'Use FileStream to create the .xls file.
objFileStream = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)
objStreamWriter = New StreamWriter(objFileStream)
'Use a DataReader to connect to the database.
cnn.Open()
Dim sql3 As String = DisplayQTXT.Text
Dim cmd As SqlCommand = New SqlCommand(sql3, cnn)
cmd.Parameters.Add(New SqlParameter("@ProgramGroupID", PgidTXT.Text))
Dim drr As SqlDataReader
drr = cmd.ExecuteReader()
'Enumerate the field names and records that are used to build the file.
For i = 0 To drr.FieldCount - 1
strLine = strLine & drr.GetName(i).ToString & Chr(9)
Next
'Write the field name information to file.
objStreamWriter.WriteLine(strLine)
'Reinitialize the string for data.
strLine = ""
'Enumerate the database that is used to populate the file.
While drr.Read()
For i = 0 To drr.FieldCount - 1
strLine = strLine & drr.GetValue(i) & Chr(9)
Next
objStreamWriter.WriteLine(strLine)
strLine = ""
End While
'Clean up.
drr.Close()
cnn.Close()
ds.Clear()
objStreamWriter.Close()
objFileStream.Close()
objStreamWriter.Dispose()
objFileStream.Dispose()
End Sub
I have an application which automates a reporting system originally based off DTS packages. The web based app is currently writing the output xls file to another server on the network. I am finding that an instance of excel is left running on the server being written to even after the website is closed. I would like to either be able to change my exiting routine to output a csv file rather than a xls file, or kill the EXCEL.exe process across networked machines. Help please!
EDIT: here is what the files looks like if i change the file extension to save as a csv
If that picture is too small it basically shows the rows are separated but the columns are not. Where the commas should be to separate the columns there are instead little boxes with question marks
here is my current routine for writing .xls files
Public Sub DisplayandConvertToXLS()
Dim i As Integer
Dim ds As New DataSet
Dim da As SqlDataAdapter
Dim objFileStream As FileStream
Dim objStreamWriter As StreamWriter
Dim fs As Object, myFile As Object
Dim cnn As SqlConnection = New SqlConnection("DataSource=dpsdb;InitialCatalog=productionservicereminder;User Id=id;Password=pass;")
Dim strLine, filePath, fileExcel As String
'Create a file name.
fileExcel = FileNameTxt.Text & ".xls"
'Set a virtual folder to save the file.
'Make sure that you change the application name to match your folder.
filePath = "\\10.1.2.4\SRS Shortcuts\Export\SQL Output\CSV Reports\"
fileName = filePath & FolderNameTXT.Text & "/" & fileExcel
'Use FileStream to create the .xls file.
objFileStream = New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write)
objStreamWriter = New StreamWriter(objFileStream)
'Use a DataReader to connect to the database.
cnn.Open()
Dim sql3 As String = DisplayQTXT.Text
Dim cmd As SqlCommand = New SqlCommand(sql3, cnn)
cmd.Parameters.Add(New SqlParameter("@ProgramGroupID", PgidTXT.Text))
Dim drr As SqlDataReader
drr = cmd.ExecuteReader()
'Enumerate the field names and records that are used to build the file.
For i = 0 To drr.FieldCount - 1
strLine = strLine & drr.GetName(i).ToString & Chr(9)
Next
'Write the field name information to file.
objStreamWriter.WriteLine(strLine)
'Reinitialize the string for data.
strLine = ""
'Enumerate the database that is used to populate the file.
While drr.Read()
For i = 0 To drr.FieldCount - 1
strLine = strLine & drr.GetValue(i) & Chr(9)
Next
objStreamWriter.WriteLine(strLine)
strLine = ""
End While
'Clean up.
drr.Close()
cnn.Close()
ds.Clear()
objStreamWriter.Close()
objFileStream.Close()
objStreamWriter.Dispose()
objFileStream.Dispose()
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您在示例中没有使用 Excel,并且您正在输出 .csv 文件。您只是保存扩展名为 .xls 的 .csv 文件。如果您确实要启动 Excel,则您不会在此代码中执行此操作。
You're not using excel in the example, as near as I can tell, and you are outputting a .csv file. You're just saving a .csv file with a .xls extension. If you're actually launching excel, you aren't doing it in this code.
这里的代码中没有任何内容与 Excel 有任何关系——它只是写出一个制表符分隔的文件,该文件恰好具有 .xls 扩展名。
您可以通过将“Chr(9)”更改为“,”来相当轻松地使其成为 CSV(尽管您还必须确保从输入数据中删除逗号或引用它,但请确保您引用您还可以在输入数据中转义引号...我想这比“相当容易”要少一点:))。
Nothing in your code here has anything to do with Excel -- It is just writing out a tab delimited file that happens to have a .xls extension.
You could fairly easily make this a CSV by changing the "Chr(9)" to "," (though you'll also have to make sure to either strip commas out of your input data or quote it, but make sure if you quote it that you also escape quotes in the input data... Guess this is a little less than "fairly easily" :) ).