对于把Excel作为数据源,你的Excel的Sheet名称必须是Sheet1,因为你在代码中已经限定了。

解决方案 »

  1.   

    还有一个问题想请教高手,我把EXCEL里面的数据读到dataset里面怎么样把DATASET里面的数据读到数据库中呢
      

  2.   

    偶用的是office的COM+组件,不过非常难用,而且找不到sdk,服务器也需要装相应的office组件
    优点是可以任意操作表。
    相关文件如下:
    Interop.Excel.dll版本1.5.0.0
    Microsoft.Vbe.Interop.dll版本11.0.5530
    Office.dll版本11.0.5530
    代码如下:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using losingrose;public partial class DB_import : System.Web.UI.Page
    {
        ArticleAS bll = new ArticleAS();
        UsersAS users = new UsersAS();
        protected void Page_Load(object sender, EventArgs e)
        {    }
        private DateTime ConvertExcelDateToDate(string excelDate)
        {
            DateTime march1st1900 = new DateTime(1900, 03, 01);
            DateTime december31st1899 = new DateTime(1899, 12, 31);
            TimeSpan after1stMarchAdjustment = new TimeSpan(1, 0, 0, 0);
            TimeSpan ts = TimeSpan.Parse(excelDate);
            DateTime dt = december31st1899 + ts;
            if (dt >= march1st1900)
            {
                return dt - after1stMarchAdjustment;
            }
            return dt;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime start = DateTime.Now;
                MyFileOp.del_file("~/excel/import.xls");
                MyFileOp.up_file(FileUpload1.PostedFile, "~/excel", "import.xls");            DataTable table = new DataTable();
                System.Reflection.Missing miss = System.Reflection.Missing.Value;
                Excel.ApplicationClass excelAS = new Excel.ApplicationClass();
                excelAS.Visible = false;
                Excel.Workbooks workbooks = excelAS.Workbooks;
                string filename = Server.MapPath("~/excel/import.xls");
                Excel.Workbook workbook = workbooks.Open(filename, miss, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing);
                Excel.Worksheet article = (Excel.Worksheet)workbook.Worksheets[2];
                Excel.Range rag;
                string[] dr = new string[26];
                dr[0] = "A";
                dr[1] = "B";
                dr[2] = "C";
                dr[3] = "D";
                dr[4] = "E";
                dr[5] = "F";
                dr[6] = "G";
                dr[7] = "H";
                dr[8] = "I";
                dr[9] = "J";
                dr[10] = "K";
                dr[11] = "L";
                dr[12] = "M";
                dr[13] = "N";
                dr[14] = "O";
                dr[15] = "P";
                DataRow row;
                int x = 1;            for (; x < article.UsedRange.Rows.Count; x++)
                {                if (x == 1)
                    {
                        for (int j = 0; j < article.UsedRange.Columns.Count; j++)
                        {
                            rag = article.get_Range((object)(dr[j] + x.ToString()), miss);
                            table.Columns.Add(rag.Value2.ToString());
                        }
                        x++;
                    }
                    row = table.NewRow();
                    for (int j = 0; j < article.UsedRange.Columns.Count; j++)
                    {
                        rag = article.get_Range((object)(dr[j] + x.ToString()), miss);
                        if (table.Columns[j].ColumnName == "ar_intime" || table.Columns[j].ColumnName == "ar_datetime")
                            row[j] = ConvertExcelDateToDate(rag.Value2.ToString()).ToString();
                        else
                            row[j] = rag.Value2.ToString();
                    }
                    table.Rows.Add(row);
                }            workbook.Close(false, Server.MapPath("~/excel/import.xls"), miss);
                workbooks.Close();
                excelAS.Quit();            System.Runtime.InteropServices.Marshal.ReleaseComObject(article);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelAS);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excelAS);
                rag = null;
                article = null;
                workbooks = null;
                workbook = null;
                excelAS = null;
                GC.Collect();
                DateTime end = DateTime.Now;
                System.Text.StringBuilder resualt = new System.Text.StringBuilder("输出完毕\n");
                resualt.AppendLine("--------------------------------");
                resualt.AppendLine("开始时间:" + start.ToLongTimeString());
                resualt.AppendLine("结束时间:" + end.ToLongTimeString());
                resualt.AppendLine("花费时间:" + ((TimeSpan)(end - start)).TotalSeconds + "秒");
                if (x == 1)
                    resualt.AppendLine("没有查找到匹配的记录");
                else
                    resualt.AppendLine("成功导入" + (x - 2) + "条记录\n");
                TextBox1.Text = resualt.ToString() + TextBox1.Text;            GridView1.DataSource = table;
                GridView1.DataBind();            for (int i = 0; i < table.Rows.Count;i++ )
                    updata(table.Rows[i]);
                table = null;
            }
            catch (Exception error)
            {
                MyScript.Show(this, "导出excel出错:" + error.Message);
            }
        }
        private void updata(DataRow row)
        {
            bll.article_edit(row["ar_id"].ToString(), users.user_id_sel(row["us_account"].ToString()), row["ar_title"].ToString(), row["ar_source"].ToString(), bll.su_id_sel(row["su_code"].ToString()), bll.section_id_sel(row["se_name"].ToString()), row["ar_datetime"].ToString(), row["ar_content"].ToString(), row["ar_level"].ToString());
        }
    }
      

  3.   

    看得不是很懂,能不能贴个.net操作excel的例子谢谢。