Paste bitmap to Excel

F

Foo

When the following function is run by a Windows program it works just fine.
When it is run from an ASP.NET page the xlWorkSheet.PasteSpecial line
generates: "Exception from HRESULT: 0x800A03EC."

The page is set up with the aspcompat="true" page directive to avoid an
exception about the lack of a Single threaded apartment.
It is also run with impersonation to the same user that runs the Windows
executable.

Any ideas, or other ways to get a bitmap into the sheet?

using System;
using System.Reflection;
using System.Globalization;
using System.Windows.Forms;
using System.Drawing;
using Excel = Microsoft.Office.Interop.Excel;

public void PasteBitmap()
{
CultureInfo oC = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new
CultureInfo("en-US"); // To work around bug 320369

Excel.Application xl = new Excel.Application();
xl.Visible = false;
Excel.Workbook xlWorkBook = xl.Workbooks.Add(Missing.Value);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;

Image logo = Image.FromFile("D:\\Images\\logo.bmp");
Clipboard.SetDataObject(logo);
Excel.Range xlRange = (Excel.Range)xlWorkSheet.Cells[1,1];
xlRange.Select();
xlWorkSheet.PasteSpecial("Bitmap",false,false,string.Empty,0,string.Empty,true);
System.Runtime.InteropServices.Marshal.ReleaseComObject (xlRange);
System.Runtime.InteropServices.Marshal.ReleaseComObject (xlWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject (xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject (xl);
xlRange = null;
xlWorkSheet = null;
xlWorkBook = null;
xl = null;

System.Threading.Thread.CurrentThread.CurrentCulture = oC;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top