Private Sub btnGenerateReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerateReport.Click
Dim dpl As New List(Of DatasetPair)
'Try
Me.Cursor = Cursors.WaitCursor
Dim intSheetCount As Integer = 0
For i As Integer = 0 To lvMatchedDvid.Items.Count - 1
Dim dp As New DatasetPair
dp.DataVersionIdOne = lvMatchedDvid.Items(i).Text.ToString
dp.DataVersionIdTwo = lvMatchedDvid.Items(i).SubItems(1).Text.ToString
intSheetCount += 1
dp.SheetId = intSheetCount
dpl.Add(dp)
Next
Dim wb As Excel.Workbook = Utilities.CreateWorkbook()
Dim strFilePath As String = wb.Path.ToString & "\" & wb.Name.ToString
MessageBox.Show(strFilePath)
For Each dp As DatasetPair In dpl
Dim excelApp As New Excel.Application
Dim ws As New Excel.Worksheet
ws = GenerateWorksheet(dp)
ws.SaveAs(wb.Path.ToString)
Dim strSourceFilePath As String
strSourceFilePath = wb.Path.ToString & "\" & wb.Name.ToString
MessageBox.Show(strSourceFilePath)
'wb.Close()
Dim wbkDest As Excel.Workbook = excelApp.Workbooks.Open(strFilePath)
Dim wbkSource As Excel.Workbook = excelApp.Workbooks.Open(strSourceFilePath)
'ws.Copy(Before:=wbkDest.Worksheets.Count - 1)
ws.Copy(wbkDest.Worksheets(wbkDest.Worksheets.Count - 1))
Next
'Catch ex As Exception
' MessageBox.Show(ex.Message, ex.GetType.ToString)
'Finally
' Me.Cursor = Cursors.Arrow
'End Try
End Sub
ASKER
ASKER
Private Function GenerateWorksheet(ByVal dp As DatasetPair) As Excel.Worksheet
If cboYears.SelectedIndex = -1 Then
MessageBox.Show("You must make a valid selection from the Budget Year drop-down box", "Missing Selection")
Return Nothing
End If
Dim strDvidOne As String = String.Empty
Dim strDvidTwo As String = String.Empty
Dim n As Integer = 0
Dim ws As New Excel.Worksheet
'Try
strDvidOne = dp.DataVersionIdOne.ToString
strDvidTwo = dp.DataVersionIdTwo.ToString
'Populates datagrid dgvPBTM
GenerateReportData(strDvidOne, strDvidOne)
If dgvPBTM.RowCount = 0 Then
MessageBox.Show("No Data Available")
Return Nothing
End If
Dim strInput As String = ""
'Retrieves Excel template
Dim file As String = Utilities.GetResourceFile("PrimaryBenefitsTraditionalMetricsTemplate")
If file <> String.Empty Then
' Starts the application of the temporary location. If necessaryYou could copy the file to another location
Dim strExcelFile As String = file
If Not IO.File.Exists(strExcelFile) Then Return ws '
' Start Microsoft Excel and make it visible.
myExcel = New Excel.Application
If myExcel Is Nothing Then
Throw (New Exception("Unable to Start Microsoft Excel."))
Else
myExcel.Visible = True
myExcel.WindowState = Excel.XlWindowState.xlMaximized
' Get the process ID of the Excel instance. This is used
' in the Closing Event to prevent orphaned Excel processes.
Dim aProcesses() As Process = Process.GetProcesses
Dim t As Integer
For t = 0 To aProcesses.GetUpperBound(0)
If aProcesses(t).MainWindowHandle.ToString = myExcel.Hwnd.ToString Then
myExcelPID = aProcesses(t).Id
Exit For
End If
Next
End If
Dim strWorkSheetName As String = "Sheet1" & DateTime.Today.ToString
Dim aWorkbook As Excel.Workbook
Dim aWorkSheet As Excel.Worksheet
n += 1
aWorkbook = myExcel.Workbooks.Open(file)
aWorkSheet = aWorkbook.Sheets(n)
aWorkSheet.Activate()
If dgvPBTM.RowCount < 1 Then
MessageBox.Show("No valid data")
Return Nothing
Else
' Assume the data to send to Excel is in a datagric view and it has 5 columns
' and will fill Excel starting with column C and row 4.
Dim intRows As Integer = myDataTable.Rows.Count
Dim intCols As Integer = myDataTable.Columns.Count
Dim strUserTitle As String = ""
Dim strRangeString As String = ""
strRangeString = "NemDataRange"
Dim aRange As Excel.Range = aWorkSheet.Range(strRangeString)
' Remove any existing data from Excel.
aRange.ClearContents()
Dim cell As Excel.Range
cell = aRange.Cells
For Each cell In aRange
cell.Interior.Color = RGB(255, 255, 0)
Next
Dim vecRangeLines() As String
Dim strRangeLines As String = "8, 10, 13, 15, 18, 20, 22, 24, 26, 29, 31, 33, 35"
Dim intXLSLine As Integer = 0
Dim intRangeLine As Integer
vecRangeLines = strRangeLines.Split(",")
'Iterate through the datasource
For Each r As DataRow In myDataTable.Rows
'Assigning the line number to the variable
intRangeLine = Val(vecRangeLines(intXLSLine)) - 7
'Iterate through the columns
For x As Integer = 1 To intCols - 1
'Assign the value from the datasource to the range
If intRangeLine = 19 Then
If r(x) Is DBNull.Value Then
aRange.Cells(intRangeLine, x) = " "
Else
aRange.Cells(intRangeLine, x) = r(x) * 1000
End If
Else
If r(x) Is DBNull.Value Then
aRange.Cells(intRangeLine, x) = " "
Else
aRange.Cells(intRangeLine, x) = r(x)
End If
End If
Next
intXLSLine += 1
Next
'Populate year range in Excel sheet
strRangeString = "YearHeaderRange"
aRange = aWorkSheet.Range(strRangeString)
Dim d As Integer = 1
For c As Integer = 1 To myDataTable.Columns.Count - 1
'Assign the value from the datasource to the range
aRange.Cells(1, d) = myDataTable.Columns(c).ColumnName.ToString
d += 1
Next
strRangeString = "UserProvidedTitleLine"
aRange = aWorkSheet.Range(strRangeString)
strUserTitle = txtUserProvidedTitle.Text.ToString
aRange.Cells(1, 1) = strUserTitle
Dim strTechName As String = TechnologySetDB.GetTechnologyName(strTech)
Dim p As New Policy
p = PolicyDB.GetSelectedPolicy(strPolicy)
Dim strPolicyName As String = p.Description.ToString
Dim b As New Budget
b = BudgetDB.GetSelectedBudget(strBudget)
Dim strBudgetName As String = b.Description.ToString
strRangeString = "ScenarioNameRange"
aRange = aWorkSheet.Range(strRangeString)
aRange.Cells(1, 1) = strFY & " " & strTechName & ", " & strPolicyName & ", " & strBudgetName '& ", " & "Version " & strVersion
strRangeString = "DataDateRange"
aRange = aWorkSheet.Range(strRangeString)
aRange.Cells(1, 1) = "Printed: " & Now.ToString
strRangeString = "DataVersionIdRange"
aRange = aWorkSheet.Range(strRangeString)
aRange.Cells(1, 1) = "DataVersionId: " & strdvid1 & vbCrLf & "DataVersionId: " & strdvid2
ws = aWorkSheet
Return ws
End If
Else
MessageBox.Show("Unable to extract the File")
End If
'Catch ex As Exception
'MessageBox.Show(ex.Message.ToString & vbCrLf & ex.GetType.ToString)
' Finally
'CleanUp()
'End Try
Return ws
End Function
ASKER
ASKER
Dim dpl As New List(Of DatasetPair)
'Try
Me.Cursor = Cursors.WaitCursor
Dim intSheetCount As Integer = 0
For i As Integer = 0 To lvMatchedDvid.Items.Count - 1
Dim dp As New DatasetPair
dp.DataVersionIdOne = lvMatchedDvid.Items(i).Text.ToString
dp.DataVersionIdTwo = lvMatchedDvid.Items(i).SubItems(1).Text.ToString
intSheetCount += 1
dp.SheetId = intSheetCount
dpl.Add(dp)
Next
Dim wb As Excel.Workbook = Utilities.CreateWorkbook()
Dim strFilePath As String = wb.Path.ToString & "\" & wb.Name.ToString
For Each dp As DatasetPair In dpl
Dim excelApp As New Excel.Application
Dim ws As New Excel.Worksheet
ws = GenerateWorksheet(dp)
Dim wbkDest As Excel.Workbook = excelApp.Workbooks.Open(strFilePath)
MessageBox.Show(ws.Range("A8").Value.ToString)
ws.Copy(wbkDest.Worksheets(wbkDest.Worksheets.Count - 1))
Next
ASKER
ASKER
ASKER
Dim xlsApp As Excel.Application
Dim xlsWB As Excel.Workbook
Dim xlsSheet As Excel.Worksheet
xlsApp = New Excel.Application
Dim wb As Excel.Workbook = Utilities.CreateWorkbook()
Dim strFilePath As String = wb.Path.ToString & "\" & wb.Name.ToString
xlsWB = xlsApp.Workbooks.Open(strFilePath)
xlsSheet = xlsWB.Worksheets(1)
For Each dp As DatasetPair In dpl
xlsSheet = GenerateWorksheet(dp)
xlsSheet.Copy(After:=xlsApp.Workbooks(xlsWB).Worksheets("Sheet1"))
Next
ASKER
Public Shared Function CreateWorkbook() As Excel.Workbook
Dim filename As String
Dim excelApp As Excel.Application
Dim excelWorkbook As Excel.Workbook
Dim excelSheet As Excel.Worksheet
Dim SaveFileDialog As New SaveFileDialog
If My.Settings.DefaultLocation = "" Then
SaveFileDialog.InitialDirectory = "C:\"
Else
SaveFileDialog.InitialDirectory = My.Settings.DefaultLocation.ToString
End If
SaveFileDialog.Filter = "xlsx files (*.xlsx)|*.xlsx"
SaveFileDialog.FilterIndex = 2
SaveFileDialog.RestoreDirectory = True
'***************************Creates Spreadsheet*********************************************
If SaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
filename = SaveFileDialog.FileName
'Validate file name
If Convert.ToString(filename) = "" Then
Return Nothing
Exit Function
End If
'Create excel instance
Dim misValue As Object = System.Reflection.Missing.Value
excelApp = New Excel.ApplicationClass
excelWorkbook = excelApp.Workbooks.Add(misValue)
excelSheet = excelWorkbook.Sheets("sheet1")
excelApp.ScreenUpdating = True
excelApp.Caption = System.Guid.NewGuid.ToString.ToUpper
If Not filename.EndsWith(".xlsx") Then
excelWorkbook.SaveAs(filename, 56)
Else
excelWorkbook.SaveAs(filename)
End If
MsgBox("File generated successfully at " & filename)
Return excelWorkbook
Else
Return Nothing
End If
End Function
ASKER
ASKER
ASKER
For Each dp As DatasetPair In dpl
xlsSheet = GenerateWorksheet(dp, xlsApp)
MessageBox.Show(xlsSheet.Range("A8").Value.ToString)
xlsSheet.Copy(Before:=xlsWB.Worksheets(1))
Next
Close Parent workbook of xlsSheet
ASKER
wb = Utilities.NewCreateWorkbook(xlsApp)
Dim strFilePath As String = wb.Path.ToString & "\" & wb.Name.ToString
xlsWB = xlsApp.Workbooks.Open(strFilePath)
xlsWB.Activate()
xlsSheet = xlsWB.Worksheets(1)
For Each dp As DatasetPair In dpl
xlsSheet = GenerateWorksheet(dp, xlsApp)
MessageBox.Show(xlsSheet.Range("A8").Value.ToString)
xlsSheet.Copy(Before:=xlsWB.Worksheets(1))
Next
Dim book As Excel.Workbook = CType(xlsSheet.Parent, Excel.Workbook)
Dim wkbName As String
wkbName = book.Name
xlsApp.Workbooks(wkbName).Close(False)
Me.Cursor = Cursors.Arrow
ASKER
ASKER
ASKER
ASKER
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
TRUSTED BY
That's all I can suggest right now without seeing the rest of the code - like I said you seem to be calling other subs/methods/whatever.
I'm also wondering why after creating the new worksheet with GenerateWorksheet you then appear to be saving the worksheet.
It might be an idea to pass the workbook you've created at the start of the code as well as the data pair.
Sorry if this isn't helping much but I'm just going on what's been posted so far.
I'm currently trying to create a similar project to see if I can find any clues.