I am using c#.net web application where i am using MySQL DB, my requirement is i want to export table data as in Excel format. please tell me how it is possible
5 Answers
The easiest way would be to render your data to a (html) table in plain text format. Excel loads such tables and even allows some basic and advanced configurations in css styles.
However, the other options presented here are more stable and of course recommended. But for a quick and dirty hack, a plain html table will do the trick as well.
Comments
Depending on what u really need i don't know for sure, why do u want to export table data into a Excel format? I would use SSIS with as source a odbc connection and as destination an excel connection. U'll only need to have sql server to execute the SSIS package, but will work for MySQL as source. U can execute that SSIS package in runtime behind the HttpPOST and return the uri to the excel document or something like that.
PS: This might be an elephant for your solution, as i said: depending on what u really need :)
Comments
I am using ExcelPackagePlus for that purpose (only xlsx !), here is a method that exports DataTable to xlsx :
public static MemoryStream DataSetToExcelXlsx(DataTable table, string sheetName)
{
MemoryStream Result = new MemoryStream();
ExcelPackage pack = new ExcelPackage();
ExcelWorksheet ws = pack.Workbook.Worksheets.Add(sheetName);
int col = 1;
int row = 1;
foreach (DataRow rw in table.Rows)
{
foreach (DataColumn cl in table.Columns)
{
if (rw[cl.ColumnName] != DBNull.Value)
ws.Cells[row, col].Value = rw[cl.ColumnName].ToString();
col++;
}
row++;
col = 1;
}
pack.SaveAs(Result);
return Result;
}