已有56人关注
c#典型模块精讲,导出到excel表中,貌似得不到结果
发表在C#图书答疑 2013-01-12
是否精华
版块置顶:
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 Microsoft.Office.Interop.Excel;
using System.IO;
using Office = Microsoft.Office.Core;
using System.Resources;



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



        private void Form1_Load(object sender, EventArgs e)
        {
            groupBox1.Text = "数据表名称:";
            string selectSQL = "select * from [教务管理系统].[dbo].[学生信息] left join[教务管理系统].[dbo].[课程信息] on [教务管理系统].[dbo].[学生信息].[班级编号]=[教务管理系统].[dbo].[课程信息] .[班级编号]";
            string connectionstring = "Data Source=HU-PC\\HULINLIN;Initial Catalog=教务管理系统;User ID=sa;Password=123456";
            SqlConnection sqlconnection = new SqlConnection(connectionstring);
            SqlDataAdapter adapter = new SqlDataAdapter(selectSQL, sqlconnection);
            sqlconnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            if (ds.Tables[0].Rows.Count != 0)
            {
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }

            else
                dataGridView1.DataSource = null;
      sqlconnection.Close(); ;

                    }
   


        private void exportdata_Click(object sender, EventArgs e)
        {public void exportdata1(DataGridView sredgv, string filename)
        { string type = filename.Substring(filename.IndexOf(".")+1);
            if(type.Equals("xls",StringComparison.CurrentCultureIgnoreCase))
            { Excel.application excel = new Excel.application();
            try 
            { excel.displayalter = false;
            excel.workbooks.add(true);
            excel.visible = false;
                for(int i=0;i<dataGridView1.Columns.Count;i++)
                {excel.cell[2,i+1]=dataGridView1.Columns[i].HeaderText;}
                
                for(int i=0;i<dataGridView1.Rows.Count;i++)
                {  for(int j=0;j<dataGridView1.Columns.Count;j++)
                    excel.cell[i+19][j+1]=dataGridView1[j,i].Value;}
                   excel.workbooks[1].savecopyas(filename);}
        
             finally
              {excel.quit();}
            return;}
        }

        }
        
        
        
    
    
    
}
       
}   
分享到:
精彩评论 1
小科_mrkj
学分:43 LV2
2013-01-14
沙发
读者朋友:
    您好,我刚才测试了一下,程序功能是好使的,你用的时候出现什么问题啊。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照