• C#操作EXCLE表


    在做一个小项目,需要把Excel数据导出,找了一些资料,自己也总结出了一点方法,与大家共享。
    一、首先简要描述一下如何操作Excel表
    先要添加对Excel的引用。选择项目-〉添加引用-〉COM-〉添加Microsoft Excel 11.0。(不同的office讲会有不同版本的dll文件)。
    using System.Reflection;
    using Excel = Microsoft.Office.Interop.Excel;

    string  UserName =System.Environment.UserName.ToString();//得到当前操作系统的用户名称

    //产生一个Excel.Application的新进程
    Excel.ApplicationClass OldApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
    //打开目标表,filename为目标表路径
    OldApp.Application.Workbooks.Open(filename,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Valu

    e,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
    //产生新的workbook
    Excel.Workbook mybook = OldApp.Workbooks[1];
    mysheet = (Excel.Worksheet)mybook.Worksheets[1];
    //得到A9单元格的值
    Excel.Range r = mysheet.get_Range(mysheet.Cells[9,1],mysheet.Cells[9,1]);
    Str = r.Text.ToString().Trim();

    //产生一个Excel.Application的新进程 ,把上表的数据导入的新的EXCEL中,savefilename为新EXCEL表路径
    Excel.ApplicationClass NewApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
    NewApp.Application.Workbooks.Open(savefilename,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);

    NewApp.Cells[2,9] = OldApp.Cells[2,1];
    NewApp.Cells[2,14] = OldApp.Cells[2,3];
    NewApp.Cells[2,21] = mysheet.get_Range(mysheet.Cells[2,13],mysheet.Cells[2,13]).Text.ToString();

    int j;
    int i;
     for(j=13;j<=((Excel.Worksheet)OldApp.Worksheets.get_Item(1)).UsedRange.Rows.Count;j++)
     {
      for(i = 1;i<=4;i++)
      {
       NewApp.Cells[j-7,i] = OldApp.Cells[j,i];
      }
      for(i=7;i<=19;i++)
      {
       NewApp.Cells[j-7,i-2]= OldApp.Cells[j,i];
      }
     }

    File.Delete(@"c:\Documents and Settings\"+UserName+@"\My Documents\Resume.xlw"); //删除Excel产生的文件
    NewApp.Visible = true;
    OldApp.Quit();

    二、套用模板
    //将模板文件复制到一个新文件中
    private bool CopyMode(string savefilename) //savefilename为需要套用模板的表路径
    {
     if(!File.Exists(ModePath)) //ModePath为模板路径
      {
       MessageBox.Show("模板不存在!");
       return false;
      }
     if(File.Exists(savefilename))
      {
       File.Delete(savefilename);
      }
     File.Copy(ModePath,savefilename);
     return true;
    }
    三、其他
    因为项目上需要导入几个表数据,所以需要对表进行区别,通过比较EXCEL表头的数据来判断是哪个表
    //添加引用
    using System.Text.RegularExpressions;

    #region 利用正则表达式与表头匹配
    private bool Regular(string str,string oldstr) //str为需要在oldstr匹配的字段
    {
     Regex reg = new Regex(str);
     Match m = reg.Match(oldstr);
     if(m.Success)
     {
      return true;
     }
     else
      return false;

    }
    #endregion

  • 相关阅读:
    matplotlib添加坐标轴实现性格测试可视化
    一段代码实现Aplayer+网易云音乐接口
    cnblogs在手机端显示的一些坑
    【转】 最全的MySQL基础【燕十八传世】
    【转】 MySQL基础知识
    【转】 SSM整合
    【转】 Mybatis学习笔记-狂神版
    【转】 SpringMVC学习笔记
    【转】 mybatis 详解(十一)------ mybatis和spring整合
    【转】 mybatis 详解(十)------ 逆向工程
  • 原文地址:https://www.cnblogs.com/nonsuch/p/473283.html
Copyright © 2020-2023  润新知