将数据集导出到一个excel文件的多个Excel工作表中

时间:2015-11-25 10:54:37

标签: c# .net visual-studio-2010 excellibrary

我需要在同一工作簿的两个excel表中导出两个数据集的值。 我的查询就像:

//数据集一:

Rails.application.config.assets.precompile += %w(semantic-ui/modules/accordion.js)

//数据集二:

        DataSet ds1 = new DataSet();
        SqlCommand commandOpen = new SqlCommand("storedproc1", conSql);
        commandOpen.CommandType = CommandType.StoredProcedure;
        var adaptOpen = new SqlDataAdapter();
        adaptOpen.SelectCommand = commandOpen;
        adaptOpen.Fill(ds1, "table");

现在创建一个我用过的Excel工作簿:

        DataSet ds2 = new DataSet();
        SqlCommand commandOpen = new SqlCommand("storedproc2", conSql);
        commandOpen.CommandType = CommandType.StoredProcedure;
        var adaptOpen = new SqlDataAdapter();
        adaptOpen.SelectCommand = commandOpen;
        adaptOpen.Fill(ds2, "table");

但不是这样,在同一工作簿中我想添加两张;一个用于ds1,另一个用于ds2。怎么做? 感谢。

4 个答案:

答案 0 :(得分:1)

可能重复:how to add dataset to worksheet using Excellibrary

在该问题的答案中,显示了如何将数据集导出到excel文件,但数据集中的每个表都有自己的工作表。您可以根据需要修改代码。

其他帖子的代码:

public static void CreateWorkbook(String filePath, DataSet dataset)
    {
        if (dataset.Tables.Count == 0)
            throw new ArgumentException("DataSet needs to have at least one DataTable", "dataset");

        Workbook workbook = new Workbook();
        foreach (DataTable dt in dataset.Tables)
        {
            Worksheet worksheet = new Worksheet(dt.TableName);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                // Add column header
                worksheet.Cells[0, i] = new Cell(dt.Columns[i].ColumnName);

                // Populate row data
                for (int j = 0; j < dt.Rows.Count; j++)
                    worksheet.Cells[j + 1, i] = new Cell(dt.Rows[j][i]);
            }
            workbook.Worksheets.Add(worksheet);
        }
        workbook.Save(filePath);
    }

答案 1 :(得分:1)

在这里,我们可以将多个表导出到单个Excel工作表中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Excel = Microsoft.Office.Interop.Excel;

namespace multiplesheets_export
{
    class Program
    {
    public static void Main(string[] args)
                {
                    object missing = Type.Missing;
                    SqlConnection con = new SqlConnection("Data Source=WINCTRL-KJ8RKFO;Initial Catalog=excel;Integrated Security=True");

                    SqlDataAdapter da = new SqlDataAdapter("select * from Employee", con);
                    SqlDataAdapter da1 = new SqlDataAdapter("select * from Department", con);

                    DataTable dt = new DataTable();
                    DataTable dt1 = new DataTable();

                    da.Fill(dt);
                    da1.Fill(dt1);

                    if (dt == null || dt.Columns.Count == 0)
                                                        throw new Exception("ExportToExcel: Null or empty input table!\n");
                    Excel.Application oXL = new Excel.Application();
                    Excel.Workbook oWB = oXL.Workbooks.Add(missing);
                    Excel.Worksheet oSheet = oWB.ActiveSheet as Excel.Worksheet;
                    oSheet.Name = "Employee Details";

                    for (var i = 0; i < dt.Columns.Count; i++)
                    {
                        oSheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
                    }
                    for (var i = 0; i < dt.Rows.Count; i++)
                    {
                        for (var j = 0; j < dt.Columns.Count; j++)
                        {
                            oSheet.Cells[i + 2, j + 1] = dt.Rows[i][j];
                        }
                    }

                    // From Here am taking EXCEL SHEET -2

                    Excel.Worksheet oSheet2 = oWB.Sheets.Add(missing, missing, 1, missing)as Excel.Worksheet;

                    if (dt1 == null || dt1.Columns.Count == 0)
                        throw new Exception("ExportToExcel: Null or empty input table!\n");
                    oSheet2.Name = "Depatment Details";

                    for (var i = 0; i < dt1.Columns.Count; i++)
                    {
                        oSheet2.Cells[1, i + 1] = dt1.Columns[i].ColumnName;
                    }
                    for (var i = 0; i < dt1.Rows.Count; i++)
                    {
                        for (var j = 0; j < dt1.Columns.Count; j++)
                        {
                            oSheet2.Cells[i + 2, j + 1] = dt1.Rows[i][j];
                        }
                    }
                    oXL.Visible = true;
                }
            }
        }

答案 2 :(得分:1)

将数据集导出到具有多个图纸的Excel中,可以使用以下代码。 首先,您必须在应用程序中安装“ ClosedXML” NuGet软件包。

public DataSet GetDataSetExportToExcel()
    {
        DataSet ds = new DataSet();
        var SPNames = new List<string>() { "storedproc1", "storedproc2" };

        foreach (var SPName in SPNames)
        {
            DataTable dt = new DataTable();
            dt = GetDataTableExportToExcel(SPName);
            ds.Tables.Add(dt);
        }

        return ds;
    }

    private DataTable GetDataTableExportToExcel(string SPName)
    {
        DataTable dt = new DataTable();

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            using (var cmd = new SqlCommand(SPName, con))
            {
                using (var sda = new SqlDataAdapter(cmd))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }

点击“导出”按钮,使用以下代码

/// <summary>
    /// Export button click event
    /// </summary>
    /// <returns>Return the excel file with multiple sheets</returns>
    public ActionResult ExportToExcel()
    {
        var sheetNames = new List<string>() { "sheetName1", "sheetName2" };
        string fileName = "Example.xlsx";

        DataSet ds = GetDataSetExportToExcel();

        XLWorkbook wbook = new XLWorkbook();

        for (int k = 0; k < ds.Tables.Count; k++)
        {
            DataTable dt = ds.Tables[k];
            IXLWorksheet Sheet = wbook.Worksheets.Add(sheetNames[k]);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                Sheet.Cell(1, (i + 1)).Value = dt.Columns[i].ColumnName;
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    Sheet.Cell((i + 2), (j + 1)).Value = dt.Rows[i][j].ToString();
                }
            }
        }

        Stream spreadsheetStream = new MemoryStream();
        wbook.SaveAs(spreadsheetStream);
        spreadsheetStream.Position = 0;

        return new FileStreamResult(spreadsheetStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = fileName };
    }

我希望它能起作用。

答案 3 :(得分:0)

要将数据放入Excel工作表,您不需要将数据导入数据集。您可以通过多种方式直接将数据提取到Excel工作表。其中一种方法是使用QueryTables.Add方法。使用此方法,在源上修改数据时,更改将反映在excel文件中。即:(使用样本Northwind数据):

void Main()
{
  Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
  var workbook = xl.Workbooks.Add();
  Worksheet sht1, sht2;
  sht1 = ((Worksheet)workbook.Sheets[1]);
  if (workbook.Sheets.Count < 2)
  {
    sht2 = (Worksheet)workbook.Sheets.Add();
  }
  else
  {
    sht2 = ((Worksheet)workbook.Sheets[2]);
  }
  xl.Visible = true;
  sht1.Move(sht2);

  sht1.Name = "Data Sheet 1";
  sht2.Name = "Data Sheet 2";

  string strCon = @"OLEDB;Provider=SQLNCLI11.0;server=.\SQLExpress;Trusted_Connection=yes;Database=Northwind";

  Range target1 = (Range)sht1.Range["A1"];
  sht1.QueryTables.Add(strCon, target1, "Select * from Customers" ).Refresh();

  Range target2 = (Range)sht2.Range["A1"];
  sht2.QueryTables.Add(strCon, target2, "Select * from Orders").Refresh();
}

另一种选择是使用Nuget的Epplus库。使用那个,您只需要有很多工作表来从集合中加载内容。即:

void Main()
{
  ExcelPackage pck = new ExcelPackage();

  var collection1 = from c in db.Customers
                    select new
                    {
                      CustomerName = c.CompanyName,
                      ContactPerson = c.ContactName,
                      FirstOrder = c.Orders.Min(o => o.OrderDate),
                      LastOrder = c.Orders.Max(o => o.OrderDate),
                      OrderTotal = c.Orders.Any() ? c.Orders.Sum(o => o.OrderDetails.Sum(od => od.Quantity * od.UnitPrice)) : 0M,
                      Orders = c.Orders.Count()
                    };
  var collection2 = db.Orders.Select(o => new {
         OrderId = o.OrderID, Customer=o.CustomerID, OrderDate=o.OrderDate});

  var ws1 = pck.Workbook.Worksheets.Add("My Sheet 1");

  //Load the collection1 starting from cell A1 in ws1
  ws1.Cells["A1"].LoadFromCollection(collection1, true, TableStyles.Medium9);
  ws1.Cells[2, 3, collection1.Count() + 1, 3].Style.Numberformat.Format = "MMM dd, yyyy";
  ws1.Cells[2, 4, collection1.Count() + 1, 4].Style.Numberformat.Format = "MMM dd, yyyy";
  ws1.Cells[2, 5, collection1.Count() + 1, 5].Style.Numberformat.Format = "$#,##0.0000";
  ws1.Cells[ws1.Dimension.Address].AutoFitColumns();

  var ws2 = pck.Workbook.Worksheets.Add("My Sheet 2");

  //Load the collection1 starting from cell A1 in ws1
  ws2.Cells["A1"].LoadFromCollection(collection2, true, TableStyles.Medium9);

  //...and save
  var fi = new FileInfo(@"d:\temp\AnonymousCollection.xlsx");
  if (fi.Exists)
  {
    fi.Delete();
  }
  pck.SaveAs(fi);
}

PS:QueryTables和Epplus方法都很快。如果你无论如何都要在循环中使用数据集和\或设置单元格值,那么请确保你有小数据。