简体   繁体   English

如何将多个数据集导出到 excel 表

[英]How can i export multiple dataset to an excel sheet

Hi all i would like to export the data from the dataset to excel sheet.大家好,我想将数据dataset的数据导出到excel表。 My dataset consists of 2 Tables so how can i write the multiple dataset values in a single excel sheet我的dataset由 2 个Tables组成,所以我如何在单个 excel 表中写入多个数据集值

before u have to create在你必须创造之前
1.using Excel = Microsoft.Office.Interop.Excel;//in header,and Add correct refference 1.使用Excel = Microsoft.Office.Interop.Excel;//在header中,并添加正确的引用
2.Excel.Application excelHandle1 = PrepareForExport(Ds); 2.Excel.Application excelHandle1 = PrepareForExport(Ds); //add handle in calling function excelHandle1.Visible = true; //在调用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";

//  for first datatable 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();
                }

            }

   //  for  second datatable 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;
    }

i am using this code我正在使用此代码

Instead of single Excel Sheet,you can write a Table values in each sheet,而不是单个 Excel 表,您可以在每个表中写入一个表值,

http://csharp.net-informations.com/excel/csharp-excel-export.htm http://csharp.net-informations.com/excel/csharp-excel-export.htm

increment the worksheet count you able to save multiple dataset values in the single excel file.增加工作表计数,您可以将多个数据集值保存在单个 excel 文件中。

Hope it may help to you.希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM