Lead the data out to excel(2)

ExpandedBlockStart.gif ContractedBlock.gif      Public   Shared   Function DownLoadExcelToClient() Function DownLoadExcelToClient(ByVal FileName As StringByVal TmpStr As StringAs Boolean
InBlock.gif        DownLoadExcelToClient 
= True
InBlock.gif
InBlock.gif        
Dim xlApp As Object
InBlock.gif        
Dim xlBook As Object
InBlock.gif        
Dim xlSheet As Object
InBlock.gif
InBlock.gif        
Try
InBlock.gif            xlApp 
= CreateObject("Excel.Application")
InBlock.gif            xlBook 
= xlApp.Workbooks.Add
InBlock.gif            xlSheet 
= xlBook.Worksheets(1)
InBlock.gif        
Catch ex As Exception
InBlock.gif            DownLoadExcelToClient 
= False
InBlock.gif            
Exit Function
InBlock.gif        
End Try
InBlock.gif
InBlock.gif        
Dim rowIndex As Integer = 0
InBlock.gif        
Dim colIndex As Integer = 0
InBlock.gif        
Dim iSheet As Integer, arrSheet As Array
InBlock.gif        
Dim Arr As Array, brr As Array
InBlock.gif        
Dim i As Integer, j As Integer
InBlock.gif
InBlock.gif        
If TmpStr <> "" Then
InBlock.gif            arrSheet 
= Split(TmpStr, SHEET_SPLITOR)
InBlock.gif            
For iSheet = 0 To UBound(arrSheet)
InBlock.gif                
If iSheet > 2 Then
InBlock.gif                    xlSheet 
= xlBook.Sheets.Add
InBlock.gif                
Else
InBlock.gif                    xlSheet 
= xlBook.Sheets(iSheet + 1)
InBlock.gif                
End If
InBlock.gif                rowIndex 
= 0
InBlock.gif                Arr 
= Split(arrSheet(iSheet), RECORD_SPLITOR)
InBlock.gif                
For i = 0 To UBound(Arr)
InBlock.gif                    rowIndex 
= rowIndex + 1
InBlock.gif                    colIndex 
= 0
InBlock.gif                    brr 
= Split(Arr(i), FIELD_SPLITOR)
InBlock.gif                    
For j = 0 To UBound(brr)
InBlock.gif                        colIndex 
= colIndex + 1
InBlock.gif                        xlSheet.Cells(rowIndex, colIndex).NumberFormatLocal 
= "@"
InBlock.gif                        xlSheet.Cells(rowIndex, colIndex) 
= brr(j)
InBlock.gif                    
Next
InBlock.gif                
Next
InBlock.gif            
Next
InBlock.gif        
End If
InBlock.gif
InBlock.gif        xlBook.Sheets(
1).Application.Visible = True   '置为可见
InBlock.gif

InBlock.gif
InBlock.gif        
Try
InBlock.gif            
'删除服务端临时文件: download.xls
InBlock.gif
            If File.Exists(FileName) = True Then
InBlock.gif                File.Delete(FileName)
InBlock.gif            
End If
InBlock.gif        
Catch ex As Exception
InBlock.gif            DownLoadExcelToClient 
= False
InBlock.gif            
Exit Function
InBlock.gif        
End Try
InBlock.gif
InBlock.gif        
'在服务端保存download.xls
InBlock.gif
        xlBook.SaveAs(FileName)
InBlock.gif        xlApp.Quit()
InBlock.gif
InBlock.gif        
'杀死Excel进程
InBlock.gif
        Dim myproc As System.Diagnostics.Process = New System.Diagnostics.Process
InBlock.gif        
Dim proc As Process
InBlock.gif        
Dim procs() As Process = Process.GetProcessesByName("excel")   '得到所有打开的进程
InBlock.gif
        Try
InBlock.gif            
For Each proc In procs
InBlock.gif                
If Not proc.CloseMainWindow() Then
InBlock.gif                    proc.Kill()
InBlock.gif                
End If
InBlock.gif            
Next
InBlock.gif        
Catch
InBlock.gif        
End Try
InBlock.gif
ExpandedBlockEnd.gif    
End Function

转载于:https://www.cnblogs.com/jiemupig/archive/2006/08/29/489047.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值