How to export a database to Excel and mail

1

How it would be the code to export the data that are inserted in a database .sdf to an excel file. in C # and for visual studio 2008 and SQL Compact 3.5, and my other question would be if I need to call a method within

  

writer.WriteLine ();

How would the call be made since if I put the method inside the parentheses because this one throws me the following error

  

the best overloaded method match for "System.IO.TextWriter.WriteLine (string.Params object [])" has some invalid arguments.

    
asked by Cristian R.M 02.09.2016 в 18:58
source

2 answers

1

If you use ado.net to access sdf then you can generate a datatable , if so, I recommend evalues

ClosedXML - The easy way to OpenXML

with this you could generate a direct excel assigning the datatable

var wb = new XLWorkbook();
DataTable dataTable = GetTable(); //aqui obtienes los datos del sdf

wb.Worksheets.Add(dataTable);

wb.SaveAs("Nombreexcel.xlsx");

So simple, with very little code, and the best thing is that you do not need to have office installed on the pc to generate the excel

    
answered by 02.09.2016 в 19:31
0
var wb = new XLWorkbook();
using(var conn = new SqlCeConnection("STRINGCONECTION"))
{
    conn.Open();

    var comm = new SqlCeCommand("CONSULTA SQL", conn);
    SqlCeDataReader reader = comm.ExecuteReader();

    wb.Worksheets.Add(reader);
    wb.SaveAs("Excel.xlsx");
}

See if it helps you save all the data in the query

I leave a link with help for doubts that this does not help you link

    
answered by 02.09.2016 в 22:11