using System;
using System.Data;
using System.Configuration;
using System.Web;
using Microsoft.Office.Interop;
using Microsoft.Office.Core;namespace Microsoft.Office.Interop.ExcelEdit
{/// <SUMMARY>/// Microsoft.Office.Interop.ExcelEdit 的摘要说明/// </SUMMARY>public class ExcelEdit{public string mFilename;public Microsoft.Office.Interop.Excel.Application app;public Microsoft.Office.Interop.Excel.Workbooks wbs;public Microsoft.Office.Interop.Excel.Workbook wb;public Microsoft.Office.Interop.Excel.Worksheets wss;public Microsoft.Office.Interop.Excel.Worksheet ws;public ExcelEdit(){//// TODO: 在此处添加构造函数逻辑//}public void Create()//创建一个Microsoft.Office.Interop.Excel对象{app = new Microsoft.Office.Interop.Excel.Application();wbs = app.Workbooks;wb = wbs.Add(true);}public void Open(string FileName)//打开一个Microsoft.Office.Interop.Excel文件{app = new Microsoft.Office.Interop.Excel.Application();wbs = app.Workbooks;wb = wbs.Add(FileName);//wb = wbs.Open(FileName, 0, true, 5,"", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "t", false, false, 0, true,Type.Missing,Type.Missing);//wb = wbs.Open(FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);mFilename = FileName;}public Microsoft.Office.Interop.Excel.Worksheet GetSheet(string SheetName)//获取一个工作表{Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[SheetName];return s;}public Microsoft.Office.Interop.Excel.Worksheet AddSheet(string SheetName)//添加一个工作表{Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);s.Name = SheetName;return s;}public void DelSheet(string SheetName)//删除一个工作表{((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[SheetName]).Delete();}public Microsoft.Office.Interop.Excel.Worksheet ReNameSheet(string OldSheetName, string NewSheetName)//重命名一个工作表一{Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[OldSheetName];s.Name = NewSheetName;return s;}public Microsoft.Office.Interop.Excel.Worksheet ReNameSheet(Microsoft.Office.Interop.Excel.Worksheet Sheet, string NewSheetName)//重命名一个工作表二{Sheet.Name = NewSheetName;return Sheet;}public void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet ws, int x, int y, object value)//ws:要设值的工作表     X行Y列     value   值{ws.Cells[x, y] = value;}public void SetCellValue(string ws, int x, int y, object value)//ws:要设值的工作表的名称 X行Y列 value 值{GetSheet(ws).Cells[x, y] = value;}public void SetCellProperty(Microsoft.Office.Interop.Excel.Worksheet ws, int Startx, int Starty, int Endx, int Endy, int size, string name, Microsoft.Office.Interop.Excel.Constants color, Microsoft.Office.Interop.Excel.Constants HorizontalAlignment)//设置一个单元格的属性   字体,   大小,颜色   ,对齐方式{name = "宋体";size = 12;color = Microsoft.Office.Interop.Excel.Constants.xlAutomatic;HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlRight;ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;}public void SetCellProperty(string wsn, int Startx, int Starty, int Endx, int Endy, int size, string name, Microsoft.Office.Interop.Excel.Constants color, Microsoft.Office.Interop.Excel.Constants HorizontalAlignment){//name = "宋体";//size = 12;//color = Microsoft.Office.Interop.Excel.Constants.xlAutomatic;//HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlRight;Microsoft.Office.Interop.Excel.Worksheet ws = GetSheet(wsn);ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;}public void UniteCells(Microsoft.Office.Interop.Excel.Worksheet ws, int x1, int y1, int x2, int y2)//合并单元格{ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Merge(Type.Missing);}public void UniteCells(string ws, int x1, int y1, int x2, int y2)//合并单元格{GetSheet(ws).get_Range(GetSheet(ws).Cells[x1, y1], GetSheet(ws).Cells[x2, y2]).Merge(Type.Missing);}public void InsertTable(System.Data.DataTable dt, string ws, int startX, int startY)
//将内存中数据表格插入到Microsoft.Office.Interop.Excel指定工作表的指定位置 为在使用模板时控制格式时使用一{for (int i = 0; i  <= dt.Rows.Count - 1; i++){for (int j = 0; j  <= dt.Columns.Count - 1; j++){GetSheet(ws).Cells[startX+i, j + startY] = dt.Rows[i][j].ToString();}}}public void InsertTable(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet ws, int startX, int startY)
//将内存中数据表格插入到Microsoft.Office.Interop.Excel指定工作表的指定位置二{for (int i = 0; i  <= dt.Rows.Count - 1; i++){for (int j = 0; j  <= dt.Columns.Count - 1; j++){ws.Cells[startX+i, j + startY] = dt.Rows[i][j];}}}public void AddTable(System.Data.DataTable dt, string ws, int startX, int startY)
//将内存中数据表格添加到Microsoft.Office.Interop.Excel指定工作表的指定位置一{for (int i = 0; i  <= dt.Rows.Count - 1; i++){for (int j = 0; j  <= dt.Columns.Count - 1; j++){GetSheet(ws).Cells[i + startX, j + startY] = dt.Rows[i][j];}}}public void AddTable(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet ws, int startX, int startY)
//将内存中数据表格添加到Microsoft.Office.Interop.Excel指定工作表的指定位置二{for (int i = 0; i  <= dt.Rows.Count - 1; i++){for (int j = 0; j  <= dt.Columns.Count - 1; j++){ws.Cells[i + startX, j + startY] = dt.Rows[i][j];}}}public void InsertPictures(string Filename, string ws)//插入图片操作一{GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);//后面的数字表示位置}//public void InsertPictures(string Filename, string ws, int Height, int Width)//插入图片操作二//{//    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);//    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;//    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;//}//public void InsertPictures(string Filename, string ws, int left, int top, int Height, int Width)//插入图片操作三//{//    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);//    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementLeft(left);//    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementTop(top);//    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;//    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;//}public void InsertActiveChart(Microsoft.Office.Interop.Excel.XlChartType ChartType, string ws, int DataSourcesX1, int DataSourcesY1, int DataSourcesX2, int DataSourcesY2, Microsoft.Office.Interop.Excel.XlRowCol ChartDataType)//插入图表操作{ChartDataType = Microsoft.Office.Interop.Excel.XlRowCol.xlColumns;wb.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);{wb.ActiveChart.ChartType = ChartType;wb.ActiveChart.SetSourceData(GetSheet(ws).get_Range(GetSheet(ws).Cells[DataSourcesX1, DataSourcesY1], GetSheet(ws).Cells[DataSourcesX2, DataSourcesY2]), ChartDataType);wb.ActiveChart.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAsObject, ws);}}public bool Save()//保存文档{if (mFilename == ""){return false;}else{try{wb.Save();return true;}catch (Exception ex){return false;}}}public bool SaveAs(object FileName)//文档另存为{try{wb.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);return true;}catch (Exception ex){return false;}}public void Close()//关闭一个Microsoft.Office.Interop.Excel对象,销毁对象{//wb.Save();wb.Close(Type.Missing, Type.Missing, Type.Missing);wbs.Close();app.Quit();wb = null;wbs = null;app = null;GC.Collect();}}
}

一个C#操作Excel类,功能比较全相关推荐

  1. 【博主推荐】Python 基于Xlwings、Openpyxl自己重新封装Python操作Excel类

    1.简介:Python操作Excel,常用Xlwings.Openpyxl类,由于其知识琐碎,使用起来不太方便,因此自己把常用用法重新封装一个操作类. 2.应用场景:使用Python操作Excel,读 ...

  2. PHP操作excel类 PHPExcel

    PHP操作excel类 PHPExcel http://www.cr173.com/soft/40741.html   我的微云:http://share.weiyun.com/2db79f1438f ...

  3. php excel 设置常规_php实现的操作excel类详解

    本文实例讲述了php实现的操作excel类.分享给大家供大家参考,具体如下: class Excel { static $instance=null; private $excel=null; pri ...

  4. QT 操作excel 类封装(转载)

    QT 操作excel 类封装(转载) 原链接:http://blog.csdn.net/liliming1234/article/details/7054941 pro file [plain]  v ...

  5. 盘点一个Pandas操作Excel多条件取值的实战案例

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 长乐钟声花外尽,龙池柳色雨中深. ...

  6. c#操作Excel类

    这是其中的一种方式,使用微软的Microsoft.Office.Interop.Excel库. 自己写了一个操作类: using System; using System.Collections.Ge ...

  7. python pandas csv时间聚合_Python通过pandas操作excel常用功能

    1.导入数据源 #导入相关库 import pandas as pd import numpy as np import os from pandas import DataFrame,Series ...

  8. Qt操作Excel类

    基于某文章(原文找不到了), 进行小修改调整 .h #ifndef QEXCEL_H #define QEXCEL_H #include <QString> #include <QV ...

  9. java操作pdf--Spire,功能较全,免费版只能操作10页

    1.添加依赖 需要下载,打入maven仓库,提取码:u9gf https://pan.baidu.com/s/1Sv2esqNF2gQO1Tg10TWTSw 打入maven仓库 mvn install ...

最新文章

  1. Cocos2d-x3.0 不规则Button
  2. 对比3家平台,我总结了疫情数据可视化的8点经验
  3. java中使用jxl导出Excel表格详细通用步骤
  4. 07.30《jQuery》——1.1DOM对和jQuery对象的转化
  5. python //运算符
  6. LintCode 生成括号
  7. JAVA读、写EXCEL文件
  8. 安装linux没有raid驱动程序,LINUX 无法在 RAID 上安装的问题
  9. Fiddler图标解释
  10. Git管理工具SourceTree文件预览乱码问题
  11. 解决wps在windows上弹窗等的流氓行为
  12. (一)目标检测经典模型回顾
  13. 软件测试周刊(第82期):其实所有纠结做选择的人心里早就有了答案,咨询只是想得到内心所倾向的选择。
  14. 第四十七题 UVA437 巴比伦塔 The Tower of Babylon
  15. 解除IIS文件下载限制
  16. 长期不上班,人会废掉吗?
  17. STM32cubIDE 黑色主题_儿童画创意第二弹 | 绘画的秘密+万圣节主题画,10个创意教程一次看完!...
  18. ChunJun Meetup演讲分享 | 基于袋鼠云开源框架的数仓一体化建设探索
  19. DVI接口关于技术性的知识导论
  20. “壮士断腕”无人驾驶能够拯救苹果的内忧外困吗?

热门文章

  1. 打AI比赛的模板整理
  2. 有关国内地图坐标系之间相互转换
  3. 微信小程序登录页动画-云层漂浮
  4. 国有企业不能有信息化
  5. 解决AE在win10/11系统上安装失败问题 AEcc2021-After Effects 2021中文正式版安装教程
  6. 推荐Linux性能分析的一篇论文和两本书
  7. 5.MySQL行锁、表锁、间隙锁详解
  8. 自定义体温折线图html,科学老师布置体温折线图作业,家长打算交“备孕表”代替...
  9. Axure RP教程_编程入门自学教程_菜鸟教程-免费教程分享
  10. 男士健身减肚腩最有效的方法