国产探花免费观看_亚洲丰满少妇自慰呻吟_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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴林右旗| 肥城市| 玉林市| 宁都县| 乐平市| 阿拉善左旗| 宜君县| 大化| 望谟县| 交口县| 宁德市| 雷山县| 镇安县| 兴安县| 阿拉善盟| 富阳市| 梁山县| 乌审旗| 汉阴县| 洛扎县| 铜山县| 黔南| 郑州市| 景谷| 诸暨市| 湖南省| 海淀区| 福建省| 正蓝旗| 贵溪市| 淮滨县| 萍乡市| 长岛县| 庐江县| 鹤壁市| 邮箱| 乾安县| 天长市| 天长市| 宾川县| 肥东县|