How can I export a bitmap graphic to a spreadsheet?
never mind I found the answer
using System.Windows.Forms;using Infragistics.Excel;// Create a workbook with a worksheetWorkbook workbook = new Workbook();Worksheet worksheet1 = workbook.Worksheets.Add( "Sheet1" );// Create an image shape which can be placed on a worksheetImage image1 = Image.FromFile( "C:\\image1.bmp" );WorksheetImage imageShape1 = new WorksheetImage( image1 );// The top-left corner of the image shape will be in the middle of cell A1imageShape1.TopLeftCornerCell = worksheet1.Rows[ 0 ].Cells[ 0 ];imageShape1.TopLeftCornerPosition = new PointF( 50, 50 );// The bottom-right corner of the image shape will be in the middle of cell B2imageShape1.BottomRightCornerCell = worksheet1.Rows[ 1 ].Cells[ 1 ];imageShape1.BottomRightCornerPosition = new PointF( 50, 50 );// Create another image shape which can be placed on a worksheetImage image2 = Image.FromFile( "C:\\image2.bmp" );WorksheetImage imageShape2 = new WorksheetImage( image2 );// Get the bounds of cell C3Rectangle cellC3Bounds = worksheet1.Rows[ 2 ].Cells[ 2 ].GetBoundsInTwips();// Place the second image shape in the same bounds as cell C3imageShape2.SetBoundsInTwips( worksheet1, cellC3Bounds );// Create a group which will hold both imagesWorksheetShapeGroup group = new WorksheetShapeGroup();// Place both images in the groupgroup.Shapes.Add( imageShape1 );group.Shapes.Add( imageShape2 );// Place the group on the worksheetworksheet1.Shapes.Add( group );
I implemented like this, but on Save I get an error (object reference not set)
WorksheetImage wsImage = new WorksheetImage(Image.FromFile(Application.StartupPath + "\\Logo.png")); wsImage.PositioningMode = ShapePositioningMode.MoveWithCells; wsImage.TopLeftCornerCell = wsInfo.Rows[0].Cells[1]; wsImage.TopLeftCornerPosition = new PointF(90, 0); wsImage.BottomRightCornerCell = wsInfo.Rows[1].Cells[2]; wsImage.BottomRightCornerPosition = new PointF(0, 0); WorksheetShapeGroup group = new WorksheetShapeGroup(); group.Shapes.Add(wsImage); wsInfo.Shapes.Add(group); wb.Save(fileName);
Stack trace (we are using v12.1, pretty sure it worked with previous version):
- ex {"Object reference not set to an instance of an object."} System.Exception {System.NullReferenceException}+ [System.NullReferenceException] {"Object reference not set to an instance of an object."} System.NullReferenceException+ Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal} HelpLink null string+ InnerException null System.Exception Message "Object reference not set to an instance of an object." string Source "Infragistics2.Documents.Excel.v12.1" string StackTrace " at Infragistics.Documents.Excel.Serialization.BIFF8.EscherRecords.ClientData.Save(BIFF8WorkbookSerializationManager manager)\r\n at Infragistics.Documents.Excel.Serialization.BIFF8.EscherRecords.EscherRecordContainerBase.Save(BIFF8WorkbookSerializationManager manager)\r\n at Infragistics.Documents.Excel.Serialization.BIFF8.EscherRecords.EscherRecordContainerBase.Save(BIFF8WorkbookSerializationManager manager)\r\n at Infragistics.Documents.Excel.Serialization.BIFF8.EscherRecords.EscherRecordContainerBase.Save(BIFF8WorkbookSerializationManager manager)\r\n at Infragistics.Documents.Excel.Serialization.BIFF8.BiffRecords.MSODRAWINGRecord.Save(BIFF8WorkbookSerializationManager manager)\r\n at Infragistics.Documents.Excel.Serialization.BIFF8.BIFF8WorkbookSerializationManager.WriteRecord(BIFF8RecordType type)\r\n at Infragistics.Documents.Excel.Serialization.BIFF8.BIFF8WorkbookSerializationManager.WriteWorksheet(Worksheet worksheet, Boolean hasShapes)\r\n at Infragistics.Documents.Excel.Serialization.WorkbookSerializationManager.WriteWorksheetRecords(Worksheet worksheet, Boolean hasShapes)\r\n at Infragistics.Documents.Excel.Serialization.BIFF8.BIFF8WorkbookSerializationManager.SaveWorkbookContents(Boolean workbookHasShapes)\r\n at Infragistics.Documents.Excel.Serialization.WorkbookSerializationManager.Save()\r\n at Infragistics.Documents.Excel.Workbook.SaveBIFF8File(Stream stream)\r\n at Infragistics.Documents.Excel.Workbook.SaveHelper(Stream stream, IPackageFactory packageFactory)\r\n at Infragistics.Documents.Excel.Workbook.Save(String fileName, IPackageFactory packageFactory)\r\n at Infragistics.Documents.Excel.Workbook.Save(String fileName)\r\n at ABB.ITT.SharedGraphicalComponents.UserControls.QuickChecker.QuickChecker.exportToExcel(String fileName) in C:\\DevProjects\\ITT\\Main\\Source\\GraphicalComponents\\SharedGraphicalComponents\\UserControls\\QuickChecker\\QuickChecker.cs:line 455\r\n at ABB.ITT.SharedGraphicalComponents.UserControls.QuickChecker.QuickChecker.exportToExcel() in C:\\DevProjects\\ITT\\Main\\Source\\GraphicalComponents\\SharedGraphicalComponents\\UserControls\\QuickChecker\\QuickChecker.cs:line 336" string+ TargetSite {Void Save(Infragistics.Documents.Excel.Serialization.BIFF8.BIFF8WorkbookSerializationManager)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}+ Static members - Non-Public members + [System.NullReferenceException] {"Object reference not set to an instance of an object."} System.NullReferenceException _className null string+ _data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal} _dynamicMethods null object+ _exceptionMethod {Void Save(Infragistics.Documents.Excel.Serialization.BIFF8.BIFF8WorkbookSerializationManager)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo} _exceptionMethodString null string _helpURL null string _HResult -2147467261 int _innerException null System.Exception _message "Object reference not set to an instance of an object." string _remoteStackIndex 0 int _remoteStackTraceString null string _source "Infragistics2.Documents.Excel.v12.1" string+ _stackTrace {sbyte[384]} object {sbyte[]} _stackTraceString null string _xcode -1073741819 int _xptrs 115003372 System.IntPtr HResult -2147467261 int IsTransient false bool
Hello,
I created the attached sample using 12.1, and I do not get any errors. You might want to check that the image actually exists in the folder you're trying to load it from.
Please let me know if my sample helps to resolve this issue.
I have run into this same problem. I downloaded the sample you provided, changed the Format to WorkbookFormat.Excel97to2003, and I get the Null Reference Exception. I tried it with both 13.1 and 13.2. Any suggested workarounds?
Currie
Hello Currie,
Thank you for your response.
I also reproduced the issue when I made the modification you described. I have submitted this issue to our developers and I've created CAS-131957-S4Z5N0 to track the issue.
This issue is resolved in the latest service release for Infragistics 2013 Volume 2 as of the date of this post. It is also resolved in Infragistics 2014 Volume 1.