国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#操作Excel(導入導出)

2024-07-21 02:17:54
字體:
來源:轉載
供稿:網友
 

前些日子,有很多朋友說需要c#導出到excel的代碼,現共享給大家

/// <summary>
  /// 讀取excel文檔
  /// </summary>
  /// <param name="path">文件名稱</param>
  /// <returns>返回一個數據集</returns>
  public dataset exceltods(string path)
  {
   string strconn = "provider=microsoft.jet.oledb.4.0;" +"data source="+ path +";"+"extended properties=excel 8.0;";
   oledbconnection conn = new oledbconnection(strconn);
   conn.open(); 
   string strexcel = "";  
   oledbdataadapter mycommand = null;
   dataset ds = null;
   strexcel="select * from [sheet1$]";
   mycommand = new oledbdataadapter(strexcel, strconn);
   ds = new dataset();
   mycommand.fill(ds,"table1");  
   return ds;
  }

/// <summary>
  /// 寫入excel文檔
  /// </summary>
  /// <param name="path">文件名稱</param>
  public bool savefp2toexcel(string path)
  {
   try
   {
    string strconn = "provider=microsoft.jet.oledb.4.0;" +"data source="+ path +";"+"extended properties=excel 8.0;";
    oledbconnection conn = new oledbconnection(strconn);
    conn.open(); 
    system.data.oledb.oledbcommand cmd=new oledbcommand ();
    cmd.connection =conn;
    //cmd.commandtext ="update [sheet1$] set 姓名='2005-01-01' where 工號='日期'";
    //cmd.executenonquery ();
    for(int i=0;i<fp2.sheets [0].rowcount -1;i++)
    {
     if(fp2.sheets [0].cells[i,0].text!="")
     {
      cmd.commandtext ="insert into [sheet1$] (工號,姓名,部門,職務,日期,時間) values('"+fp2.sheets [0].cells[i,0].text+ "','"+
       fp2.sheets [0].cells[i,1].text+"','"+fp2.sheets [0].cells[i,2].text+"','"+fp2.sheets [0].cells[i,3].text+
       "','"+fp2.sheets [0].cells[i,4].text+"','"+fp2.sheets [0].cells[i,5].text+"')";
      cmd.executenonquery ();
     }
    }
    conn.close ();
    return true;
   }
   catch(system.data.oledb.oledbexception ex)
   {
    system.diagnostics.debug.writeline ("寫入excel發生錯誤:"+ex.message );
   }
   return false;
  }

這種方法目前最有效,如果有不明白的地方可以來信交流

a

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎兰屯市| 通江县| 仲巴县| 邳州市| 会理县| 灵武市| 嘉鱼县| 鹤峰县| 建瓯市| 蒙山县| 信宜市| 贡觉县| 嘉峪关市| 睢宁县| 双江| 望谟县| 老河口市| 曲沃县| 孝义市| 资兴市| 东阳市| 岚皋县| 嵊州市| 彭州市| 博客| 扬州市| 逊克县| 蓬莱市| 资阳市| 安龙县| 平山县| 南漳县| 鄯善县| 台中市| 个旧市| 博爱县| 楚雄市| 阜康市| 永定县| 赣州市| 商丘市|