开发者

Convert xls to csv or kill EXCEL.exe process

开发者 https://www.devze.com 2023-03-24 01:29 出处:网络
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

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

Convert xls to csv or kill EXCEL.exe process

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, f开发者_如何学运维ileExcel 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


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.


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" :) ).

0

精彩评论

暂无评论...
验证码 换一张
取 消