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

发布于 2024-11-25 13:01:53 字数 119 浏览 0 评论 0原文

大家好,我想将数据从数据集导出到excel表。我的数据集由2个组成,那么我如何在单个Excel工作表中写入多个数据集值

Hi all i would like to export the data from the dataset to excel sheet. My dataset consists of 2 Tables so how can i write the multiple dataset values in a single excel sheet

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北座城市 2024-12-02 13:01:53

在你必须创建之前
1.using Excel = Microsoft.Office.Interop.Excel;//在标题中,并添加正确的引用
2.Excel.Application excelHandle1=PrepareForExport(Ds); //在调用函数中添加句柄
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;
    }

我正在使用这个代码

before u have to create
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";

//  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

凑诗 2024-12-02 13:01:53

您可以在每个工作表中写入一个表值,而不是单个 Excel 工作表,

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

增加工作表计数,您可以在单个 Excel 文件中保存多个数据集值。

希望它可以对您有所帮助。

Instead of single Excel Sheet,you can write a Table values in each sheet,

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.

Hope it may help to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文