2011-07-29 2 views
0

DTS 패키지를 기반으로 한보고 시스템을 자동화하는 응용 프로그램이 있습니다. 웹 기반 앱은 현재 출력 xls 파일을 네트워크의 다른 서버에 쓰고 있습니다. 웹 사이트를 닫은 후에도 서버 인스턴스에서 Excel 인스턴스가 실행 중임을 알게되었습니다. xls 파일 대신 csv 파일을 출력하는 내 기존 루틴을 변경하거나 네트워크로 연결된 컴퓨터에서 EXCEL.exe 프로세스를 종료 할 수 있습니다. 도와주세요!xls을 csv로 변환하거나 EXCEL.exe 프로세스를 죽입니다.

편집 : 여기에이 파일이 그 사진은 기본적으로 행이 분리되어 보여줍니다 너무 작은하지만 열이없는 경우 CSV enter image description here

로 저장하기 전 파일 확장자를 변경하면 모습입니다. 쉼표가 열을 구분하는 데 있어야 할 곳에 물음표 대신 작은 상자가있다

여기

이다 가깝게, 당신은 예에서 엑셀을 사용하지

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 

답변

1

.XLS 파일을 작성하기위한 내 현재의 일상 알 수 있듯이 .csv 파일을 출력하고 있습니다. 확장자가 .xls 인 .csv 파일을 저장하는 것입니다. 실제로 Excel을 실행하는 경우이 코드에서 수행하지 않습니다.

+0

기타 Chris가 옳습니다. 나는 ascii 뇌 손상이 있었다. – MNGwinn

+0

감사합니다. 그게 다른 사람이 나 한테도 말한거야. 하지만 파일 확장명을 CSV로 변경하면 파일이 모두 꺼집니다. 위의 편집을 참조하십시오. – JDV590

+0

@ JDV590 - 탭 문자를 표시 할 수없는 문자로 표시하는 Excel (또는 파일을보고있는 프로그램) 일 가능성이 높습니다. "텍스트 가져 오기 마법사"를 사용하여 Excel에서 파일을 열면 "탭"을 선택해야하는 구분 기호가 무엇인지 묻습니다. –

1

여기에 코드에 Excel과 아무런 관련이 없습니다. 이것은 확장자가 .xls 인 탭으로 구분 된 파일을 쓰는 것입니다.

"Chr (9)"을 ","로 변경하면 CSV를 쉽게 만들 수 있습니다 (하지만 입력 데이터에서 쉼표를 제거하거나 따옴표를 붙여야하지만, 물론 인용 부호를 사용하면 입력 데이터에서 따옴표를 벗어날 수 있습니다 ...이 값이 "상당히 쉽게"보다 작음을 알 수 있습니다 :)).

관련 문제