简体   繁体   English

C#-将断开连接的数据集数据导出到Excel工作表

[英]C# - Export disconnected dataset data to an excel sheet

I am using ADO.Net. 我正在使用ADO.Net。 I need to export a table of a disconnected dataset to an excel sheet. 我需要将断开连接的数据集表导出到Excel工作表。 I can export the whole dataset, but I need to export one table. 我可以导出整个数据集,但是我需要导出一张表。

do
            {
                if (!reader.HasRows)
                {
                    MessageBox.Show("Empty Database");
                }
                else
                {

                    while (reader.Read())
                    {
                        j++;
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            Console.Write(reader[i] + " ");
                            xlWorkSheet.Cells[j, i + 1] = reader[i];
                        }
                        Console.WriteLine();
                    }
                }
            } while (reader.NextResult());

Somebody please help me. 有人请帮助我。 Sorry for my English and explaining. 对不起,我的英语和解释。 Thanks 谢谢

This might help you, this method takes datatable as input and you can give dataSet.Tables[indexOfyouDatatable] or dataSet.Tables["yourdatatableName"] as parameter. 这可能会对您有所帮助,此方法将数据表作为输入,并且可以将dataSet.Tables [indexOfyouDatatable]或dataSet.Tables [“ yourdatatableName”]作为参数。 String parameter strSheetname is the name of excel sheet that will be exported, you can give table name of your datatable here. 字符串参数strSheetname是将要导出的Excel工作表的名称,您可以在此处提供数据表的表名。

public void exportDtToExcel(DataTable dt, string excelPath, string StrSheetName)
    {
        try
        {
            int ColumnCount;
            if (dt == null || (ColumnCount = dt.Columns.Count) == 0)
            {
                throw new Exception("Null or empty input table!\n");
            }

            Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
            Excel.Workbooks.Add();

            Microsoft.Office.Interop.Excel._Worksheet Worksheet = Excel.ActiveSheet;

            object[] Header = new object[ColumnCount];                           
            for (int i = 0; i < ColumnCount; i++)
            {
                Header[i] = dt.Columns[i].ColumnName;
            }
            Microsoft.Office.Interop.Excel.Range HeaderRange = Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[1, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[1, ColumnCount]));
            HeaderRange.Value = Header;
            DataTable tempdtsheet;
            Worksheet = Excel.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);                
            {
                Worksheet.Name = StrSheetName;
                tempdtsheet = dt;
                Worksheet.Activate();
            }              
            Excel.Range cells = Worksheet.Cells;
            try
            {              
                for (int i1 = 0; i1 < ColumnCount; i1++)
                    Header[i1] = tempdtsheet.Columns[i1].ColumnName;
                Microsoft.Office.Interop.Excel.Range HeaderRange1 = Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[1, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[1, ColumnCount]));
                HeaderRange1.Value = Header;
                int RowsCount1 = tempdtsheet.Rows.Count;
                object[,] Cells1 = new object[RowsCount1, ColumnCount];

                for (int j = 0; j < RowsCount1; j++)
                    for (int i1 = 0; i1 < ColumnCount; i1++)
                    {
                        Cells1[j, i1] = tempdtsheet.Rows[j][i1];
                    }
                Worksheet.get_Range((Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[2, 1]), (Microsoft.Office.Interop.Excel.Range)(Worksheet.Cells[RowsCount1 + 1, ColumnCount])).Value = Cells1;
                Worksheet.Columns.AutoFit();
                ////deleting other sheets
                ((Microsoft.Office.Interop.Excel.Worksheet)Excel.Worksheets["Sheet3"]).Delete();
                ((Microsoft.Office.Interop.Excel.Worksheet)Excel.Worksheets["Sheet2"]).Delete();
                ((Microsoft.Office.Interop.Excel.Worksheet)Excel.Worksheets["Sheet1"]).Delete();

            }
            catch (Exception e1)
            {
                MessageBox.Show("Error" + e1.Message, "Error!!!");
            }

            if (excelPath != null && excelPath != "")
            {
                try
                {
                    Worksheet.SaveAs(excelPath);
                    Excel.Quit();
                    MessageBox.Show("Output file is saved");
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                }
                catch (Exception ex)
                {
                    throw new Exception("Problem with File path." + ex.Message);
                }
                finally
                {
                    Marshal.ReleaseComObject(Worksheet);
                    Marshal.ReleaseComObject(Excel);
                    Worksheet = null;
                }
            }
            else
            {
                Excel.Visible = true;
            }
        }
        catch (Exception exc)
        {
            throw new Exception("Error in Exporting : " + exc.Message);
        }
    }

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

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