使用C#将SQL Server中的两个表导出到Single Excel工作表

时间:2019-03-22 16:33:11

标签: c# sql-server excel console-application

我是C#的新手...我正在SQL Server中创建两个表到一个Excel工作表的应用程序。我成功将一个表导出到excel,但是我不知道将第二个表导出到同一Excel工作表,并且两个表在excel单元中的放置方式不同...所以无法加入我的代码下面的表帮我解决这个问题

class ExportToExcel
    {

        private Excel.Application app;
        private Excel.Workbook workbook;
        private Excel.Worksheet previousWorksheet;
        private static string CONNECTION_STR = "Integrated Security=True";

        public ExportToExcel()
        {
            this.app = null;
            this.workbook = null;
            this.previousWorksheet = null;
            createDoc();
        }

        private void createDoc()

        {
            try
            {
                app = new Excel.Application();

                app.Visible = false;
                workbook = app.Workbooks.Add(1);


            }
            catch (Exception e)
            {
                Console.Write(e.ToString());

            }
            finally
            {
            }
        }

        public void shutDown()

        {
            try
            {
                workbook = null;

                app.Quit();
            }
            catch (Exception e)

            {
                Console.Write(e.ToString());
            }

            finally
            {
            }
        }

        public void ExportTable(string query, string sheetName)

        {
            SqlConnection myConnection = new SqlConnection(CONNECTION_STR);
            SqlDataReader myReader = null;

            try
            {

                Excel.Worksheet oSheet = (Excel.Worksheet)workbook.Sheets.Add(Missing.Value, Missing.Value, 1, Excel.XlSheetType.xlWorksheet);
                oSheet.Name = sheetName;
                previousWorksheet = oSheet;
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand(query, myConnection);
                myReader = myCommand.ExecuteReader();

                int columnCount = myReader.FieldCount;
                int rowCounter = 3;
                while (myReader.Read())

                {
                    for (int n = 0; n < columnCount; n++)

                    {

                        addData(oSheet, rowCounter, n + 1, myReader[myReader.GetName(n)].ToString());

                    }
                    rowCounter++;
                }


            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }
            finally
            {
                if (myReader != null && !myReader.IsClosed)

                {
                    myReader.Close();
                }

                if (myConnection != null)

                {
                    myConnection.Close();
                }

                myReader = null;
                myConnection = null;
            }

        }


        public void createHeaders(Excel.Worksheet worksheet, int row, int col, string htext)

        {
            worksheet.Cells[row, col] = htext;


        }

        public void addData(Excel.Worksheet worksheet, int row, int col, string data)

        {
            worksheet.Cells[row, col] = data;

        }


        public void SaveWorkbook()
        {

            String folderPath = "C:\\My Files\\";

            if (!System.IO.Directory.Exists(folderPath))
            {

                System.IO.Directory.CreateDirectory(folderPath);

            }
            DateTime today = DateTime.Today;
            string fileNameBase = today.ToString("dd-MMM-yyyy");
            String fileName = today.ToString("dd-MMM-yyyy");
            string ext = ".xlsx";
            int counter = 1;

            while (System.IO.File.Exists(folderPath + fileName + ext))
            {

                fileName = fileNameBase + counter;
                counter++;
            }

            fileName = fileName + ext;

            string filePath = folderPath + fileName;

            try
            {
                workbook.SaveAs(filePath, Excel.XlFileFormat.xlWorkbookDefault, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }
        }


        static void Main(string[] args)

        {

            ExportToExcel export = new ExportToExcel();
            export.ExportTable("SELECT * from Table_1", "Data");
            export.SaveWorkbook();
            export.shutDown();
        }





    }

0 个答案:

没有答案