在c#.net中将MySQL数据库导出到EXCEL

时间:2021-11-02 23:18:02

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

我正在使用c#.net web应用程序,我正在使用MySQL DB,我的要求是我想以Excel格式导出表格数据。请告诉我这是怎么可能的

5 个解决方案

#1


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.

最简单的方法是以纯文本格式将数据呈现给(html)表。 Excel加载这样的表,甚至允许css样式中的一些基本和高级配置。

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.

但是,此处介绍的其他选项更稳定,当然也是推荐的。但是对于快速而肮脏的黑客攻击,一个简单的html表也可以做到这一点。

#2


1  

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

CSV(逗号分隔值)是Excel的常用格式。它可能是导出数据的最简单方法。非常便携:)

#3


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.

根据您真正需要的我不确定的原因,您为什么要将表格数据导出为Excel格式?我会使用SSIS作为源的odbc连接和作为目标的excel连接。你只需要有sql server来执行SSIS包,但是作为源代码可以用于MySQL。你可以在HttpPOST后面的运行时执行那个SSIS包,并将uri返回到excel文档或类似的东西。

PS: This might be an elephant for your solution, as i said: depending on what u really need :)

PS:这可能是你的解决方案的大象,正如我所说:取决于你真正需要的:)

#4


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.

C#.NET可以使用ADO.NET访问MySQL数据:因此您也可以使用DataReader或DataTable。请记住使用正确的SqlClient。使用提取的数据创建并保存平面.csv文件。

#5


0  

I am using ExcelPackagePlus for that purpose (only xlsx !), here is a method that exports DataTable to xlsx :

我正在使用ExcelPackagePlus(仅限xlsx!),这是一个将DataTable导出到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;
}

#1


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.

最简单的方法是以纯文本格式将数据呈现给(html)表。 Excel加载这样的表,甚至允许css样式中的一些基本和高级配置。

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.

但是,此处介绍的其他选项更稳定,当然也是推荐的。但是对于快速而肮脏的黑客攻击,一个简单的html表也可以做到这一点。

#2


1  

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

CSV(逗号分隔值)是Excel的常用格式。它可能是导出数据的最简单方法。非常便携:)

#3


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.

根据您真正需要的我不确定的原因,您为什么要将表格数据导出为Excel格式?我会使用SSIS作为源的odbc连接和作为目标的excel连接。你只需要有sql server来执行SSIS包,但是作为源代码可以用于MySQL。你可以在HttpPOST后面的运行时执行那个SSIS包,并将uri返回到excel文档或类似的东西。

PS: This might be an elephant for your solution, as i said: depending on what u really need :)

PS:这可能是你的解决方案的大象,正如我所说:取决于你真正需要的:)

#4


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.

C#.NET可以使用ADO.NET访问MySQL数据:因此您也可以使用DataReader或DataTable。请记住使用正确的SqlClient。使用提取的数据创建并保存平面.csv文件。

#5


0  

I am using ExcelPackagePlus for that purpose (only xlsx !), here is a method that exports DataTable to xlsx :

我正在使用ExcelPackagePlus(仅限xlsx!),这是一个将DataTable导出到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;
}