I am working on saving excel objects to a directory of my choosing. I am able to use a folder browser dialog to select the save location and the saveas method within the excel interop and i am able to create a file in the folder i select with the name i choose.
The issue is that when i later go to open that file it is blank. no workbooks no worksheets etc . Just a seemingly blank file in the proper location with the proper name.
It should be noted that if i single click on the report icon to open it the microsoft browser window does show a tiny preview of what seems like the correct report.
Im late binding because the end users are going to be using multiple versions of office and i was having an issue if i simply imported the interop i have locally from office 2010.
patient_frm.savefolder is simply a string that is the location of a user selected folder to save too. And i had to put "Anterior Postural Analysis.xls" in the mix so it would actually create an excel file.
The line of code seems to be working because when run it creates an excel file named Anterior Postural Analysis.xls in the specified folder. I just cant actually see the worksheet when i open the file.
I have altered the line of code so that it should be saving at the workbook level at this point. But i am still having the same issue. The file saves but when i open it it seems like it is blank. However if i look at the preview closely at the bottom of the window it has definately saved the output i am sending to the file. Its almost like it gets saved with the workbook hidden or invisible. Its wierd
I did some testing with this and I am pretty sure that the way i am saving it now is at the workbook level. I say that only because when i removed the .worksheets(1) from the line of code and replaced it with .workbook I reveived an error message stating that workbook is not a member of workbook.
This leads me to believe that in this case using eapp.saveas is saving it at the workbook level.
When you are using Automation to edit an Excel workbook, keep the following in mind.
Creating a new instance of Excel and opening a workbook results in an invisible instance of Excel and a hidden instance of the workbook. Therefore, if you edit the workbook and save it, the workbook is saved as hidden. The next time the user opens Excel manually, the workbook is invisible and the user has to click Unhide on the Window menu to view the workbook.
To avoid this behavior, your Automation code should unhide the workbook before editing it and saving it. Note that this does not mean Microsoft® Excel itself has to be visible.
This is why sometimes in cases like this a better option is using a third party library. I use Aspose Cells which a) does not require Excel installed to create/edit workbooks b) will create new files hidden but when viewed are not hidden in regards to worksheets c) extremely easy to use even for complex operations.
I did some testing with this and I am pretty sure that the way i am saving it now is at the workbook level. I say that only because when i removed the .worksheets(1) from the line of code and replaced it with .workbook I reveived an error message stating that workbook is not a member of workbook.
This leads me to believe that in this case using eapp.saveas is saving it at the workbook level.
1,3 and 6 were all at the worksheet level and did not notice you changed it as I was focused on getting you to change it and you did.
So problem number one is that as soon as i import Aspose.cells into my solution i now have an error with this function.
It is telling me that type drawing.bitmap is not defined. This has always worked in the past but now it seems to be conflicting with aspose.
Code:
Private Function SaveImage(ByVal name As String)
Dim bmp As New Drawing.Bitmap(xray.Image.Width, xray.Image.Height)
xray.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
bmp.Save("c:\Chiro Report\" & name)
Return 0
End Function
I use the following (good once you register Cells) to initialize the library for the paid version. Note the last section DesignerFile which points to a predefined template that I simply have Aspose bookmarkers to populate data. Designer files are way cool.
Code:
Module AsposeSupport
Private Const LicenseFile As String = "Aspose.Cells.lic"
<System.Diagnostics.DebuggerStepThrough()> _
Public Function PrepareCellsLibrary() As Boolean
Dim Sections As New AppSections(frmMainForm.ConfigurationFile)
Dim Result As Boolean = False
If IO.File.Exists(LicenseFile) Then
Dim license As Aspose.Cells.License = New Aspose.Cells.License
license.SetLicense(LicenseFile)
Result = True
End If
If Sections.DefaultEnvironment.Equals("Production") Then
If Not Result Then
frmMainForm.AsposeLicFileAvailable = False
End If
ElseIf Sections.DefaultEnvironment.Equals("Development") Then
frmMainForm.AsposeLicFileAvailable = Result
ElseIf Sections.DefaultEnvironment.Equals("QAC") Then
frmMainForm.AsposeLicFileAvailable = Result
End If
Return Result
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Public Function DesignerFile(ByVal TemplateName As String) As String
Return (From T In XDocument.Load(Application.StartupPath & "\Excel\AsposeSettings.xml")...<Report> _
Where T.<Name>.Value = TemplateName _
Select T.<Template>.Value).First
End Function
End Module
The problem is that where i had the issue actually had nothing to do with excel or aspose. The code i reference below is used simply to flatten and save the image in the picturebox to a .bmp file.
Im betting that the term Drawing is used within the Aspose methods and thats what was screwing me up there.
Code:
Private Function SaveImage(ByVal name As String)
Dim bmp As New Drawing.Bitmap(xray.Image.Width, xray.Image.Height)
xray.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
bmp.Save("c:\Chiro Report\" & name)
Return 0
End Function
The attached demo (VS2008, Option Strict On) shows adding data to a cell, adding a image directly below the cell and finally saves the file. The save operation uses an enumeration so you can control the versioning.
In regards to the image issue, see the last bit of code which I compiled w/o issues pointing to a PictureBox as I do not have you class.
Note PrepareCellsLibrary code is only for when you have a paid version of Cells but does no harm if you keep it in the project.
Code:
Public Class frmDemo
Dim FileName As String = IO.Path.Combine(Application.StartupPath, "Test1.xls")
Private Sub cmdCloseForm_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdCloseForm.Click
Close()
End Sub
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim Book As Workbook = New Workbook()
Dim Sheet As Worksheet = Book.Worksheets(0)
Dim styles As Styles = Book.Styles
Dim styleInvoice As Style = GetStyleInvoice(styles)
Sheet.Name = "Just Created"
Sheet.Cells("B4").PutValue("My simple image")
Sheet.Cells("B4").Style = styleInvoice
Sheet.Pictures.Add(4, 1, IO.Path.Combine(Application.StartupPath, "AllLabels.bmp"))
'
' You could easily make this dynamic in regards to the versioning
'
Book.Save(FileName, FileFormatType.Excel2003)
End Sub
Private Shared Function GetStyleInvoice(ByVal styles As Styles) As Style
Dim styleIndex As Integer = styles.Add()
Dim styleInvoice As Style = styles(styleIndex)
styleInvoice.Font.Color = Color.Blue
styleInvoice.Font.IsBold = True
styleInvoice.Font.IsItalic = True
styleInvoice.ForegroundColor = Color.LightGray
styleInvoice.Pattern = BackgroundType.Solid
Return styleInvoice
End Function
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
PrepareCellsLibrary()
End Sub
Private Function SaveImage(ByVal name As String) As Integer
Dim bmp As New Drawing.Bitmap(PictureBox1.Image.Width, PictureBox1.Image.Height)
PictureBox1.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
bmp.Save("c:\Chiro Report\" & name)
Return 0
End Function
End Class
I noticed you are note indicating the type returned from your function which means you have option Strict set to Off (my guess is for the late binding). You should make sure all code that does not require late binding to be compiled with Option Strict On but placing said code into another module and place the statement Option Strict On at the top of the module.
Originally Posted by Deslyxia
The problem is that where i had the issue actually had nothing to do with excel or aspose. The code i reference below is used simply to flatten and save the image in the picturebox to a .bmp file.
Im betting that the term Drawing is used within the Aspose methods and thats what was screwing me up there.
Code:
Private Function SaveImage(ByVal name As String)
Dim bmp As New Drawing.Bitmap(xray.Image.Width, xray.Image.Height)
xray.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
bmp.Save("c:\Chiro Report\" & name)
Return 0
End Function