Link to home
Create AccountLog in
Avatar of freddykhan
freddykhanFlag for United States of America

asked on

Export TreeView to Excel

Morning Experts ...

I populated a treeview from an oracle database using recursion.  Of course now that they have a tree, my users want to export it to MS Excel so they can do user magic on it.  I have not been able to find any information on just pushing a button and exporting the contents of a treeview into MS Excel.  All I want it to do is to open the contents of the tree in Excel and have the nodes indented into columns.  Please help; and also, please let me know if you see any problems with my code in the snippet.
static string fc;
Static OracleConnection Conn = new OracleConnection("bla bla");
protected void Page_Load(object sender, EventArgs e)
{
  if (Page.IsPostBack)
    fc = ((DropDownList)((Page)sender).FindControl("ddl1")).SelectedValue.ToString();
  else
    { if (Conn.State != ConnectionState.Open) Conn.Open(); }
}
protected void ddl1_DataBound(object sender, EventArgs e)
{
  if ( ! Page.IsPostBack )
    fc = ((DropDownList)sender).SelectedValue.ToString();
}
protected void pbFind_Click(object sender, EventArgs e)
{
  DateTime dt1 = DateTime.Now;
  TreeView1.Nodes.Clear();
  TreeNode tn = new TreeNode(ddl1.SelectedItem.ToString());
  TreeView1.Nodes.Add(tn);
  GetNodes(fc, tn);
  DateTime dt2 = DateTime.Now;
  TimeSpan ts = dt2 - dt1;
  if ( ts.Seconds <= 1 )
    Label1.Text = "Time to execute: " + ts.Seconds.ToString() + " second!!";
  else
    Label1.Text = "Time to execute: " + ts.Seconds.ToString() + " seconds!!";
}
protected void GetNodes(string Parent, TreeNode Tn)
{
  string Sql = "select failurecode, failurelist, parent, description from failurecode";
  OracleCommand Cmd = new OracleCommand(Sql, Conn);
  OracleDataReader Dr;
  Dr = Cmd.ExecuteReader();
  while (Dr.Read())
  {
    GridView1.DataSource = Dr;
    TreeNode tn = new TreeNode(Dr[0].ToString() + " (" + Dr[3].ToString() + ")", Dr[0].ToString());
    Tn.ChildNodes.Add(tn);
    if (! string.IsNullOrEmpty(Dr[1].ToString()))
    {
      Parent = Dr[1].ToString();
      GetNodes(Parent, tn);
    }
  }
  Dr.Close();
}

Open in new window

Avatar of ZachSmith
ZachSmith
Flag of United States of America image

You're going to have to recurse through the Tree (let me know if you need help with that), and build a CSV file from the contents. For example if the tree looks like this:

Test
  Test1
  Test2
Good
  Good1
    Good2

The result should be:

Test
,Test1
,Test2
Good
,Good1
,,Good2
ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of freddykhan

ASKER

Could you please send the code to create a csv file on the fly, make entries into it, and then open its contents in Excel?  I guess I can make an entry into to csv file every time I call GetNodes.
The code above will generate the CSV .

Are you wanting Excel to automatically pop up for them as well? If so, you'll need to use the Office libraries for that. See this link:

http://msdn2.microsoft.com/en-us/library/aa192488(office.11).aspx
Even though it does not automatically load excel and open the file, it gets me through my immediate issue which is to export the file to a csv my users can open and analyze.  Thanks!!