我无法动态打开我的excel文件,我的Excel工作表没有显示我的SQL表中显示的coloumn名称。 [英] I can not open my excel file dynamicly and my excel sheet does not show the coloumn name as shown in my SQL table.

查看:75
本文介绍了我无法动态打开我的excel文件,我的Excel工作表没有显示我的SQL表中显示的coloumn名称。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn;
            string connectionString = null;
            string sql = null;
            string data = null;
            int i = 0;
            int j = 0;

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            connectionString = "Data Source=VISHAL-PC; Initial Catalog=arif; Integrated security=true";
           
            cnn = new SqlConnection(connectionString);
            cnn.Open();
            sql = "SELECT * FROM khan1";
            SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
            DataSet ds = new DataSet();
            dscmd.Fill(ds);


            for (i = 0; i <= ds.Tables[0].Rows.Count -1; i++)
            {
                for (j = 0; j <= ds.Tables[0].Columns.Count -1; j++)
                {
                    

                    data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                   
                    
                        xlWorkSheet.Cells[i + 2, j + 1] = data;
                        
                }
            }
            
            xlWorkBook.SaveAs("E:\\Arif khan\\ted.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
           
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);

            MessageBox.Show("Excel file created , you can find the file  E:\\Arif khan\\ted.xls");


        }
        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }

        }
    }
}





我尝试过:





What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn;
            string connectionString = null;
            string sql = null;
            string data = null;
            int i = 0;
            int j = 0;

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            connectionString = "Data Source=VISHAL-PC; Initial Catalog=arif; Integrated security=true";
           
            cnn = new SqlConnection(connectionString);
            cnn.Open();
            sql = "SELECT * FROM khan1";
            SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
            DataSet ds = new DataSet();
            dscmd.Fill(ds);


            for (i = 0; i <= ds.Tables[0].Rows.Count -1; i++)
            {
                for (j = 0; j <= ds.Tables[0].Columns.Count -1; j++)
                {
                    

                    data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                   
                    
                        xlWorkSheet.Cells[i + 2, j + 1] = data;
                        
                }
            }
            
            xlWorkBook.SaveAs("E:\\Arif khan\\ted.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
           
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);

            MessageBox.Show("Excel file created , you can find the file  E:\\Arif khan\\ted.xls");


        }
        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }

        }
    }
}

推荐答案

你可以使用随附的DBQuery工具直接在Excel中执行此操作。以下是如何说明:

* 使用Microsoft Query检索外部数据 - Excel [ ^ ]

* 如何创建Excel查询! - YouTube [ ^ ]



或更高级的VBA方法:

* 使用VBA从Excel公式查询SQL Server数据库 - YouTube [ ^ ]
You can do this directly in Excel itself with the included DBQuery tool. Here are instructions on how:
* Use Microsoft Query to retrieve external data - Excel[^]
* How-To Create An Excel Query! - YouTube[^]

Or a more advanced VBA approach:
* Querying a SQL Server Database From an Excel Formula with VBA - YouTube[^]


这篇关于我无法动态打开我的excel文件,我的Excel工作表没有显示我的SQL表中显示的coloumn名称。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆