本文介绍了在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 :)
推荐答案
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
这篇关于在C#,ASP.Net中读取Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!