Dear All,
I have a Dept Table with DeptId,Name columns
i am binding this table to grid.
on click of a button i am exporting it to excel.
I have other table Emp with EmpId,Name,DeptId
Is their any possiblity on to show the EmployeeName and EmpId on the Click of Dept Id in the excel either in the same sheet or different sheet using C#.
Please help me to solve this issue
Loading
sanjay dobhalPosted Apr 29, 2008, 7:35 AM
Try this, Hope this wiill solve ur problem,
Protected
Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click If FileUpload2.PostedFile.FileName <> "" ThenUploadBusinessProfile()
ElseLabel7.Visible =
TrueLabel7.Text =
"Please Select A Excel File First...." End If End Sub Sub UploadBusinessProfile() Dim path As String = Server.MapPath("~/Admin/ImportData/")FileUpload2.PostedFile.SaveAs(path & _
FileUpload2.FileName)
Label7.Visible =
True Dim prmpathexcelfile As String = path + FileUpload2.FileNameImportBusinessProfile(prmpathexcelfile)
End Sub Sub ImportBusinessProfile(ByVal PrmPathExcelFile As String) Dim strpath = PrmPathExcelFile Dim count As Int32 = 0 Dim loopcounter, i As Int32 Dim cmd As New SqlCommand Dim query As String Dim con As SqlConnectioncon =
New SqlConnection(ocon) Dim PostName = "", Category = "", CompanyName = "", Companybriefdis = "", Turnover = "", ProjectHandle = "", ContactPerson = "", Address = "", State = "", City = "" Dim PhoneNo = "", Fax = "", Weblink = "" Dim econ As OleDbConnection = New System.Data.OleDb.OleDbConnection( _ "Provider=Microsoft.Jet.OLEDB.4.0;" _&
"Data Source=" & strpath & ";" _&
"Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""")
Tryecon.Open()
Dim objCmdSelect As New OleDbCommand("select * from [Sheet1$]", econ) Dim objAdapter1 As New OleDbDataAdapter()objAdapter1.SelectCommand = objCmdSelect
Dim ds As New DataSet() 'pb1.Minimum = 1objAdapter1.Fill(ds,
"Test") If ds.Tables(0).Rows.Count > 0 Then 'GridView1.DataSource = Nothing 'GridView1.DataSource = ds.Tables(0) 'GridView1.DataBind()count = ds.Tables(0).Rows.Count
'pb1.Maximum = count For loopcounter = 0 To count If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("PostName"))) ThenPostName = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"PostName")) ElsePostName =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("Category"))) ThenCategory = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"Category")) ElseCategory =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("CompanyName"))) ThenCompanyName = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"CompanyName")) ElseCompanyName =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("Companybriefdis"))) ThenCompanybriefdis = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"Companybriefdis")) ElseCompanybriefdis =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("Turnover"))) ThenTurnover = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"Turnover")) ElseTurnover =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("ProjectHandle"))) ThenProjectHandle = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"ProjectHandle")) ElseProjectHandle =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("State"))) ThenState = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"State")) ElseState =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("City"))) ThenCity = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"City")) ElseCity =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("ContactPerson"))) ThenContactPerson = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"ContactPerson")) ElseContactPerson =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("Address"))) ThenAddress = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"Address")) ElseAddress =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("Fax"))) ThenFax = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"Fax")) ElseFax =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("PhoneNo"))) ThenPhoneNo = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"PhoneNo")) ElsePhoneNo =
"" End If If Not (IsDBNull(ds.Tables(0).Rows.Item(i).Item("Weblink"))) ThenWeblink = Convert.ToString(ds.Tables(0).Rows.Item(i).Item(
"Weblink")) ElseWeblink =
"" End Ifquery =
"Insert into Agent(PostName,Category, CompanyName, Companybriefdis, Turnover,ProjectHandle, ContactPerson, Address, State, City, PhoneNo, Fax, Weblink) " _&
" values('" & PostName & "', '" & Category & "','" & CompanyName & "','" & Companybriefdis & "','" & Turnover & "','" & ProjectHandle & "','" & ContactPerson & "','" & Address & "','" & State & "','" & City & "','" & PhoneNo & "','" & Fax & "','" & Weblink & "')"cmd.CommandText = query
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
If i = count - 1 Then Exit For End Ifi = i + 1
'pb1.Value = i NextLabel7.Text =
"Business Profile Data Successfully Imported..........."Label7.Visible =
True End Ifcon.Close()
Catch ex As ExceptionLabel7.Text = ex.ToString()
End Try End Subl lxPosted Apr 22, 2008, 2:05 AM
try to do as following code.
Response.Clear();
Response.Buffer = false;
Response.Charset ="UTF-8";
//filename is your define format Response.AppendHeader("Content-Disposition","attachment;filename=Member("+DateTime.Now.ToShortDateString() +").xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
// Excel file ContentType = "application/vnd.ms-excel"
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter excelWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htriter = new System.Web.UI.HtmlTextWriter(excelWriter);
// dgMemberInfo is your DataGrid object
if(dgMemberInfo.Items.Count > 0)
this.dgMemberInfo.RenderControl(htriter);
Response.Write(excelWriter.ToString());
Response.End();
Vijaya KadiyalaPosted Apr 14, 2008, 10:51 AM
Hi,
do you want to export the data from dept(grid), Emp(grid) to two different excel sheets and link them based on the Dept ID?
Thanks -- Vj