如何将多个数据集导出到单个Excel工作表?

时间:2014-02-14 05:00:40

标签: asp.net sql-server-2008 c#-4.0

我有两个数据集,其中第一个数据集有6行,第二个数据集有'N'行数,第二个数据集行数不断变化,我需要的是我需要将数据集导出到单个excelsheet。两个数据集都有标题,所以我需要使用c#将带有标题的数据集导出到单个excelsheet。任何人都可以帮我解决c#

中的示例代码

2 个答案:

答案 0 :(得分:0)

在你必须创建之前

1. using Excel = Microsoft.Office.Interop.Excel;//in header,and Add correct refference
2. Excel.Application excelHandle1 = PrepareForExport(Ds); //add handle in calling function excelHandle1.Visible = true;


public Excel.Application PrepareForExport(System.Data.DataSet ds,string[] sheet)
{
        object missing = System.Reflection.Missing.Value;
        Excel.Application excel = new Excel.Application();
        Excel.Workbook workbook = excel.Workbooks.Add(missing);

        DataTable dt1 = new DataTable();
        dt1 = ds.Tables[0];
        DataTable dt2 = new DataTable();
        dt2 = ds.Tables[1];


        Excel.Worksheet newWorksheet;
        newWorksheet = (Excel.Worksheet)excel.Worksheets.Add(missing, missing, missing, missing);
        newWorksheet.Name ="Name of data sheet";

//为第一个数据表dt1 ..

        int iCol1 = 0;
        foreach (DataColumn c in dt1.Columns)
        {
            iCol1++;
            excel.Cells[1, iCol1] = c.ColumnName;
        }

        int iRow1 = 0;
        foreach (DataRow r in dt1.Rows)
        {
            iRow1++;

            for (int i = 1; i < dt1.Columns.Count + 1; i++)
            {

                if (iRow1 == 1)
                {
                    // Add the header the first time through 
                    excel.Cells[iRow1, i] = dt1.Columns[i - 1].ColumnName;
                }

                excel.Cells[iRow1 + 1, i] = r[i - 1].ToString();
            }

        }

//第二个数据表dt2 ..

        int iCol2 = 0;
        foreach (DataColumn c in dt2.Columns)
        {
            iCol2++;
            excel.Cells[1, iCol] = c.ColumnName;
        }


        int iRow2 = 0;
        foreach (DataRow r in dt2.Rows)
        {
            iRow2++;

            for (int i = 1; i < dt2.Columns.Count + 1; i++)
            {

                if (iRow2 == 1)
                {
                    // Add the header the first time through 
                    excel.Cells[iRow2, i] = dt2.Columns[i - 1].ColumnName;
                }

                excel.Cells[iRow2 + 1, i] = r[i - 1].ToString();
            }

        }




    return excel;
}

答案 1 :(得分:0)

首先,您必须声明名称空间:

using Excel = Microsoft.Office.Interop.Excel;

public static void Main(string[] args) {

        DataTable dt1 = new DataTable("Employee");
        dt1.Columns.Add("Employee ID");
        dt1.Columns.Add("Employee Name");
        dt1.Rows.Add("1", "ABC");
        dt1.Rows.Add("2", "DEF");
        dt1.Rows.Add("3", "PQR");
        dt1.Rows.Add("4", "XYZ");

        DataTable dt3 = new DataTable("Department");
        dt3.Columns.Add("Department ID");
        dt3.Columns.Add("Department Name");
        dt3.Rows.Add("1", "IT");
        dt3.Rows.Add("2", "HR");
        dt3.Rows.Add("3", "Finance");
        DataSet ds = new DataSet();
        ds.Tables.Add(dt1);
        ds.Tables.Add(dt3);

       Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
        Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);

        Excel._Worksheet worksheet = null;
        app.Visible = false;
        worksheet = workbook.Sheets["Sheet1"];
        worksheet = workbook.ActiveSheet;

        worksheet.Name = "Exported from gridview";
        int z = 1;
        for (int x = 0; x < ds.Tables.Count; x++)
        {
            int r = z;
            int t = r + 1;
            for (int i = 1; i < ds.Tables[x].Columns.Count + 1; i++)
            {
                worksheet.Cells[r, i] = ds.Tables[x].Columns[i - 1].Caption;
            }
            for (int i = 0; i < ds.Tables[x].Rows.Count; i++)
            {
                for (int j = 0; j < ds.Tables[x].Columns.Count; j++)
                {
                    worksheet.Cells[i + t, j + 1] = ds.Tables[x].Rows[i]
                    [j].ToString();
                }
            }
            z = ds.Tables[x].Rows.Count + 3;
        }
        string textPath = "e:\\output.xls";
        if (File.Exists(textPath))
        {
            File.Delete(textPath);
        }

        workbook.SaveAs(textPath, Type.Missing, Type.Missing, Type.Missing,
         Type.Missing,
         Type.Missing, 
         Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
         Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        app.Quit();
}