2011-07-14 22 views
2

私はEPPlusを経由して、Excel 2007ブックにいくつかの写真を追加しようとしているが、私は From.Columnを使用してFrom.Rowたときに発生するのxlsxファイルを開くときに、私はこのエラーを取得:EPPlus AddPicture関数は、Excel 2007の原因となるファイルの破損

enter image description here

ご協力いただきありがとうございます。あなたが使用

foreach (DataRow row in table.Rows) 
    { 
     for (int i = 0; i < row.ItemArray.Length; i++) 
     { 
      if (!row.ItemArray[i].ToString().Contains(';')) 
      { 
       wrksht.Cells[_rows, i + 1].Value = row.ItemArray[i]; 
      } 
      else 
      { 
       if (row.ItemArray[i].ToString().Split(';')[0] == "img") 
       { 
        if (File.Exists(row.ItemArray[i].ToString().Split(';')[1])) 
        { 
         System.Drawing.Image img = System.Drawing.Image.FromFile(row.ItemArray[i].ToString().Split(';')[1]); 
         OfficeOpenXml.Drawing.ExcelPicture pic = wrksht.Drawings.AddPicture(
           (_rows + i * new Random(_rows + i).Next()).ToString(), img 
           ); 
         pic.SetSize(img.Width + 5, img.Height + 5); 
         pic.From.Column = i + 1; 
         pic.From.Row = _rows; 
         pic.From.RowOff = ExcelHelper.Pixel2MTU(1); 
         pic.From.ColumnOff = ExcelHelper.Pixel2MTU(1); 
        } 
       } 
      } 
     } 
     _rows++; 
    } 

答えて

0
var img = Image.FromFile(row.ItemArray[i].ToString().Split(';')[1]); 
var pic = wrksht.Drawings.AddPicture((_rows + i * new Random(_rows + i).Next()).ToString(), img); 
//pic.SetSize(img.Width + 5, img.Height + 5); 
//pic.From.Column = i + 1; 
//pic.From.Row = _rows; 
//pic.From.RowOff = ExcelHelper.Pixel2MTU(1); 
//pic.From.ColumnOff = ExcelHelper.Pixel2MTU(1); 
pic.SetPosition(_rows-1, 0, i, 0); // always one less than actual value 
0

ライン:あなたがビットマップに画像を変換する場合は、問題が解決します

System.Drawing.Image img = System.Drawing.Image.FromFile(row.ItemArray[i].ToString().Split(';')[1]); 

は、ここに私のコードです。次のコードを試すことができます:

Bitmap newImage= new Bitmap(System.Drawing.Image.FromFile(row.ItemArray[i].ToString().Split(';')[1])); 

これで問題を解決できる場合があります。

関連する問題