2010-04-13 8 views
3

두 개의 열이있는 Excel 시트가 있으며 하나는 숫자이고 두 번째 열은 그림이 있습니다. Oledb 연결을 사용하여 C#에서이 데이터를 읽으려는 경우 숫자를 쉽게 읽을 수 있지만 그림은 두 번째 열에 포함되지 않으므로 C#에서는 첫 번째 열만 가져옵니다.OLEDB를 사용하여 Excel 파일에서 그림 가져 오기

지금 어떻게 이미지를 읽을 수 있습니까? 나는이 엑셀 시트에서 숫자와 관련 이미지를 추출하고 싶다.

답변

2

불가능합니다.

사진이 셀에 존재하지 않습니다. 을 셀에 넣고 이상의 셀에 배치 할 수 있으며, 셀에있는 것처럼 보이도록 크기를 조정할 수는 있지만 셀을 차지하는 것은 아닙니다.

OLEDB가 아닌 VBA 및 COM interop을 사용하여 워크 시트의 이미지 내용을 조작 할 수 있습니다.

5

이것은 오래된 항목이지만 지금까지 내가 사용한 코드 중 일부를 추가 할 것이라고 생각했습니다.

이 예제에서는 "pictureBox1"이라는 PictureBox를 배치 한 Windows 응용 프로그램이 있다고 가정합니다.

또한 Excel (Microsoft.Office.Interop.Excel)에 대한 참조를 추가한다고 가정합니다.

사진은 통합 문서에 바인딩되며 제이가 언급 한 것처럼 셀 자체에 포함되지 않습니다. TopLeftCell 및 BottomRightCell을 사용하여 이미지가 쉽게 이동해야하는 위치를 찾을 수 있습니다.

이제 우리는 문서의 모든 이미지를 가져 오기 위해 루프를 작성해야하지만, 나는 그 정보를 남겨 둘 것입니다. 이 복사되지 않은 작은 변화가 내 웹 응용 프로그램에서 나를 위해 큰

 string file = @"C:\sample.xlsx"; 

     if(System.IO.File.Exists(file)) 
     { 

      Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); 
      excelApp.Visible = true; //FOR TESTING ONLY 
      Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.Open(file, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing, 
           Type.Missing); 

      Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1]; //Selects the first sheet 
      Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, 1];  //Select cell A1 
      object cellValue = range.Value2; 

      #region Extract the image 
      Microsoft.Office.Interop.Excel.Picture pic = (Microsoft.Office.Interop.Excel.Picture)ws.Pictures(1); 

      if (pic != null) 
      { 
       //This code will detect what the region span of the image was 
       int startCol = (int)pic.TopLeftCell.Column; 
       int startRow = (int)pic.TopLeftCell.Row; 
       int endCol = (int)pic.BottomRightCell.Column; 
       int endRow = (int)pic.BottomRightCell.Row; 


       pic.CopyPicture(Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap); 
       if (Clipboard.ContainsImage()) 
       { 
        Image img = Clipboard.GetImage(); 
        this.pictureBox1.Image = img; 
       } 
      } 
      #endregion 

      //Close the workbook 
      wb.Close(false,Type.Missing,Type.Missing); 

      //Exit Excel 
      excelApp.Quit(); 
     } 
+0

또한 : 당신이 그것을 할 경우 작동합니다 winform, wpf. 클립 보드가 필요하기 때문에. (콘솔이 작동하지 않음) –

1

Nick's answer 작업은 이미지

Thread thread = new Thread(() => 
       { 
        foreach (var pic in ws.Pictures()) 
        { 
         if (pic != null) 
         { 
          //This code will detect what the region span of the image was 
          int startCol = pic.TopLeftCell.Column; 
          int startRow = pic.TopLeftCell.Row; 
          int endCol = pic.BottomRightCell.Column; 
          int endRow = pic.BottomRightCell.Row; 
          pic.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap); 
          if (Clipboard.GetDataObject() != null) 
          { 
           Image img = Clipboard.GetImage(); 
          } 
         } 
        } 
       }); 
       thread.SetApartmentState(ApartmentState.STA); 
       //Set the thread to STA 
       thread.Start(); 
       thread.Join(); 

작품을 클립 보드에 저

관련 문제