如何使用窗口应用程序将excel文档的数据导入数据库表? [英] how to import data of excel document to database table using window application?

查看:86
本文介绍了如何使用窗口应用程序将excel文档的数据导入数据库表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人请帮助我如何使用winform将数据从excel文档导入到数据库中的表格中。

Any one please help me how i import data from excel document to a table present in data base using winform.????

推荐答案

步骤
1)创建与数据库具有相同列的Datagridview。

2)读取Excel文件并将行和列数据添加到Datagridview

3)保存按钮和单击保存按钮,通过读取每一行,将数据从gridview插入到数据库中。

(对于inser,您可以使用for循环一次插入一行,或者可以使用Union all在所有行中插入所有行时间)



以下是上述步骤的参考资料:



阅读Excel文件数据: http://stackoverflow.com/questions/15828/reading-excel-files-from-c-sharp [ ^ ]


嗨ashu_dhiman



1.如果要将所有列导入sql数据库,请使用sqlbulkcopy并映射源列和目标列。

2.其次如果你想要选择要导入的值去Excel Interop类。



使用Excel Interop课程你可以做这样的事情



Hi ashu_dhiman

1.If you want to import all columns to sql database use sqlbulkcopy and map your source and destination columns.
2.Secondly if you want selected values to import go for Excel Interop classes.

with Excel Interop classes you can do something like this

public void parsefunction(string filepath)
    {
        Microsoft.Office.Interop.Excel.Application Exlapp;
        Microsoft.Office.Interop.Excel.Workbook Exlwkbk;
        Microsoft.Office.Interop.Excel.Worksheet Exlwksht;
        Microsoft.Office.Interop.Excel.Range ExlRng;
        Exlapp = new Microsoft.Office.Interop.Excel.ApplicationClass();
        //opening Excel file
        Exlwkbk = Exlapp.Workbooks.Open(@filepath,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
        Exlwksht = (Microsoft.Office.Interop.Excel.Worksheet)Exlwkbk.Sheets.get_Item(1);
        ExlRng = Exlwksht.UsedRange;
        ExlRng = Exlwksht.get_Range("A2","L2");//this is starting and ending index in Excel column..
         
        string a3;//declare some variables
        string b3;
        string c3;
        string d3;
        string e3;
        string f3;
        string g3;
        string h3;
        string i3;
        string j3;
        string k3;
       a3 = Exlwksht.get_Range("A3",Type.Missing).Value.ToString();//assign Index values to those variables you may also go for both the things(declaring and assigning) together.
       b3 = Exlwksht.get_Range("B3",Type.Missing).Value.ToString();
       c3= Exlwksht.get_Range("C3",Type.Missing).Value.ToString();
       d3 = Exlwksht.get_Range("D3",Type.Missing).Value.ToString();
       e3 = Exlwksht.get_Range("E3",Type.Missing).Value.ToString();
       f3 = Exlwksht.get_Range("F3",Type.Missing).Value.ToString();
       g3 = Exlwksht.get_Range("G3",Type.Missing).Value.ToString();
       h3 = Exlwksht.get_Range("H3",Type.Missing).Value.ToString();
       i3 = Exlwksht.get_Range("I3",Type.Missing).Value.ToString();
       j3 = Exlwksht.get_Range("J3",Type.Missing).Value.ToString();
       k3 = Exlwksht.get_Range("K3",Type.Missing).Value.ToString();
       SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["connstring"], "insertetartdata",a3,b3, c3, d3, e3,f3,g3,h3,i3,j3,k3 //now insert data into sql server using a sprocedure..
      //look I am using a different function(which i have not shown here) here for reading the path of Excel file''s location,you may go for a hardcoded location i.e specify the path here itself on the page..

Hope this may help you ..


这篇关于如何使用窗口应用程序将excel文档的数据导入数据库表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆