2014-02-18 79 views
0

我的水晶報表出現問題,導出爲ex​​cel。如果您導出的文件已存在,是否有人可以幫助我如何編寫代碼或任何建議代碼?例如你導出lotinfo,接下來是lotinfo2,然後lotinfo3等,我的代碼總是導出單個文件和單個名稱。excel中的水晶報表導出

私人小組的button1_Click(BYVAL發件人爲System.Object的,BYVALË作爲System.EventArgs)把手Button1.Click

Try 
     Dim CrExportOptions As ExportOptions 
     Dim CrDiskFileDestinationOptions As New _ 
     DiskFileDestinationOptions() 
     Dim CrFormatTypeOptions As New ExcelFormatOptions 
     CrDiskFileDestinationOptions.DiskFileName = _ 
            "c:\Lot Enterprise Information.xls" 
     CrExportOptions = crypt.ExportOptions 
     With CrExportOptions 
      .ExportDestinationType = ExportDestinationType.DiskFile 
      .ExportFormatType = ExportFormatType.Excel 
      .DestinationOptions = CrDiskFileDestinationOptions 
      .FormatOptions = CrFormatTypeOptions 
     End With 
     crypt.Export() 
     MessageBox.Show("LOT ENTERPRISE INFORMATION IS SUCCESSFULLY EXPORTED!, LOCATED AT DRIVE C:", "PLEASE CHECK AT DRIVE C:", MessageBoxButtons.OK, MessageBoxIcon.Information) 
    Catch ex As Exception 
     MsgBox("Please Select Specific date to convert!") 
     'MsgBox(ex.ToString) 
    End Try 

End Sub 

回答

1

我一直在使用這個功能相當長的一段時間了。根據您的使用情況修復它。

Private Function fileExists(ByVal path As String, ByVal filename As String) As String 
    ' This function basically adds a counter when the file already exists 
    ' eg filename 
    ' filename(1) 
    ' filename(2) 

    Dim counter As Integer = 0 
    Dim orginialFileName As String = System.IO.Path.Combine(path, filename) 
    Dim newFileName = orginialFileName 
    Dim extension As String = "" 
    Dim testIndex As Integer = 0 
    While File.Exists(newFileName) 
     counter = counter + 1 
     extension = newFileName.Substring(newFileName.LastIndexOf(".")) 

     filename = filename.Substring(0, filename.LastIndexOf(".")) 
     testIndex = filename.LastIndexOf("(") 
     If testIndex <> -1 Then 
      filename = filename.Substring(0, testIndex) 
     End If 

     newFileName = String.Format("{0}({1})", System.IO.Path.Combine(path, filename), counter.ToString()) 
     filename = String.Format("{0}({1})", filename, counter.ToString()) 

     newFileName += extension 
     filename += extension 
    End While 

    Return filename 
End Function 

例如使用

Dim output as string 
output = fileExists("C:\test", "file.xls") 
MsgBox(output) 

This鏈接也可能對您有用。

編輯:

您可以將Try-Catch塊之前使用它

Dim fullPath As String = "C:\fileinfo.xls" 
    Dim directory, output, filename As String 
    If File.Exists(fullPath) Then 
     directory = fullPath.Substring(0, fullPath.LastIndexOf("\")) 
     filename = fullPath.Substring(fullPath.LastIndexOf("\") + 1) 
     output = fileExists(directory, filename) 
     fullPath = path.combine(directory,output) 
    End If 

然後改變這部分

CrDiskFileDestinationOptions.DiskFileName = _ 
            "c:\Lot Enterprise Information.xls" 

CrDiskFileDestinationOptions.DiskFileName = _ 
            fullPath 
+0

我試圖複製粘貼你的代碼,但有一個錯誤返回'文件'沒有聲明。對不起,我只是一個初學者。 T_T – user3221790

+0

把它放在代碼的最上面。 '進口System.IO' – Codemunkeee

+0

哦,這個文件我進口system.io,但仍然同樣的問題,沒有lotinfo 2 lotinfo 3 – user3221790