这是导出的代码,我以前写的,可以参考一下
public delegate void ProgressChangedEventHander(int current, int total);
        public event ProgressChangedEventHander ProgressChanged;        private void OnProgressChanged()
        {
            if (ProgressChanged != null)
            {
                ProgressChanged(m_current, m_total);
            }
        }        private int m_total = 0;        private int m_current = 0;        public bool ExportExcel(string path, DataTable table)
        {
            exportCheck(path, table);
            m_total = table.Rows.Count;
            Excel.ApplicationClass app = null;
            Excel.Workbook book = null;
            Excel.Worksheet sheet = null;
            try
            {
                app = new Excel.ApplicationClass();
                book = app.Workbooks.Add(Missing.Value);
                sheet = book.Worksheets[1] as Excel.Worksheet;
                Excel.Range myR = sheet.UsedRange;                object[,] context = getContext(table);
                myR = sheet.get_Range("A1", myR[table.Rows.Count, table.Columns.Count]);
                myR.Value2 = context;                if (table.TableName != "")
                {
                    sheet.Name = table.TableName;
                }
                if (Path.GetExtension(path) != ".xls")
                {
                    path += ".xls";
                }
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                sheet.SaveAs(path, Excel.XlFileFormat.xlExcel9795, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            }
            finally
            {
                sheet = null;
                if (book != null)
                {
                    book.Close(false, null, null);
                    book = null;
                }
                if (app != null)
                {
                    app.Workbooks.Close();
                    app.Quit();
                    app = null;
                }
                GC.Collect();
            }
            return true;
        }
        public bool ExportCsv(string path, DataTable table, bool isAppend)
        {
            return ExportCsv(path, table, isAppend, Encoding.Default);
        }
        public bool ExportCsv(string path, DataTable table, bool isAppend, Encoding encode)
        {
            exportCheck(path, table);            StreamWriter myWirter = null;
            m_total = table.Rows.Count;
            m_current = 0;
            try
            {
                if (Path.GetExtension(path) != ".csv")
                {
                    path += ".csv";
                }
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                myWirter = new StreamWriter(path, isAppend, encode);
                StringBuilder context = null;
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    context = new StringBuilder();
                    for (int j = 0; j < table.Columns.Count; j++)
                    {
                        string data = "";
                        if (table.Rows[i][j] != null)
                        {
                            data = table.Rows[i][j].ToString();
                            if (data.Contains(","))
                            {
                                data = "\"" + data.Replace("\"", "\"\"") + "\"";
                            }
                        }
                        context.Append(data);
                        context.Append(",");
                    }
                    if (context.Length > 0)
                    {
                        myWirter.WriteLine(context.Remove(context.Length - 1, 1).ToString());
                    }
                    else
                    {
                        myWirter.WriteLine();
                    }
                    m_current = i + 1;
                    OnProgressChanged();
                }
                myWirter.Flush();
            }
            finally
            {
                myWirter.Close();
            }
            return true;
        }        public bool ExportCsv(string path, string context, bool isAppend)
        {
            return ExportCsv(path, context, isAppend, Encoding.Default);
        }        public bool ExportCsv(string path, string context, bool isAppend, Encoding encode)
        {
            exportCheck(path, context);            StreamWriter myWirter = null;
            try
            {
                if (Path.GetExtension(path) != ".csv")
                {
                    path += ".csv";
                }
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                myWirter = new StreamWriter(path, isAppend, encode);
                myWirter.Write(context);
                myWirter.Flush();
            }
            finally
            {
                myWirter.Close();
            }
            return true;
        }        private object[,] getContext(DataTable data)
        {
            object[,] obj = new object[data.Rows.Count, data.Columns.Count];
            for (int i = 0; i < data.Rows.Count; i++)
            {
                for (int j = 0; j < data.Columns.Count; j++)
                {
                    obj[i, j] = data.Rows[i][j];
                }
                m_current = i + 1;
                OnProgressChanged();
            }
            return obj;
        }        private void exportCheck(string path, DataTable table)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }
            if (table == null || table.Columns.Count == 0)
            {
                throw new ArgumentNullException("table");
            }
            string m_directory = Path.GetDirectoryName(path);
            if (!Directory.Exists(m_directory))
            {
                Directory.CreateDirectory(m_directory);
            }
        }        private void exportCheck(string path, string context)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }
            if (string.IsNullOrEmpty(context))
            {
                throw new ArgumentNullException("context");
            }
            string m_directory = Path.GetDirectoryName(path);
            if (!Directory.Exists(m_directory))
            {
                Directory.CreateDirectory(m_directory);
            }
        }

解决方案 »

  1.   


    忘了说了
    我那中写法你需要添加一个引用
    Microsoft.Office.Interop.Excel
      

  2.   

     我用DataGrid导出,很简单
       public void ExportGridToExcel(DataGrid grid, string sFileName)
        {        Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + sFileName);
            Response.ContentType = "application/excel";        StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grid.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
     
      

  3.   

    用ADO导入
    String file=""        DataSet ss = new DataSet();
            OleDbConnection mConn=null;
                string connstr = @"provider=microsoft.jet.oledb.4.0;extended properties=""excel 8.0;IMEX=1;hdr=no"";data source=" + file;
    ;
                string sSql = "select * from [sheet1$]";
                System.Data.DataTable Soe = CreateSoeTable();
                ss.Tables.Add(Soe);
                System.Data.DataTable station = CreateStationTable();
                ss.Tables.Add(station);
                System.Data.DataTable Job = CreateJobTable();
                ss.Tables.Add(Job);            DataTable ExcelDt = new DataTable();
                mConn = new OleDbConnection(connstr);
                mConn.Open();
                DataTable tableName = mConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, new object[] { });
                if (tableName.Rows[0][11].ToString() == "1")
                    sSql = "select * from [" + tableName.Rows[0][2].ToString() + "]";
                System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter(sSql, mConn);
                da.Fill(ExcelDt);
    下面不写了就是table读取.