實際的開發中,我們會經常遇到數據的轉化的需要,將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;