1

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

1
  • please show some source code... what have you tried ? do you want XLSX or XLS files ? Would CSV be ok for your use case ? Commented Mar 5, 2012 at 10:05

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

CSV (comma separated values) are common format for Excel. It is, probably, the easiest way to export data. And very portable :)

Comments

0

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

0

C#.NET can access MySQL data with ADO.NET: so you can use DataReader or DataTable as well. Remember to use the correct SqlClient. Create and save a flat .csv file with the extracted data.

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.