Update a table linked to a PowerPivot datamodel using EPPlus and it corrupts the datamodel - vb.net

Using EPPlus I read an XLSX file.
I replace the data in a table and set the table range.
When I open the resulting spreadsheet I get an error:
"We found a problem with some content in 'MySpreadsheet.xlsx'. Do you want us to try to recover as much as we can?" -- I click Yes and I get another error:
"Excel was able to open the file by repairing or removing the unreadable content. Removed Part: Data store"
The error only happens after I add this table to a PowerPivot data model.
[EDIT] - I created a win forms app that reproduces this problem. You
can download it at here
I found the problem but don't know how
to fix it.
rename the xlsx to zip
Open the zip and browse to the xl\workbook.xml file
Look for the node collection.
Notice how EPPlus changes the <definedNames> collection to use absolute cell addresses.
Excel: <definedName name="_xlcn.LinkedTable_MyDate" hidden="1">MyDate[]</definedName>
EPPlus: <definedName name="_xlcn.LinkedTable_MyDate" hidden="1">'MyDate'!$A$2:$A$5</definedName>
If I modify this line after EPPlus is done saving then I can pull it
up in Excel without corrupting the Data Model.
I tried changing the WorkbookXml but it is happening when the
ExcelPackage.Save method runs.
For Each node In pck.Workbook.WorkbookXml.GetElementsByTagName("definedNames")(0).ChildNodes
node.innerText = "MyDate[]"
Next
Any ideas?
Try this first: create a spreadsheet with one table in it. Name the worksheet and table "DateList". Save it and run the below code on it -- it will work.
Then do this: open the same spreadsheet and add the DateList table to a pivottable data model. Save it and run the below code on it -- it will fail.
Here's some code from my MVC Controller -- only the relevant bits:
Public Class ScorecardProgressReportDatesVM
Public Property WeekRange As Date
End Class
Public Function GetScorecardProgressReport(id As Integer) As ActionResult
Dim contentType As String = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Dim DateList As New List(Of ScorecardProgressReportDatesVM)
DateList.Add(New ScorecardProgressReportDatesVM With {.WeekRange = CDate("Jan 1, 2015")})
DateList.Add(New ScorecardProgressReportDatesVM With {.WeekRange = CDate("Jan 1, 2015")})
Dim templateFile As New IO.FileInfo("c:\test.xlsx")
Dim ms As New IO.MemoryStream
Using pck As New ExcelPackage(templateFile)
ExtendTable(pck, "DateList", DateList)
pck.SaveAs(ms)
ms.Position = 0
End Using
Dim fsr = New FileStreamResult(ms, contentType)
fsr.FileDownloadName = "StipProgress.xlsx"
Return fsr
End Function
Private Sub ExtendTable(package As ExcelPackage, tableName As String, newList As Object)
Dim ws As OfficeOpenXml.ExcelWorksheet
ws = package.Workbook.Worksheets(tableName)
Dim OutRange = ws.Cells("A1").LoadFromCollection(newList, True)
Dim t = ws.Tables(tableName)
Dim te = t.TableXml.DocumentElement
Dim newRange = String.Format("{0}:{1}", t.Address.Start.Address, OutRange.End.Address)
te.Attributes("ref").Value = newRange
te("autoFilter").Attributes("ref").Value = newRange
End Sub

In order to fix this we had to change the EPPlus v4.04 source code.
ExcelWorkbook.cs # line 975 was changed
//elem.InnerText = name.FullAddressAbsolute; This was causing issues with power pivot
to
elem.InnerText = name.FullAddress; //Changed to full address... so far everything is working

Related

'A generic error occurred in GDI+.' saving image to anywhere not Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

Hoping someone here can help me. I've read the many other similar questions on StackOverflow but haven't found an answer that works for me...
I have code that exports data into an excel report. Part of that code copies an image and places it into a folder just created on the desktop. Then also inserts that image into the excel report. There are two different ways data is exported. Users can export one item at a time, or an entire program of items. When exporting one Item a folder is created on the Desktop with the ItemID, and the image is saved in that folder. When exporting a program a folder is craeted on the desktop for the Program, another folder is created inside that folder for vendor, and anther inside the vendor forlder for the item; Then the image is saved in that item folder. The code works just fine When exporting only an item, but gives an error...
A generic error occurred in GDI+
...on the first image save when exporting a program.
The code to Call the method when exporting one Item is...
Rprt_ItemRFQ_Class.ExportRFQ(Itm_ItemIDCmb.Text, ItmKey, Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), ProgKey)
The Code to Export a group of Items is...
Dim SaveFold As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), ProgName, VendName)
My.Computer.FileSystem.CreateDirectory(SaveFold)
For Each RFQRow As DataRow In DS.RFQRequestForm.Rows()
Rprt_ItemRFQ_Class.ExportRFQ(RFQRow.Item("ItemID"), RFQRow.Item("ItmKey"), SaveFold, ProgKey)
Next
The Code being called is... (SaveFold is a class variable)
Public Shared Sub ExportRFQ(ItemID As String, ItmKey As Integer, StartPath As String, ProgKey As Integer)
SaveFold = IO.Path.Combine(StartPath, ItemID)
My.Computer.FileSystem.CreateDirectory(SaveFold)
"I have cut out all the code for creating and formatting the excel sheet as it is not relevent"
ImgLoad(ItmKey, ItemID)
End Sub
Public Shared Sub ImgLoad(ItmKey As Integer, ItemID As String)
GetPrimImg.Fill(DS.GetPrimImage, ItmKey)
Dim FileStr As String = DS.GetPrimImage.Rows(0).Item(0)
Dim NewPath As String = IO.Path.Combine(SaveFold, ItemID & ".jpg")
Dim fs As IO.FileStream
fs = New IO.FileStream(FileStr, IO.FileMode.Open, IO.FileAccess.ReadWrite)
Dim RFQImg As System.Drawing.Image = System.Drawing.Image.FromStream(fs)
RFQImg.Save(NewPath)
Dim Wid As Integer = IIf(RFQImg.Width = 450, RFQImg.Width * 0.53333, RFQImg.Width * 0.24)
Dim Hgt As Integer = IIf(RFQImg.Height = 450, RFQImg.Height * 0.53333, RFQImg.Height * 0.24)
RFQImg.Dispose()
fs.Close()
xlWorksheet.Range("T1").Select()
Dim P As Object = xlWorksheet.Pictures.Insert(NewPath)
With P
.Left = xlWorksheet.Range("T1").Left
.Top = xlWorksheet.Range("T1").Top
.Placement = ExcelVB.XlPlacement.xlMoveAndSize
.PrintObject = True
With P.ShapeRange
.LockAspectRatio = False
.Width = Wid
.Height = Hgt
.IncrementTop(3.333228)
.IncrementLeft(102.1872441)
End With
End With
End Sub
The Error Occurs on the line
RFQImg.Save(NewPath)
I have tried modifying what gets passed in as SaveFold from the program export, it works just fine if I pass in Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), but any other file path I pass in causes the error.
Ok, I'm kind of an idiot here. Someone saved the vendor name which was the VendName variable with a space character at the end. That is why I was getting the error. I added a trim to the variables and all is good now.

How do I copy a file to the clipboard and paste it somewhere else?

I have a listview with small thumbnails of images.
Each image has a tag with it's full path in it.
With a rightclick menu the user can click COPY.
Then this code is excecuted:
Dim selectedfile As String
selectedfile = Me.lvFotos.SelectedItems(0).Tag
Dim dataobj As New DataObject(DataFormats.FileDrop, selectedfile)
Clipboard.Clear()
Clipboard.SetDataObject(dataobj)
Now when I click on my desktop to paste the file I get an exception error in VS2010:
An exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Windows.Forms.dll and wasn't handled before a managed/native boundary
Additional information: Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC))
What am I doing wrong here?
rg.
Eric
You could use directly My.Computer.FileSystem.CopyFile.
Dim source As String = lvFotos.SelectedItems(0).Tag
Dim destination As String = My.Computer.FileSystem.SpecialDirectories.Desktop & from.Substring(from.LastIndexOf("\"))
My.Computer.FileSystem.CopyFile(source, destination)
Using the code from John Smith at Copying a File To The Clipboard:
Dim f() As String = {"C:\temp\Folder.jpg"}
Dim d As New DataObject(DataFormats.FileDrop, f)
Clipboard.SetDataObject(d, True)
(Tested as working in VS2013 on Windows 7 x64.)
Note that you have to pass an array of strings representing your filename(s), so you could allow the user to gather several items before pasting, if you wanted to.
The true in Clipboard.SetDataObject allows the data to remain on the clipboard when you exit the program, so if the user were to select a file and exit before pasting, they would not have lost their selection.
Found what I was doing wrong.
At first I had tried it with the name of the file in an array, but that gave the same error.
Now I have it like this:
Dim selectedfile(0) As String
selectedfile(0) = Me.lvFotos.SelectedItems(0).Tag
Dim dataobj As New DataObject
dataobj.SetData(DataFormats.FileDrop, True, selectedfile)
Clipboard.Clear()
Clipboard.SetDataObject(dataobj, True)
The difference is in the line with SETDATA.
By setting the second argument to TRUE in SetData and also in the SetDataObject, it started to work.

Remove sas content from Excel with VBA (MS Office Add-in)

I've figured out how to use the MS Office Add-in to run a SAS Stored Process from within Excel (using an input stream) to obtain outputs.
Everytime I run this, new columns are inserted to accommodate the new output. Also, additional content is added to the workbook.
My understanding of the SAS Add-in object model is poor, so I was hoping someone could help me:
1) Remove the existing content with VBA before running the process again; or
2) Write VBA code to refresh existing content only (within the same output range).
The code I'm using is:
Sub sasTests()
Application.ScreenUpdating = False
Dim sas As SASExcelAddIn
Set sas = Application.COMAddIns.Item("SAS.ExcelAddIn").Object
Dim inputStream As SASRanges
Set inputStream = New SASRanges
inputStream.Add "Prompts", Worksheets("sasInput").Range("sasInput")
sas.InsertStoredProcess "/Shared Data/C139/sasTests", _
Worksheets("sasOutput").Range("A1"), , , inputStream
End Sub
Many thanks!
PS. If anyone has any handy references for the SAS Add-in for MS Office, please provide links
You could clear the range containing the existing content with the .Clear method. If this range were in columns M:P, you could use the following line:
sheetNameHere.Range("M:P").Clear
If you added that line after setting
Application.ScreenUpdating = False
I believe this would imitate a "refresh" of the data.
This will remove all old components from the workbook. I am not sure how to refresh all but assuming its similar:
Sub DeleteSasContent()Dim sas As SASExcelAddIn
Set sas = Application.COMAddIns.Item("sas.exceladdin").Object
Dim stpList As SASStoredProcesses
Dim dataList As SASDataViews
Dim pivotList As SASPivotTables
Dim reportList As SASReports
Set stpList = sas.GetStoredProcesses(ThisWorkbook)
Set dataList = sas.GetDataViews(ThisWorkbook)
Set pivotList = sas.GetPivotTables(ThisWorkbook)
Set reportList = sas.GetReports(ThisWorkbook)
For i = 1 To stpList.Count
stpList.Item(i).Delete
Next i
For i = 1 To dataList.Count
dataList.Item(i).Delete
Next i
For i = 1 To pivotList.Count
pivotList.Item(i).Delete
Next i
For i = 1 To reportList.Count
reportList.Item(i).Delete
Next i
End Sub
See the SAS help here http://support.sas.com/kb/45/606.html

Parse text file and create an excel report

My application is supposed to parse a text file (relatively easy) and create an excel spreadsheet report.
Should I write a stand alone VB.NET application that saves the excel file, or should I use VSTO? I am unsure if there are any differences in terms of ease of development, usability issues, API functions available, etc.
Are there any other programming languages/interfaces/libraries that will allow me to rapidly develop an involved excel spreadsheet? I am talking about things like functions, graphs, etc.
Thank you
You can do this very easily by taking advantage of excel 2007 format (.xlsx)
Here is what I did, you can modify it pretty easily. Im basically taking advantage that the xlsx file is really just a zip file containing xml files.
I created an empty excel file called Empty.xlsx and added it to my application as a resource. (build action embedded resource)
I'm also using a library for standard zip and unzip since that is how you get at the parts of the excel file.
Here is how i take a datatable and create the excel file.. notice that Excel is not actually needed.
Private Function CreateExcelReport(ByVal FilePath As String, ByVal tbl As DataTable) As FileInfo
'Just loading the excel file from the assembly, you could do it from a file also
Dim _assembly As Assembly = Assembly.GetExecutingAssembly
Dim xlStream As New StreamReader(_assembly.GetManifestResourceStream("YourAssembly.Empty.xlsx"))
'Create a new fileinfo that will hold the outputed excel file with the data.
Dim fiRet As New FileInfo(FilePath)
'Im using Ionic Zip Reduced free library to break the slsx file into its subparts
Using z As ZipFile = ZipFile.Read(xlStream.BaseStream)
'Grab Sheet 1 out of the file parts and read it into a string.
Dim myEntry As ZipEntry = z("xl/worksheets/sheet1.xml")
Dim msSheet1 As New MemoryStream
myEntry.Extract(msSheet1)
msSheet1.Position = 0
Dim sr As New StreamReader(msSheet1)
Dim strXMLData As String = sr.ReadToEnd
'Grab the data in the empty sheet and swap out the data that I want
Dim str2 As XElement = CreateSheetData(tbl)
Dim strReplace As String = strXMLData.Replace("<sheetData/>", str2.ToString)
z.UpdateEntry("xl/worksheets/sheet1.xml", strReplace)
'This just rezips the file with the new data it doesnt save to disk
z.Save(fiRet.FullName)
End Using
'Return a Fileinfo class to be saved to disk or DB or streamed to browser
Return fiRet
End Function
Private Function CreateSheetData(ByVal dt As DataTable) As XElement
Dim sheedata As XElement = <sheetData></sheetData>
'Create Header Rows
Dim HeaderRow As New XElement(<row></row>)
For j = 0 To dt.Columns.Count - 1
Dim c As New XElement(<c t="inlineStr"></c>)
Dim _is As New XElement(<is></is>)
Dim v As New XElement(<t></t>)
v.Add(dt.Columns(j).ColumnName)
_is.Add(v)
c.Add(_is)
HeaderRow.Add(c)
Next
sheedata.Add(HeaderRow)
'Create row for each datarow
For Each dr As DataRow In dt.Rows
Dim newrow As New XElement(<row></row>)
For j = 0 To dt.Columns.Count - 1
Dim c As New XElement(<c t="inlineStr"></c>)
Dim _is As New XElement(<is></is>)
Dim v As New XElement(<t></t>)
v.Add(dr(j).ToString)
_is.Add(v)
c.Add(_is)
newrow.Add(c)
Next
sheedata.Add(newrow)
Next
Return sheedata
End Function
As far as in what form you develop this application, it's really up to you. I would base it on what kind of deployment you would like to use.
If you choose to do it outside of excel, then I would look into EPPlus.
For commercial libraries, you should look at Aspose Cells and SpreadsheetGear.
It's been a while since I've used either, but I recall that the Aspose approach is focused on calling into their libraries from code, and does not require Excel, while SpreadsheetGear is somewhat more template-driven.

How to do Mailmerge in Openoffice using Vb.net

Its 5th Question and apart of one I didn't get response from the experts....
Hope this time I will get the helping hand.
I want to do mailmerge in openoffice using Vb.net and I am totally new with openoffice.
I searched on net for some help to understand how to use openoffice with vb.net but all I get is half info.....So can you please help me and give me code for mailmerge in vb.net for openoffice.
Well i have list of workers in DB and there is this facility that if they want to mail to all or some of the workers then they can do it.I have completed this task using Microsoft Office now as a Add in we are providing the facility to perform the same task using Open Office.
What they have to do is just select the List of workers and click on a button and it will automate the mailmerge using the field of those workers data from DB. The Code of mine is as shown below
Public Sub OpenOfficeMail(ByVal StrFilter As String)
Dim oSM ''Root object for accessing OpenOffice from VB
Dim oDesk, oDoc As Object ''First objects from the API
Dim arg(-1) ''Ignore it for the moment !
''Instanciate OOo : this line is mandatory with VB for OOo API
oSM = CreateObject("com.sun.star.ServiceManager")
''Create the first and most important service
oDesk = oSM.createInstance("com.sun.star.frame.Desktop")
''Create a new doc
oDoc = oDesk.loadComponentFromURL("private:factory/swriter", "_blank", 0, arg)
''Close the doc
oDoc.Close(True)
oDoc = Nothing
''Open an existing doc (pay attention to the syntax for first argument)
oDoc = oDesk.loadComponentFromURL("file:///C:\Users\Savan\Documents\1.odt", "_blank", 0, arg)
Dim t_OOo As Type
t_OOo = Type.GetTypeFromProgID("com.sun.star.ServiceManager")
Dim objServiceManager As New Object
objServiceManager = System.Activator.CreateInstance(t_OOo)
Dim oMailMerge As New Object
oMailMerge = t_OOo.InvokeMember("createInstance", Reflection.BindingFlags.InvokeMethod, Nothing, _
objServiceManager, New [Object]() {"com.sun.star.text.MailMerge"}) 'com.sun.star.text.MailMerge"})
oMailMerge.DocumentURL = "file:///C:\Users\Savan\Documents\1.odt"
oMailMerge.DataSourceName = CreateSource(StrFilter)''Function that will return the datasource name which will be a text file's path
oMailMerge.CommandType = 0
oMailMerge.Command = "file:///C:\Mail.txt"
oMailMerge.OutputType = 2
oMailMerge.execute(New [Object]() {})**---->I am getting Error here**
End Sub