在C#,ASP.Net中读取Excel文件 [英] Reading Excel File in C#, ASP.Net

查看:106
本文介绍了在C#,ASP.Net中读取Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个应用程序,在其中我要在网格视图中显示数据集,我的应用程序的数据源来自.xl / .xlsx格式文件。我需要从工作表中仅检索2 0r 3列以显示在网格中。所以最终我必须在C#中读取.xl / .xlsx(我正在使用Microsoft Visual Studio 2010)。帮我解决这个问题。在此先感谢:)

I'm trying to create an app in which I've to display a dataset in a grid view, the data source for my app is from a .xl / .xlsx format files. I need to retrieve only 2 0r 3 columns from the sheet to display in the grid. So ultimately I have to read a .xl/.xlsx in C#(I'm using Microsoft Visual Studio 2010). Help me to sove this issue. Thanks in advance :)

推荐答案

您应该能够通过此链接中的信息弄清楚如何将数据读入.NET:



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



祝你好运:))
You should be able to figure out how to read data into .NET by using the information in this link:

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

So good luck to you :)


试试下面的代码....



Try below code....

if (txtVName1.Text != string.Empty && cmbVid1.SelectedIndex != 0)
           {
               string input = string.Empty;
               OpenFileDialog p1 = new OpenFileDialog();
               p1.Title = "Select an Excel File..";
               p1.Filter = "EXCEL files (*.xls)|*.xls|All files (*.*)|*.*";
               p1.InitialDirectory = "c:";
               try
               {
                   if (p1.ShowDialog() == DialogResult.OK)
                   {
                       input = p1.FileName;
                       if (input != string.Empty)
                       {

                           OleDbConnection oledbcnn = new OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + p1.FileName + "';Extended Properties=Excel 8.0;");
                           DataSet dsoledb = new DataSet();
                           oledbcnn.Open();
                           OleDbDataAdapter daoledb = new OleDbDataAdapter();
                           DataTable dt = oledbcnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                           daoledb.TableMappings.Add("Table", "Hello");
                           daoledb = new OleDbDataAdapter(@"select * from [Sheet1


,oledbcnn);
daoledb.Fill(dsoledb);
if (dsoledb.Tables [ 0 ]。列[ 0 ]。ColumnName == S No&& dsoledb .Tables [ 0 ]。列[ 1 ]。ColumnName == MacId
{
dsoledb.Tables [ 0 ]。Columns.Add( Key typeof string ));
dsoledb.Tables [ 0 ]。Columns.Add( Status typeof string ));
dsoledb.Tables [ 0 ]。Columns.Add( Decrypted MacId typeof string ));
dataGridView1.DataSource = dsoledb.Tables [ 0 ];

}
else
{
MessageBox.Show( 文件格式不匹配\ n文件应该有两列命名\S No\和\MacId \ RSBY 64k密钥生成);
return ;

}
}
}
}
catch (例外情况)
{
MessageBox.Show(ex.Message);
}
", oledbcnn); daoledb.Fill(dsoledb); if (dsoledb.Tables[0].Columns[0].ColumnName == "S No" && dsoledb.Tables[0].Columns[1].ColumnName == "MacId") { dsoledb.Tables[0].Columns.Add("Key", typeof(string)); dsoledb.Tables[0].Columns.Add("Status", typeof(string)); dsoledb.Tables[0].Columns.Add("Decrypted MacId", typeof(string)); dataGridView1.DataSource = dsoledb.Tables[0]; } else { MessageBox.Show("File Format Does Not Match \n File Should have Two Columns Named \"S No\" and \"MacId\" ", "RSBY 64k Key Generation"); return; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); }


这篇关于在C#,ASP.Net中读取Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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