zoukankan      html  css  js  c++  java
  • C#操作Excel(导入导出)

    1. class ExampleOfLegalsoft   
    2. {   
    3.     /// <summary>    
    4.     /// 读取Excel文档    
    5.     /// </summary>    
    6.     /// <param name="Path">文件名称</param>    
    7.     /// <returns>返回一个数据集</returns>    
    8.     public DataSet ExcelToDS(string Path)   
    9.     {   
    10.         string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";   
    11.         OleDbConnection conn = new OleDbConnection(strConn);   
    12.         conn.Open();   
    13.         string strExcel = "";   
    14.         OleDbDataAdapter myCommand = null;   
    15.         DataSet ds = null;   
    16.         strExcel = "select * from [sheet1$]";   
    17.         myCommand = new OleDbDataAdapter(strExcel, strConn);   
    18.         ds = new DataSet();   
    19.         myCommand.Fill(ds, "table1");   
    20.         return ds;   
    21.     }   
    22.     /// <summary>    
    23.     /// 写入Excel文档    
    24.     /// </summary>    
    25.     /// <param name="Path">文件名称</param>    
    26.     public bool SaveFP2toExcel(string Path)   
    27.     {   
    28.         try  
    29.         {   
    30.             string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";   
    31.             OleDbConnection conn = new OleDbConnection(strConn);   
    32.             conn.Open();   
    33.             System.Data.OleDb.OleDbCommand cmd = new OleDbCommand();   
    34.             cmd.Connection = conn;   
    35.             //cmd.CommandText ="UPDATE [sheet1$] SET 姓名="2005-01-01" WHERE 工号="日期"";    
    36.             //cmd.ExecuteNonQuery ();    
    37.             for (int i = 0; i < fp2.Sheets[0].RowCount - 1; i++)   
    38.             {   
    39.                 if (fp2.Sheets[0].Cells[i, 0].Text != "")   
    40.                 {   
    41.                     cmd.CommandText = "INSERT INTO [sheet1$] (工号,姓名,部门,职务,日期,时间) VALUES(" + fp2.Sheets[0].Cells[i, 0].Text + "," +   
    42.                     fp2.Sheets[0].Cells[i, 1].Text + "," + fp2.Sheets[0].Cells[i, 2].Text + "," + fp2.Sheets[0].Cells[i, 3].Text +   
    43.                     "," + fp2.Sheets[0].Cells[i, 4].Text + "," + fp2.Sheets[0].Cells[i, 5].Text + ")";   
    44.                     cmd.ExecuteNonQuery();   
    45.                 }   
    46.             }   
    47.             conn.Close();   
    48.             return true;   
    49.         }   
    50.         catch (System.Data.OleDb.OleDbException ex)   
    51.         {   
    52.             System.Diagnostics.Debug.WriteLine("写入Excel发生错误:" + ex.Message);   
    53.         }   
    54.         return false;   
    55.     }   
  • 相关阅读:
    Linux系统Tomcat进程使用shutdown无法关闭进程
    统一延长复工,带薪休假,几十万中小微企业面临的困境
    由数据迁移至MongoDB导致的数据不一致问题及解决方案
    整理了一周的Python资料,包含各阶段所需网站、项目,收藏了慢慢来
    2019,我是如何转向战略成功,并成功跑通一个现金流项目的
    500行代码,教你用python写个微信飞机大战
    会用python把linux命令写一遍的人,进大厂有多容易?
    Python 开发植物大战僵尸游戏
    分享一个 pycharm 专业版的永久使用方法
    mongodb 启动 WARNING: soft rlimits too low, transparent_hugepage/enabled is 'always'. never
  • 原文地址:https://www.cnblogs.com/bluedy1229/p/1431576.html
Copyright © 2011-2022 走看看