Export dasboard data to csv

Hi,
I’m adding custom button to dashboard which should export data to csv file. Could use BAQ export but it exports as standard csv with quotes(which is not good). What i need is data structure to be exported as like mouse rclick. I’m not strong in coding the best I could do is I found some scripts, edited it to my needs, but it exports just the first column data and without column labels. What am I missing?
Code:

	private void epiButtonC1_Click(object sender, System.EventArgs args)
	{
		// ** Place Event Handling Code Here **
	EpiDataView edvPP  = (EpiDataView)(oTrans.EpiDataViews["V_JOB_EXCEPTIONS_1View"]);



int Count = 0;
      int i = 0;
      using (System.IO.StreamWriter sw = new      System.IO.StreamWriter(@"C:\Location\Test.CSV"))

{
     foreach(DataRow dr in edvPP.dataView.Table.Rows)
      {
        object[] ay = dr.ItemArray;
        for (i=0; i < Count - 1; i++)
           {
              sw.Write(ay[i].ToString() + "");
           }
         sw.WriteLine(ay[i].ToString());
       }
    }
}

Expected output is:

2021-06-11 11_38_20-prwez0pl.p5e - Excel

I am not certain, but I think right here. Your for statement says “do this loop while i is less than count-1.” Your count starts at 0, so the loop only goes once. I am not sure about the syntax, but something like this might work to set your count before you get into that for loop.

Count = edvPP.dataView.Table.Rows.Count();

Having said all this, I have never exported data this way from a button click.
Good Luck!
Nate

You might be able to simply get rid of the for i loop by changing it to a foreach loop. Then you can get rid of the itterator and the count.

private void epiButtonC1_Click(object sender, System.EventArgs args) {
        // ** Place Event Handling Code Here **
        EpiDataView edvPP = (EpiDataView) (oTrans.EpiDataViews[“V_JOB_EXCEPTIONS_1View”]);

        using(System.IO.StreamWriter sw = new System.IO.StreamWriter(@“ C: \Location\ Test.CSV”)) {
            foreach (DataRow dr in edvPP.dataView.Table.Rows) {
                foreach (var ay in dr.ItemArray) {
                    sw.Write(ay.ToString() + “”);
                }
            }
        }
2 Likes

Ill post my code once I have it sorted out.

1 Like

Here is the code i’m using for my own purposes.

private void EasyButton_Click(object sender, System.EventArgs args)
{
	// ** Place Event Handling Code Here **
	EpiDataView edvPP  = (EpiDataView)(oTrans.EpiDataViews["V_kpNameplateData_1View"]);

	using (System.IO.StreamWriter sw = new      System.IO.StreamWriter(@"C:\temp\NameplateData.CSV", false, System.Text.Encoding.UTF32))

	{
		char ctab = '\t';
		string stab = "\t";
		string newline = "\n";
		string[] headerList = {"model", "serial #", "Rating"}; 
		string header = "";
		string detail = "";
		
		header = string.Join(stab, headerList);
		sw.Write(header + newline);

		foreach (DataRow dr in edvPP.dataView.Table.Rows) {
			foreach (var ay in dr.ItemArray) {
			sw.Write(ay.ToString() + "" + ctab);
			}
		}		
	}
}

}

2 Likes

Hi,

This is basicaly almost a solution. Just one question, maybe you know fast answer. if view contains more than one row. How to jump to new line every time when row reaches the end, is some count needed? And how to ignore sysrow, do i need to point code to exact columns which are needed?

Ok figured out how to jump to new line on each row(added \n after first loop instead before), but can not get rid of sysrow
image

	{
		char ctab = '\t';
		string stab = "\t";
		string newline = "\n";
		string[] headerList = {"Load", "Character01"}; 
		string header = "";
		string detail = "";
		
		header = string.Join(stab, headerList);
		sw.Write(header);
		foreach (DataRow dr in edvPP.dataView.Table.Rows) {
		sw.Write(newline);
			foreach (var ay in dr.ItemArray) {
			sw.Write(ay.ToString() + "" + ctab);
			}
		}		
	}
}
}

@kylepbsps thanks for code.
Finally solved the puzzle by combining your code with some additional scripts. Don’t know if this is the best way, but seems now I can get only needed rows splited per line and without sysrow.
image

private void epiButtonC1_Click(object sender, System.EventArgs args)
	{
		// ** Place Event Handling Code Here **
EpiDataView edvPP  = (EpiDataView)(oTrans.EpiDataViews["MyView"]);

	using (System.IO.StreamWriter sw = new      System.IO.StreamWriter(@"temp\NameplateData.CSV", false, System.Text.Encoding.UTF32))

	{
		char ctab = '\t';
		string stab = "\t";
		string newline = "\n";
		string[] headerList = {"Load", "Character01"}; 
		string header = "";
		string detail = "";
		
		header = string.Join(stab, headerList);
		sw.Write(header);

		foreach (DataRow dr in edvPP.dataView.ToTable().Rows) {
                sw.Write(newline);
			

			sw.Write(dr["Calculated_sum"].ToString() + "" + ctab + dr["UD02_Character01"].ToString());
			
		}		
	}
}
}