当前位置:软件学习 > Excel >>

C#数据库数据导入导出系列之四 WinForm数据库导入导出到Excel

 

在日常的项目中,Excel,Word,txt等格式的数据导入到数据库中是很常见的,我在这里做一下总结

 

这里将分为Asp.net导入Sql Server,Oracle数据库和WinForm导入Sql Server,Oracle数据库。

 

这里将的数据库数据库导入导出,其实对Sql Server 和Oracle都是通用的

 

如果使用ADO.Net连接Oracle数据库,需要在引用里添加“System.Data.OracleClient ”,其他方面与连接Sql Server数据库是一样的

 

SqlConnection cn = new SqlConnection();

OracleConnection oraleCn = new OracleConnection();

 

如果使用诸如Ibatis等持久层框架的话,唯一的区别就是在数据库连接语句上的差别而已。下面是两个例子

 

Oracle:Data Source=192.168.0.11/Contact;User ID=system;Password=ss;Unicode=True

 

Sql Server:Data Source=Contact;Server=localhost;uid=sa;pwd=ss

 

1,数据库导出到Excel

 

先看界面

\

 

 

然后是代码

 

 

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.Diagnostics; 

using Microsoft.Office.Interop.Excel; 

using System.Reflection; 

using System.IO; 

using System.Data.SqlClient; 

using System.Data.OracleClient; 

 

namespace SqlServer__Excel 

    public partial class SqlDB_To_Excel : Form 

    { 

        public SqlDB_To_Excel() 

        { 

            InitializeComponent(); 

        } 

 

        private Microsoft.Office.Interop.Excel.Application myExcel = null; 

 

        private void button1_Click(object sender, EventArgs e) 

        { 

            print(dataGridView1); 

        } 

 

        public void print(DataGridView dataGridView1) 

        { 

            //导出到execl    

            try 

            { 

                SaveFileDialog saveFileDialog = new SaveFileDialog(); 

                saveFileDialog.Filter = "导出Excel (*.xls)|*.xls"; 

                saveFileDialog.FilterIndex = 0; 

                saveFileDialog.RestoreDirectory = true; 

                saveFileDialog.CreatePrompt = true; 

                saveFileDialog.Title = "导出文件保存路径"; 

                saveFileDialog.ShowDialog(); 

                string strName = saveFileDialog.FileName; 

                if(strName.Length != 0) 

                { 

                    //没有数据的话就不往下执行    

                    if(dataGridView1.Rows.Count == 0) 

                        return; 

 

                    // toolStripProgressBar1.Visible = true; 

                    System.Reflection.Missing miss = System.Reflection.Missing.Value; 

                    //实例化一个Excel.Application对象 

                    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); 

                    excel.Application.Workbooks.Add(true); 

                    excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。 

                    if(excel == null) 

                    { 

                        MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); 

                        return; 

                    } 

                    Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks; 

                    Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss)); 

                    Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet; 

      

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,