2011-03-16 94 views

回答

12

下面是示例代碼:

 OpenFileDialog od = new OpenFileDialog(); 
     od.Filter = "XLS files|*.xls"; 
     od.Multiselect = true; 
     if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      string tempFolder = System.IO.Path.GetTempPath(); 

      foreach (string fileName in od.FileNames) 
      { 
       System.IO.File.Copy(fileName, tempFolder + @"\" + System.IO.Path.GetFileName(fileName)); 
      } 
     } 
+0

@AntonSemenov,謝謝你的迴應!這不會在Web應用程序和勝利表單應用程序中使用嗎?這裏沒有方法或selectFilesButton_Click方法。 – 2011-03-17 13:02:29

+0

@AntonSemenov,這個代碼很好,但並沒有給我一個完整的圖片。這段代碼在哪裏進入應用程序?在private void selectFilesButton_Click方法下?我需要編輯其他方法嗎? – 2011-03-18 13:15:36

+0

好的。創建新的Windows窗體應用程序項目並將按鈕放在窗體上。雙擊按鈕後,IDE將創建按鈕單擊事件處理程序。只需將我的答案中的代碼放到按鈕單擊事件處理程序中並運行項目 – 2011-03-18 16:52:14

8

OpenFileDialogMultiSelect屬性,您需要將其設置爲true以允許選擇多個文件。

從MSDN Here is a code example,其允許用戶選擇圖像的倍數數目,並顯示它們在圖片框窗體上的控件:

private void Form1_Load(object sender, EventArgs e) 
{ 
    InitializeOpenFileDialog(); 
} 

private void InitializeOpenFileDialog() 
{ 
    // Set the file dialog to filter for graphics files. 
    this.openFileDialog1.Filter = 
    "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + 
    "All files (*.*)|*.*"; 

    // Allow the user to select multiple images. 
    this.openFileDialog1.Multiselect = true; 
    this.openFileDialog1.Title = "My Image Browser"; 
} 

private void selectFilesButton_Click(object sender, EventArgs e) 
{ 
    DialogResult dr = this.openFileDialog1.ShowDialog(); 
    if (dr == System.Windows.Forms.DialogResult.OK) 
    { 
    // Read the files 
    foreach (String file in openFileDialog1.FileNames) 
    { 
     // Create a PictureBox. 
     try 
     { 
      PictureBox pb = new PictureBox(); 
      Image loadedImage = Image.FromFile(file); 
      pb.Height = loadedImage.Height; 
      pb.Width = loadedImage.Width; 
      pb.Image = loadedImage; 
      flowLayoutPanel1.Controls.Add(pb); 
     } 
     catch (SecurityException ex) 
     { 
      // The user lacks appropriate permissions to read files, discover paths, etc. 
      MessageBox.Show("Security error. Please contact your administrator for details.\n\n" + 
       "Error message: " + ex.Message + "\n\n" + 
       "Details (send to Support):\n\n" + ex.StackTrace 
      ); 
     } 
     catch (Exception ex) 
     { 
      // Could not load the image - probably related to Windows file system permissions. 
      MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\')) 
       + ". You may not have permission to read the file, or " + 
       "it may be corrupt.\n\nReported error: " + ex.Message); 
     } 
    } 
} 
+0

感謝您的迴應!該代碼示例允許用戶選擇多個圖像並將其顯示在Form的PictureBox控件中。如何將選定的xls文件複製到Consolidated文件夾並通過命令提示符命令[C:\ CommissionRecon \ ConvertExcel \ ConvertExcelTo.exe^xxxxx.xls^xxxx.csv] – 2011-03-17 12:58:10

+0

轉換爲.csv文件就像這樣工作:foreach(字符串fileName在od.FileNames){System.IO.File.Copy(文件名,consolidatedFolder + @「\」+ System.IO.Path.GetFileName(fileName)); } – 2011-03-17 13:39:20

+0

+1感謝您的幫助。使用if/else組合和try/catch組合有什麼區別?我把你的答案結合起來提出我的答案。請檢查我的答案,看看它是否看起來不錯。 – 2011-03-24 13:42:53

1

結合兩個答案,這是我想出的代碼:

  • 啓用用戶到選擇多個XLSX檔案(多選使用,打開文件對話框,this.OpenFileDialog屬性& FileBrowserDialog)
  • 選擇後製成,顯示在文本框選擇XLSX文件名(通過設置textBoxSourceFiles.Text值sourceFileOpenFileDialog.FileNames)
  • 複製所選文件到一個單獨的綜合目錄(使用foreach循環,System.IO.File.Copy,System.IO.Path.GetFileName,sourceFileOpenFileDialog.FileName)

    private void sourceFiles_Click(object sender, EventArgs e) 
    { 
        Stream myStream; 
        OpenFileDialog sourceFileOpenFileDialog = new OpenFileDialog(); 
    
        this.sourceFileOpenFileDialog.InitialDirectory = "i:\\CommissisionReconciliation\\Review\\"; 
        this.sourceFileOpenFileDialog.Filter = "Excel Files (*.xls;*.xlsx;)|*.xls;*.xlsx;|All Files (*.*)|*.*"; 
        this.sourceFileOpenFileDialog.FilterIndex = 2; 
        this.sourceFileOpenFileDialog.RestoreDirectory = true; 
        this.sourceFileOpenFileDialog.Multiselect = true; 
        this.sourceFileOpenFileDialog.Title = "Please Select Excel Source File(s) for Consolidation"; 
    
        if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK) 
        { 
         try 
         { 
          if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null) 
          { 
           using (myStream) 
           { 
            // populates text box with selected filenames 
            textBoxSourceFiles.Text = sourceFileOpenFileDialog.FileNames; 
           } 
          }  // ends if 
         }   // ends try 
    
         catch (Exception ex) 
         { 
          MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); 
         } 
        }    // ends if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK) 
    }     // ends public void sourceFiles_Click 
    
    private void consolidateButton_Execute_Click(object sender, EventArgs e) 
    { 
    
        string consolidatedFolder = targetFolderBrowserDialog.SelectedPath; 
    
         foreach (String file in sourceFileOpenFileDialog.FileNames) 
         { 
          try 
          { 
           // Copy each selected xlsx files into the specified TargetFolder 
    
           System.IO.File.Copy(sourceFileOpenFileDialog.FileName, consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName)); 
           Log("File" + sourceFileOpenFileDialog.FileName + " has been copied to " + consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName)); 
          } 
         }   // ends foreach loop 
    }     // ends void consolidateButton_Execute_Click 
    
+0

你想要文件只在鞏固按鈕不被點擊時複製因此file.copy操作必須在consolidationateButton_Execute_Click方法下移動:) – 2011-03-24 13:58:20

相關問題