我现在想:     1.批量从excel中把数据导入到 html中 或(aspx)      2.批量导入winform到datagridview中
     怎么搞呢 有了点思路但是还不能实现 
      交流哈

解决方案 »

  1.   

    第一个,把excel读取出来,以xml格式化数据,再以html显示出来。
      

  2.   

    有详细的Excel转xml的例子没有》?
      

  3.   

      //导入excel的方法
        private DataSet CreateDataSource() 
        {
            string strCon; 
            strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("1.xls") + "; Extended Properties=Excel 8.0;";
            OleDbConnection olecon = new OleDbConnection(strCon); 
            OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon); 
            DataSet myds = new DataSet(); myda.Fill(myds); return myds; }   
            protected void Button1_Click(object sender, EventArgs e)
        {
           //调用该方法,并绑定数据
            GridView2.DataSource = CreateDataSource(); GridView2.DataBind();
        }
      

  4.   

     protected void btn2_Click(object sender, EventArgs e)
        {
            //调用Export方法(导出文件的类型,文件的名字)
            Export("application/ms-excel", "用户信息表.xls");
        }    //导出excel表
        public void Export(string filetype, string filename)
        {
            //设置格式
            Response.Charset = "GB2312";
            //设置输出流的字节
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            //将http添加到输出流
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename).ToString());
            //设置输出文件的类型
            Response.ContentType = filetype;
            this.EnableViewState = false;
            //创建写的流
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            GridView1.RenderControl(hw);
            //写入流
            Response.Write(tw.ToString());
            //结束写的流
            Response.End();
        }
        //导出一个流的时候 必须重新这个方法
        public override void VerifyRenderingInServerForm(Control control)
        {
        }
      

  5.   

    我就正在做这样的东西。哈哈。using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using Microsoft.Office;
    using Excel = Microsoft.Office.Interop.Excel;
    using System.Reflection;
    using System.Net;
    using System.Windows.Forms;namespace DmppDeployConfig
    {
       
        public class DeployInfo
        {
            public string m_strError;
            //部署版本信息
            public int nConfVersion = 1; //0x01 电信版本, 0x02 移动版本,0x03 其它版本      //  public List<SERVER_INFO> m_ServerList;
          //  public List<SERVER_INFO> m_DBServerList;
          //  public ONLY_PROCESS_INFO m_OnlyProcess;
          //  public List<PROBE_INFO> m_ProbeList;
          //  public List<REMOTE_INFO> m_RemoteList;        private Excel.Application ObjApp;
            private Excel.Sheets ObjSheets;//定义一个工作表组 
            private Excel._Worksheet ObjSheet;//定义一个工作表         public string m_strFullPath;        public string m_strWorkDirectory;        public DeployInfo()
            {
             //   m_ServerList = new List<SERVER_INFO>();
             //   m_DBServerList = new List<SERVER_INFO>();
             //   m_OnlyProcess = new ONLY_PROCESS_INFO();
             //   m_ProbeList = new List<PROBE_INFO>(); ;
             //   m_RemoteList = new List<REMOTE_INFO>();
            }               public DeployInfo(string file)
           {
               m_strFullPath = file;
            }        public bool ReadDeployDoc(string strFn)
            {
               
                bool bRet = true;            try
                {
                    ObjApp = new Excel.ApplicationClass();//创建一个Excel对象 
                    ObjApp.Workbooks.Open(strFn, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                    ObjSheets = ObjApp.Worksheets;//引用工作簿中的工作表              //  if(!ReadServerInfo() || !ReadProbeInfo() || !ReadRemoteInfo())
                  //      bRet = false;                int count = ObjSheets.Count;              //  ObjApp.Workbooks.Close();
                 //   ObjApp.Quit();
                }
                catch (System.Exception e)
                {
                    ObjApp.Quit();
                    m_strError = "Read deploy document fail!\n";
                    m_strError += e.ToString();
                    bRet = false;
                }            ObjApp = null;
                return bRet;
            }        public bool CreateDirectory(string direcory)
            {
                //判断文件夹是否存在
                if (Directory.Exists(direcory))
                    return true;
                //如果不存在则进行创建
                else
                {
                    Directory.CreateDirectory(direcory);
                    return true;
                }      
            }        
            public bool GetDicts()
            {
                
                int count= ObjSheets.Count;
                //读取excel中内容生成ldif文件
                StreamWriter fs,fssysobjects;
                bool bFound;            string FieldName = "";
                string type = "";
                string length = "";
                string strTmp = "";
                string tableid="9999999";            int nRow;
                int id = 0;            CreateDirectory(m_strWorkDirectory + "\\dict\\");
                //依次读入每一个sheet
                for (int i = 0; i < count; i++)
                {
                    ObjSheet = (Excel._Worksheet)ObjSheets.get_Item(i+1);                if (Excel.XlSheetVisibility.xlSheetHidden == ObjSheet.Visible)
                        continue;               
                       
                    string SheetName = ObjSheet.Name;
                    //判断是否为字典
                    if ((SheetName.IndexOf("cdr") == -1) && (SheetName.IndexOf("CDR") == -1) && (SheetName.IndexOf("stat") == -1) && (SheetName.IndexOf("STAT") == -1) && (SheetName.IndexOf("CDR") == -1) && (SheetName.IndexOf("bdr") == -1) && (SheetName.IndexOf("BDR") == -1) && (SheetName.IndexOf("warn") == -1) && (SheetName.IndexOf("WARN") == -1)) 
                       continue;
                    else
                    {                   
                       // fssysobjects = new StreamWriter("c:\\tmp\\dict\\sysobjects-ddict-" + SheetName + ".ldif");
                        fssysobjects = new StreamWriter(m_strWorkDirectory + "\\dict\\sysobjects-ddict-" + SheetName + ".ldif");
                        fssysobjects.Write("dn: sysobjectName=ddict-"+SheetName+",subOptionName=sysobjects,dc=zctt\n");
                        fssysobjects.Write("objectClass: sysobject\n");
                        fssysobjects.Write("configureDN: subOptionName=ddict-"+SheetName+",subOptionName=dyna-dict,subOptionName=dictionary,subOptionName=config-instance,subOptionName=dmp,dc=zctt\n");
                        fssysobjects.Write("sysobjectName: ddict-" + SheetName+"\n");
                        fssysobjects.Close();
                        //fs = new StreamWriter(m_strFullPath + "/"+SheetName+".ldif");
                       // fs = new StreamWriter("c:\\tmp\\dict\\ddict-" + SheetName + ".ldif");
                        fs = new StreamWriter(m_strWorkDirectory + "\\dict\\ddict-" + SheetName + ".ldif");
                        //读取每一行
                        fs.Write("dn: subOptionName=ddict-"+SheetName+",subOptionName=dyna-dict,subOptionName=dictionary,subOptionName=config-instance,subOptionName=dmp,dc=zctt\n");
                        fs.Write("objectClass: objectClassDict1\n");
                        fs.Write("element: yes\n");
                        fs.Write("subOptionName: ddict-"+SheetName+"\n");
                        fs.Write("time-field-name-element: start_time\n");
                        
                        if ((SheetName.IndexOf("stat")!=-1) || (SheetName.IndexOf("STAT")!=-1))
                            fs.Write("type-class-element: SAR\n");
                        else fs.Write("type-class-element: CDR\n");
                                            bFound = false; 
                       
                        for (nRow =0; nRow <100; nRow++)
                        {
                            //找到列名或字段名
                            if (!bFound)
                            {
                                ReadItemText(nRow, 0,  ref strTmp);
                                if (strTmp == null) continue;
                                if (strTmp.Equals("列名") || strTmp.Equals("字段名"))
                                {
                                    bFound = true;
                                    ReadItemText(nRow-1 ,1, ref tableid);
                                    if (tableid == null)
                                    {
                                        MessageBox.Show("请输入" + "table-id:" + SheetName);
                                        return false;
                                    }
                                    fs.Write("type-id-element: " + tableid + "\n");
             
                                    fs.Write("type-name-element: ddict-"+SheetName+"\n");
                                  
                                    fs.Write("version-element:090627\n");
                                    fs.Write("\n");                                fs.Write("dn:subOptionName=record,subOptionName=ddict-" + SheetName + ",subOptionName=dyna-dict,subOptionName=dictionary,subOptionName=config-instance,subOptionName=dmp,dc=zctt\n");
                                    fs.Write("objectClass: objectClassDict2\n");
                                    fs.Write("subOptionName: record\n\n");
                                    id = 0;
                                    continue;                            }
                                else { id = 0; continue; };
                            }
                            //提取字段
                            else
                            {
                               
                                ReadItemText(nRow, 0, ref FieldName);
                                if (FieldName == null) break;
                                ReadItemText(nRow, 2, ref type);
                                ReadItemText(nRow, 3, ref length);                            fs.Write("dn: identifier=" + (id+1).ToString() + "+subOptionName=field,subOptionName=record,subOptionName=ddict-"
                                    +SheetName+",subOptionName=dyna-dict,subOptionName=dictionary,subOptionName=config-instance,"
                                    +"subOptionName=dmp,dc=zctt\n");
                                fs.Write("objectClass: objectClassDict3\n");
                                fs.Write("field-name:"+FieldName+"\n");
                                fs.Write("id:" + id.ToString() + "\n");
                                fs.Write("identifier:" + (id+1).ToString() + "\n");
                                fs.Write("subOptionName: field\n");                            if (type.IndexOf("ip") == -1 && type.IndexOf("IP") == -1)
                                    fs.Write("type:" + type + "\n");
                                else
                                    fs.Write("subtype: ipv4addr\ntype: unsignedInt\n");                            if (type == null) { type = "int";}
                                if (type.Equals("string") || type.Equals("binary")) fs.Write("length:" + length + "\n");                                                      fs.Write("\n");                            id++;                        }
                        }
                        fs.Close();
                        
                    }
                }
                return true;
            }}
      

  6.   

    可以把Excel得数据保存一个xml中,然后不管是winform或者是html都以这个xml为数据源。//创建一个xml,大概如下:
    <Root><Key><name></name><value></value></Key></Root>//循环Excel每行,每列,//创建xml节点,并保存到xml中去//以xml为数据源,在html或winform中显示数据。