Public Shared Function DownLoadExcelToClient(ByVal FileName As StringByVal TmpStr As StringAs Boolean
        DownLoadExcelToClient 
= True

        
Dim xlApp As Object
        
Dim xlBook As Object
        
Dim xlSheet As Object

        
Try
            xlApp 
= CreateObject("Excel.Application")
            xlBook 
= xlApp.Workbooks.Add
            xlSheet 
= xlBook.Worksheets(1)
        
Catch ex As Exception
            DownLoadExcelToClient 
= False
            
Exit Function
        
End Try

        
Dim rowIndex As Integer = 0
        
Dim colIndex As Integer = 0
        
Dim iSheet As Integer, arrSheet As Array
        
Dim Arr As Array, brr As Array
        
Dim i As Integer, j As Integer

        
If TmpStr <> "" Then
            arrSheet 
= Split(TmpStr, SHEET_SPLITOR)
            
For iSheet = 0 To UBound(arrSheet)
                
If iSheet > 2 Then
                    xlSheet 
= xlBook.Sheets.Add
                
Else
                    xlSheet 
= xlBook.Sheets(iSheet + 1)
                
End If
                rowIndex 
= 0
                Arr 
= Split(arrSheet(iSheet), RECORD_SPLITOR)
                
For i = 0 To UBound(Arr)
                    rowIndex 
= rowIndex + 1
                    colIndex 
= 0
                    brr 
= Split(Arr(i), FIELD_SPLITOR)
                    
For j = 0 To UBound(brr)
                        colIndex 
= colIndex + 1
                        xlSheet.Cells(rowIndex, colIndex).NumberFormatLocal 
= "@"
                        xlSheet.Cells(rowIndex, colIndex) 
= brr(j)
                    
Next
                
Next
            
Next
        
End If

        xlBook.Sheets(
1).Application.Visible = True   '置为可见


        
Try
            
'删除服务端临时文件: download.xls
            If File.Exists(FileName) = True Then
                File.Delete(FileName)
            
End If
        
Catch ex As Exception
            DownLoadExcelToClient 
= False
            
Exit Function
        
End Try

        
'在服务端保存download.xls
        xlBook.SaveAs(FileName)
        xlApp.Quit()

        
'杀死Excel进程
        Dim myproc As System.Diagnostics.Process = New System.Diagnostics.Process
        
Dim proc As Process
        
Dim procs() As Process = Process.GetProcessesByName("excel")   '得到所有打开的进程
        Try
            
For Each proc In procs
                
If Not proc.CloseMainWindow() Then
                    proc.Kill()
                
End If
            
Next
        
Catch
        
End Try

    
End Function
posted on 2006-08-29 09:25  Liangyy  阅读(267)  评论(0编辑  收藏  举报