大家好,最近被这个问题困扰,因为在ASp.net我没做过报表,看了很多网上的列子,但是很多在使用的时候都会在Com里配置---
Microsoft Excel类型库的引用:从"工程"菜单中选择"引用"栏;选择Microsoft Excel 8.0 Object Library;选择"确定"---
但是在我的电脑里又没有Microsoft Excel 8.0 Object Library----只有Microsoft Excel 11.0 Object Library和Microsoft Excel 5.0 Object Library   所以不能用Using Excle!!
我不知道要怎么做了   请大家帮帮忙!!!
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnStr"]);
conn.Open();
string str = "select [Name] as 姓名,Sex as 性别,Brithday as 出生日期,Branch as 工作部门,Instructor as 职能 from Basic_Message ";
SqlCommand comm = new SqlCommand(str, conn);//SqlDataAdapter dr=com.ExecuteReader();
SqlDataReader dr = comm.ExecuteReader(); Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null) { Response.Write("Can’t open Excel!"); return; }
xlApp.Application.Workbooks.Add(true); int row = 2, fieldcount;
//dr
fieldcount = dr.FieldCount; string[] strn = { "姓名", "性别", "出生日期","工作部门","职能"};
       
for (int col = 0; col < fieldcount; col++)
xlApp.Cells[1, col + 1] =strn[col].ToString();
        
while (dr.Read())
{
for (int col = 0; col < fieldcount; col++)
xlApp.Cells[row, col+1] = dr.GetValue(col).ToString();
row++;
}
xlApp.Visible = true;
xlApp = null;
conn.Close();

解决方案 »

  1.   

    Microsoft Excel 11.0 Object Library这个是office的版本问题,11.0应该是office2003,应该也可以直接用吧.不过估计命名空间有所改变
      

  2.   

    给你一段我的读取方法吧
    using System;
    using System.Data;
    using System.Data.OleDb;namespace ClassLibrarys
    {
        public class ExcelRead
        {
            public DataSet ExcelReader(string excelName)
            {
                // 拼写连接字符串,打开连接
                string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + excelName + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
                OleDbConnection objConn = new OleDbConnection(strConn);
                objConn.Open();
                // 取得Excel工作簿中所有工作表
                DataTable schemaTable = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                OleDbDataAdapter sqlada = new OleDbDataAdapter();
                DataSet ds = new DataSet();
                // 遍历工作表取得数据并存入Dataset
                foreach (DataRow dr in schemaTable.Rows)
                {
                    string strSql = "Select * From [" + dr[2].ToString().Trim() + "]";
                    OleDbCommand objCmd = new OleDbCommand(strSql, objConn);
                    sqlada.SelectCommand = objCmd;
                    sqlada.Fill(ds, dr[2].ToString().Trim());
                }
                objConn.Close();
                return ds;
            }
        }
    }
      

  3.   

    asp.net做报表的话,可以使用rdlc,这个功能很强大,可以导出excel,格式可以做的非常漂亮
    你在网上搜一下rdlc,很多例子
      

  4.   

    很谢谢你的帮忙,还给出了列子!很感谢!使用不一样的,我试过了  
    Microsoft Excel 11.0 Object Library 要用 using Microsoft.Office.Interop.Excel;可是引用成功了,但后面很多Excle库里自带的方法或属性就不能用了而且要改变项目的权限,但是访问还是拒绝!