在datagridview中从excel导入bill的内容 [英] Import contents of bill from excel in datagridview

查看:89
本文介绍了在datagridview中从excel导入bill的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取excel中账单中的数据

跳过标题内容并在datagridview中添加该数据。我怎么能这样做?

我创建了一个窗体。

I want to extract the data present in the bill which is in excel
Skipping the header content and add that data in datagridview. How can I do it?
I have created a windows form.

推荐答案

为了阅读Excel,官方微软有很多例子谷歌搜索应该为您提供的文档:



http:// support.microsoft.com/kb/302084 [ ^ ]



您也可以使用OLEdb连接。
For reading Excel there are lots of examples from the official microsoft documentation which a google search should provide for you:

http://support.microsoft.com/kb/302084[^]

You could use OLEdb connection also.


<br />
<pre>using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Data.OleDb;<br />
<br />
namespace Import_Excel_file_into_DataGridView<br />
{<br />
    public partial class Form1 : Form<br />
    {<br />
        public Form1()<br />
        {<br />
            InitializeComponent();<br />
        }<br />
<br />
        private void btnBrowse_Click(object sender, EventArgs e)<br />
        {<br />
            OpenFileDialog dlg = new OpenFileDialog();<br />
            DialogResult dlgResult = dlg.ShowDialog();<br />
            if (dlgResult == DialogResult.OK)<br />
            {<br />
                txtPath.Text = dlg.FileName;<br />
            }<br />
        }<br />
<br />
        private void btnLoadData_Click(object sender, EventArgs e)<br />
        {<br />
            if (System.IO.File.Exists(txtPath.Text))<br />
            {<br />
                string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", txtPath.Text);<br />
                string query = String.Format("select * from [{0}


,Sheet1);< br />
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query,connectionString);< br />
DataSet dataSet = new DataSet() ;< br />
dataAdapter.Fill(dataSet);< br />
dataGridView1.DataSource = dataSet.Tables [0]; < br />
}< br />
else< br />
{< br />
MessageBox.Show(No File is Selected); < br />
}< br />
}< br />
}< br />
}< / pre>< br />
< br />
", "Sheet1");<br /> OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);<br /> DataSet dataSet = new DataSet();<br /> dataAdapter.Fill(dataSet);<br /> dataGridView1.DataSource = dataSet.Tables[0]; <br /> }<br /> else<br /> {<br /> MessageBox.Show("No File is Selected");<br /> }<br /> }<br /> }<br /> }</pre><br /> <br />


这篇关于在datagridview中从excel导入bill的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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