1

how to Export DataSet(multiple of Datatables) to a notepad. From c#

Note: NOT A SINGLE DataTable ,Multiple DataTables From DataSet;

  • Which format? (xml, csv, json) – H. Herzl Oct 04 '16 at 06:59
  • to .txt, html,and ,xpf Format to these i need to export –  Oct 04 '16 at 07:01
  • If I understand your reply, if we select txt format the export class will generate a txt file with all data set information (in a simple matrix format), if we set xml output the export class will generate the xml that represents dataset's information and if we want export to xpf the export class will generate the same output than txt file but into a xpf file ? – H. Herzl Oct 04 '16 at 07:10
  • @ h herzi i didn't understand –  Oct 04 '16 at 07:14
  • you want to export into .txt file, that means a simple output not the xml from data set: table 1: ... table 2: ... instead of xml data – H. Herzl Oct 04 '16 at 07:16
  • s, it is my requirement –  Oct 04 '16 at 07:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124857/discussion-between-john-walker-and-h-herzl). –  Oct 04 '16 at 07:37

2 Answers2

1

I would suggest something simple, create a translator or download a bunch of other libraries available on the web.

you would most like go

public interfacte IExport
{
   bool Export(Databale sometable);// this can also reference interface
   //concrete implementation could also handle saving of file
}

then call on a concrete class to implement that value, use a Factory Patter, Dependency Injection, etc to supply the concrete type. Then you can keep adding as many converters to support as many file types as you'd like.

mahlatse
  • 1,322
  • 12
  • 24
  • which are the libraries i need to download, i searched a lot i got only datatable export to a notepad, not dataSet(hving more than 2 datatables) to a single notepad –  Oct 04 '16 at 07:13
  • First you can use a for loop on a datatable, also [microsoft c# has a witeXml method](https://msdn.microsoft.com/en-us/library/x3zy2whb(v=vs.110).aspx), for txt, make it CSV format for easier passing, [Microsoft also has an easy Creating Xps Document](https://msdn.microsoft.com/en-us/library/ms771596(v=vs.100).aspx) that you can check, also there is a simple code sample on [Stackoverflow](http://stackoverflow.com/questions/352540/how-to-create-an-xps-document) about how to create the file. – mahlatse Oct 04 '16 at 07:25
0
private  void Write(System.Data.DataSet dts, string outputFilePath)
{
   System.Data.DataTable dt = new System.Data.DataTable();
   for (int z = 0; z < dts.Tables.Count; z++)
   {
     dt = dts.Tables[z];
     int[] maxLengths = new int[dt.Columns.Count];    
     for (int i = 0; i < dt.Columns.Count; i++)
     {
       maxLengths[i] = dt.Columns[i].ColumnName.Length;    
       foreach (DataRow row in dt.Rows)
       {
         if (!row.IsNull(i))
         {
           int length = row[i].ToString().Length;    
            if (length > maxLengths[i])
            {
              maxLengths[i] = length;
            }
          }
       }
     }

   using (StreamWriter sw = new StreamWriter(outputFilePath, true))
    {     
    for (int i = 0; i < dt.Columns.Count; i++)
     {
                            sw.Write(dt.Columns[i].ColumnName.PadRight(maxLengths[i] + 2));
     }    
     sw.WriteLine();    
      foreach (DataRow row in dt.Rows)
      {
        for (int i = 0; i < dt.Columns.Count; i++)
        {
          if (!row.IsNull(i))
          {
                                    sw.Write(row[i].ToString().PadRight(maxLengths[i] + 2));
          }
          else
          {
           sw.Write(new string(' ', maxLengths[i] + 2));
          }
        }    
      sw.WriteLine();
    }  
    sw.Close();
  }
 }
}
Thomas
  • 1,445
  • 14
  • 30
  • @ swetha thanks good answer it works fine for me, it helpful to my most of reports –  Oct 04 '16 at 08:42