Bulk export to Excel and force print

E

Erik

Good Morning,
I have a request that timesheet records for employees be printed on a
separate sheet of paper, essentially doing a bulk 'hard-copy' print the
week's time. I have tried building a dataset for each employee's timesheets,
binding a datagrid to it and exporting to Excel, but
Response.Write("<script>window.print();history.back();</script>") does not
force the print and return scope back to the for each loop. Instead the
initial bound datagrid is rendered and scope is on the Excel spreadsheet.
Below is the current code. Any help or insight is greatly appreciated.
Thank you, Erik
For Each dr As DataRow In dstEmployeeNumbers.Tables(0).Rows

iRecCnt = GetTimeSheets(dr(0))

If iRecCnt > 0 Then
dgrdExport.DataSource = dstTimeSheets.Tables("TimeSheets")
dgrdExport.DataBind()

' Set the content type to Excel.
Response.ContentType = "application/vnd.ms-excel"
' Remove the charset from the Content-Type header.
Response.Charset = ""
' Turn off the view state.
Me.EnableViewState = False

Response.AddHeader("Content-Disposition",
"attachment;filename=TimeSheets" & _
Replace(calEndDt.SelectedDate.ToShortDateString, "/",
"") & ".xls")
dgrdExport.RenderControl(hw)
Response.Write(tw.ToString())

Response.Write("<script>window.print();history.back();</script>")
Response.End()
End If

dstTimeSheets.Clear()
Next
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top