教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 asp.net 动态获取Excel表名(第一个表)的代码

asp.net 动态获取Excel表名(第一个表)的代码

发布时间:2015-12-29   编辑:jiaochengji.com
为大家介绍asp.net 动态获取excel中的表名的方法,我们先来看一个asp.net获取excel第一个表名的例子,然后再看一段动态获取excel表名的代码。两者相比较,可以加深理解。

为大家介绍asp.net 动态获取excel中的表名的方法,我们先来看一个asp.net获取excel第一个表名的例子,然后再看一段动态获取excel表名的代码。
两者相比较,可以加深理解。
 

复制代码 代码示例:

//获取excel第一个表名
public string GetExcelFirstTableName(string excelFileName)
{
string tableName = null;
if (File.Exists(excelFileName))
{
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet." +
"OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + excelFileName))
{
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
for (int i = 0; i < dt.Rows.Count; i++)
{
tableName += dt.Rows[i][2].ToString().Trim();
view sourceprint?1 }
}
}
return tableName;
}

//读取excel动态获取表名
string a=File1.PostedFile.FileName.ToString();
string excelFilePath=a;
Excel.Application myExcel=new Excel.ApplicationClass( ) ;
object oMissing = System.Reflection.Missing.Value ;
myExcel.Application.Workbooks.Open(excelFilePath,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing) ;
Excel.Workbook myBook = myExcel.Workbooks[1] ;
Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1] ;
Response.Write(mySheet.Name);
string Name=mySheet.Name;
System.Data.DataTable dt=new System.Data.DataTable("mytable");
dt.Columns.Add("F1", System.Type.GetType("System.String"));
dt.Columns.Add("F2", System.Type.GetType("System.String"));
dt.Columns.Add("F3", System.Type.GetType("System.String"));
dt.Columns.Add("F4", System.Type.GetType("System.String"));
dt.Columns.Add("F5", System.Type.GetType("System.String"));
DataSet myDs = new DataSet();
myDs.Tables.Add(dt);
DataRow myRow;
myDs.Clear();
for( int i = 2 ; i <= 4 ; i ++ ) //第一行为标题,不读取
{
myRow = myDs.Tables["mytable"].NewRow();
for( int j = 1 ; j <= 5 ; j ++ )
{
Excel.Range r=(Excel.Range)mySheet.Cells[i,j];
string strValue=r.Text.ToString();
string aa=strValue;
string columnname="F"+j.ToString();
myRow[columnname]=strValue;
}
myDs.Tables["mytable"].Rows.Add(myRow);
// }
myExcel.Quit();
DataGrid1.DataSource=myDs.Tables["mytable"].DefaultView;
DataGrid1.DataBind();

您可能感兴趣的文章:
asp.net 动态获取Excel表名(第一个表)的代码
php框架laravel excel包使用教程介绍
在C#.net 中读取EXCEL文件的数据
python怎么读取和写入excel表格
Farpoint操作excel的用法举例
c#.net 读取Excel数据到DataTable中的代码
php写excel文件的实现代码
C#读取excel数据的简单实例
PHP通过PHPExcel类导入导出excel
python使用pandas处理excel的方法

[关闭]
~ ~