当前位置:数据库 > SQLServer >>

C#将SQL数据库中数据导入Excel中,并将Excel中反导入SQL数据库中

\

实际的开发中,我们会经常遇到数据的转化的需要,将Excel中的数据转入到SQL中,或将SQL在数据库表中的数据导入到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 GemBox.ExcelLite;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Collections;
using System.IO;

namespace Encryption
{
    public partial class ExcelDemo : Form
    {
        private static string _filePath = string.Empty;
                
        public ExcelDemo()
        {
            InitializeComponent();
            BindUser();
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindUser()
        {
            string sql = "select * from Users";
            DataTable dt = DbHelperSQL.QueryTb(sql);
            dataGridView1.DataSource = dt;
        }
        /// <summary>
        /// 将Users表中的数据导入Excel中
        /// </summary>
        private void btnExcelin_Click(object sender, EventArgs e)
        {
            ExcelFile excelFile = new ExcelFile();
            ExcelWorksheet sheet = excelFile.Worksheets.Add("Users");

            int columns = dataGridView1.Columns.Count;
            int rows = dataGridView1.Rows.Count;

      

补充:软件开发 , C# ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,