论坛上搜索了不少,但没找到合适自己的。我想是点击一个按钮,弹出OpenFileDialog,让用户选择文件,点确定后自动导入数据库。
Excel 表字段为:线路,部门,行向,起点,终点
数据库表字段为:线路,行向,起点,终点   (少了部门)
请问怎样编程实现

解决方案 »

  1.   

    先导入datatable然后再入库. 
    很容易的.论坛上搜一个一般就能用.怎么可能都不能用呢
      

  2.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Data.OleDb;namespace WindowsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                string fileName = GetExcelPath();
                DataTable dt = doImport(fileName);
                MessageBox.Show(dt.Rows.Count.ToString());
                //后面的代码我就不写了。只要把dt入库就可以了。        }        private string GetExcelPath()
            {
                using (OpenFileDialog dlgFile = new OpenFileDialog())
                {
                    dlgFile.CheckFileExists = false;
                    dlgFile.Filter = "Excel files|*.xls|All files|*.*";
                    if (dlgFile.ShowDialog() == DialogResult.OK)
                    {
                        return dlgFile.FileName;
                    }                return string.Empty;
                }
            }        private DataTable doImport(string strFileName)
            {
                if (strFileName == "") return null;
                string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + strFileName + ";Extended Properties=Excel 8.0";
                OleDbDataAdapter excelDA = new OleDbDataAdapter("select * from [Sheet1$]", strConn);//连接字符串
                DataTable ExcelDt = new DataTable();//建立datatable,用于存放导入Excel的数据
                try
                {
                    excelDA.Fill(ExcelDt);
                }
                catch (Exception ee)
                {
                    System.Windows.Forms.MessageBox.Show(ee.Message);
                }
                return ExcelDt;
            }    }
    }
      

  3.   

    首先感谢你在 如何编程实现将Excel表数据导入SQL Server 2000数据库的回答。 
    我已成工导入,但我现在有一个问题,就是将数据库的数据读到ListView时,显示时每个项后面都有省略号。(宽度已足够,但仍有省略号)。 
    有什么办法去除省略号吗?