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();}}
}

https://www.cnblogs.com/softwyy/p/8929317.html

C# 处理excel 大全相关推荐

  1. 计算机办公类常用excel,[计算机软件及应用]办公常用EXCEL大全.ppt

    [计算机软件及应用]办公常用EXCEL大全 二.改变图表的文字.颜色和图案 图表的编辑 若要对标题进行编辑,可将鼠标移至标题的左下角处双击,即会出现右图所示对话框,用该对话框可以对标题的字体.字体的背 ...

  2. C# 操作Excel大全(转载)

    1 /引用Microsoft.Office.Interop.Excel.dll文件 2 //添加using 3 using Microsoft.Office.Interop.Excel; 4 usin ...

  3. Delphi操作Excel大全

    分享一下我老师大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow 转自  上帝的鱼- ...

  4. 【Python】Pandas读存Excel大全

    公众号:尤而小屋 作者:Peter 编辑:Peter 本文记录的是如何通过Pandas来读取Excel文件,以及如何将DataFrame保存到Excel文件中. 官网参数详解:https://pand ...

  5. C# 操作Excel大全

    //引用Microsoft.Office.Interop.Excel.dll文件  //添加using using Microsoft.Office.Interop.Excel; using Exce ...

  6. Pandas读存Excel大全

    公众号:尤而小屋 作者:Peter 编辑:Peter 本文记录的是如何通过Pandas来读取Excel文件,以及如何将DataFrame保存到Excel文件中. 官网参数详解:https://pand ...

  7. 计算机表格 求差,excel表格怎么求差多个

    Q5:excel表格透视表怎么做? 大家好,我是Excel大全,头条原创作者,每日分享实用Excel小技巧. 刚好看见这个问题,我就来分享下我的经验,希望能帮到你! 我将从以下方面回答这个问题:数据透 ...

  8. c语言 为参数设置默认值,js函数参数设置默认值

    前端学HTTP之网关.隧道和中继 前面的话 Web是一种强大的内容发布工具.人们已经从只在网上发送静态的在线文档,发展到共享更复杂的资源,比如数据库内容或动态生成的HTML页面.Web浏览器为用户提供 ...

  9. excel函数大全_让你的EXCEL工作效率翻倍的函数大全

    详细的函数说明和应用实例可查看我上传的<EXCEL快速学习教程视频>对应各类函数教程 常用函数大全 使用函数:公式选项卡>函数库>插入函数.常用函数.最近使用函数.财务.逻辑. ...

最新文章

  1. [Nuxt.js]Nuxt项目启动如何跳过“Are you interested in participation?”
  2. ArduinoYun教程之配置Arduino Yun环境
  3. 数学学渣的福利,看看图就能学会的机器学习
  4. CMDB服务器管理系统【s5day90】:创建资产更新服务器硬盘信息
  5. perl学习之:编译、执行与内存关系(转)
  6. [转载] 杜拉拉升职记——53 自由自在地活
  7. LeetCode Encode and Decode TinyURL
  8. android广播的使用
  9. Shell awk 求标准差
  10. show,hide与fadeIn、fadeOu的区别
  11. pyhton基础中的要点一
  12. 二进制、十进制、八进制、十六进制 各代表的英文字母是什么
  13. 威纶触摸屏485通信控制多台台达变频器程序
  14. PDF页码怎么设置?如何给PDF文件设置页码
  15. 清华山维EPS二次开发基础篇
  16. HTML+CSS打造简单的横向时间轴
  17. c语言编译笑脸,C语言快速入门——笑脸绘图程序:窗口实现
  18. Mac字体怎么安装?如何在macOS电脑导入字体文件?
  19. 谷雨主题的微信公众号图文排版有哪些技巧?
  20. 面向消费者的产品(To C 类)、面向企业的产品(To B 类)这2类产品在品牌营销方式上的区别?

热门文章

  1. android studio 便携式wlan热点 网络名称_速存 | WLAN信号增强器
  2. python编程入门第一课教案_python编程从入门到实践 第一课:输入输出
  3. 步进电机可以连续运转吗?
  4. QEventLoop进行函数运行进度控制
  5. RUP和IPD流程的优缺点
  6. java151和152_编写高质量代码:改善Java的151个建议(性能和效率)132-133
  7. Java必备技能:IDEA一定要懂的32条快捷键
  8. Shiro【授权过滤器、与ehcache整合、验证码、记住我】
  9. mysql主从之slave-skip-errors和sql_slave_skip_counter
  10. C语言内存对齐详解(1)