Hello good afternoon How can I create an Excel spreadsheet with the collected values, with a C# script?

Hello good afternoon

How can I create an Excel spreadsheet with the collected values, with a C# script?

Hi Taylor, could you give us more details about your applications?

Where are you collecting this data from and how are you currently storing these values?

We have an example that might help you.
Example Export Historian to CSV

Best Regards.
Tatsoft Team.



Good morning, as you can see, I took the idea from Excel and chose to use a text block.

However, when creating the text block and writing what is inside the internal database (sqlite), the number 4 only appears.

Could you help me with this?

My idea is to take the data from my internal database and create a csv file, but the example you sent is not opening, could you help me in another way?

The better way to extract data from a database and save it into a CSV file is by first loading the retrieved data into a DataTable object.
You can do this with the following command:

DataTable dt = @Dataset.Query.SelectALL.SelectCommand();

After that, you need to iterate through the DataTable to save its data.

We have prepared an example for you:

using System.IO;
public void Save(object sender, System.Windows.Input.InputEventArgs e)
{
	string filePathAndName = @Info.Project.ProjectPath+"/File1";
	DataTable dt = @Dataset.Query.SelectALL.SelectCommand();	
	
	try  
    {  
    	StreamWriter sw = new StreamWriter(filePathAndName, false);  
    	int columnCount = dt.Columns.Count;  
	    for (int i = 0; i < columnCount ; i++)  
	    {  
	    	sw.Write(dt.Columns[i]);  
	        if (i < columnCount - 1)  
	        {  
	        	sw.Write(",");  
	        }  
	    }  
	    sw.Write(sw.NewLine);  
	    foreach (DataRow dr in dt.Rows)  
	    {  
	    	for (int i = 0; i < columnCount ; i++)  
	        {  
	        	if (!Convert.IsDBNull(dr[i]))  
	            {  
	            	sw.Write(dr[i].ToString());  
	            }  
	            if (i < columnCount - 1)  
	            {  
	            	sw.Write(",");  
	            } 
	        }  
	        sw.Write(sw.NewLine);  
	  	}  
	    sw.Close();
	}  
    catch (Exception ex)  
    {  
    	@Info.Trace(ex.Message);  
    }
}

Best Regards.
Tatsoft Team.