在ASP.NET中使用EXCEL之二 读取Excel文件
来源:互联网 发布:傲慢与偏见 知乎 编辑:程序博客网 时间:2024/05/22 01:12
设置权限之后,就可以使用ASP.NET读取Excel文件了。
首先创建一个aspx文件,在页面中加入一个Button和一个DataGrid控件。
在工程的引用中添加Excel引用,并将测试的电子表格文件放到D盘中。
在CS文件中的Button1_Click事件中输入如下代码:
private void Button1_Click(object sender, System.EventArgs e)
{
string excelFilePath=@"D:/Book1.xls";
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] ;
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);
}
DataGrid1.DataSource=myDs.Tables["mytable"].DefaultView;
DataGrid1.DataBind();
}
- 在ASP.NET中使用EXCEL之二 读取Excel文件
- 在ASP.NET中使用EXCEL之二 读取Excel文件
- 在ASP.NET中使用EXCEL之三 写Excel文件
- ASP.NET中读取Excel文件数据
- ASP.NET生成EXCEL文件 之 二
- ASP.NET读取Excel文件
- asp.net 读取excel文件
- 在ASP.NET中使用Excel提供的类读取excel数据
- 在asp.net中调用Excel 文件
- 在ASP.NET中访问Excel文件
- 在ASP.NET中读写Excel文件
- 在asp.net中调用Excel 文件
- 在asp.net中使用excel模板
- 在ASP.NET中使用EXCEL
- 在asp.net中使用excel模板
- 在asp.net中使用excel模板
- 在asp.net中使用excel模板
- 在asp.net中使用excel模板
- JFreeChart 高手请进!
- Struts 源码学习笔记
- 迎接MM Studio 8的到来
- 今天是并行的日子
- 什么是shell
- 在ASP.NET中使用EXCEL之二 读取Excel文件
- EJB3.0初步之环境配置
- XCodeFactory QQ群正式开通!群号码:2136707
- 創建一個簡單的"專家"
- WordNet
- 在ASP.net中使用static变量
- 经典的分页(存储过程)
- php的几个配置文件函数
- [求助]如何快速获得应用程序图标?