惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
小众软件
小众软件
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
G
Google Developers Blog
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
爱范儿
爱范儿
B
Blog
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
博客园 - 叶小钗
aimingoo的专栏
aimingoo的专栏
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
博客园_首页
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence

博客园 - 挖豆

关于iframe的滚动条,如何去掉水平滚动条或垂直滚动条 javascript得到网址中传递的参数 的处理函数 页面的焦点:window.onfocus javascript 实现随机数 解析 实现随页面滚动的层(最简单效果) 如何用代码设置滚动条的位置? javascript 网页注册快捷键的函数 关于javascript和c#中日期的两点比较--以防在使用ajax时出错 CSS中的overflow css实现圆角 关于Ajax.net 操作和访问 session application request ajax.net 的使用方法--摘自网上 ArrayList用法 最全的DOS命令集. 如何写批处理文件 读取数据库表的列名,表列的数据类型 ASP内置对象详细介绍 提供一个专业提供连接字符串的网站 Excel连接字符串
在.NET中用MICROSOFT的Excel.Application 部件读取Excel表
挖豆 · 2007-08-13 · via 博客园 - 挖豆

首先
在.NET的解决方案管理器中右键单击工程找到添加引用,在选项卡里选择COM   在列表里找到Excelxx(xx是版本号有9.0;10.0等,对了一定要先安装Excel啊,否则找不到的),选择确定,然后就可以在你的程序中Excel.ApplicationClass了

string excelFilePath = @"C:\Inetpub\wwwroot\news\test.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;
                Response.Write("--"+aa+"--");

              //  string columnname = "F" + j.ToString();
                //myRow[columnname] = strValue;
            }
            Response.Write("</br>");
            //myDs.Tables["mytable"].Rows.Add(myRow);
        }
        //DataGrid1.DataSource = myDs.Tables["mytable"].DefaultView;
       // DataGrid1.DataBind();