一个C#操作Excel类,功能比较全
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 = ;
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 = ; i <= dt.Rows.Count - ; i++)
{
for (int j = ; j <= dt.Columns.Count - ; 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 = ; i <= dt.Rows.Count - ; i++)
{
for (int j = ; j <= dt.Columns.Count - ; 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 = ; i <= dt.Rows.Count - ; i++)
{
for (int j = ; j <= dt.Columns.Count - ; 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 = ; i <= dt.Rows.Count - ; i++)
{
for (int j = ; j <= dt.Columns.Count - ; 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, , , , );
//后面的数字表示位置
} //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类,功能比较全的更多相关文章
- 一个poi操作实现导出功能的类
public class ExportExcel<T> { public void exportExcel(Collection<T> dataset, OutputStrea ...
- NPOI操作EXCEL 类代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.SS. ...
- java分享第十七天-02(封装操作excel类)
java解析EXCEL用的是POI的JAR包,兼容EXCEL2003及2007+版本的EXCEL所需要的JAR包:poi-3.8.jarpoi-ooxml.jarpoi-ooxml-schemas. ...
- 通过NPOI操作Excel
最近在做的一个项目中需要生成Excel,通过学习使用NPOI实现了相关需求,写了一个简便操作的类,记录如下: public class NPOIHelperForExcel { #region exc ...
- Java 操作 EXCEL
今天帮朋友写了一段用来处理EXCEL内容的程序,在这里记录下自己的学习过程.主要是对EXCEL表格中的内容做分类和统计,使用计算机来做这种重复的机械性地工作再好不过了.首先,我们需要下载一个java操 ...
- NPOI 操作EXCEL 小计
由于需要做一个生成下载Excel的功能,查了一下 常用的操作有 NPOI Spire DOCX,于是便下载了NPOI试了一下,发现确实好用,但是还是有几个比较坑的地方 1.不能直接删除列 虽然提供了 ...
- 一个操作EXCEL的C#类ExcelUtils
近期在公司里一直从事服务类的工作,涉及到非常多excel的处理.部分工作内容是每天反复的,仅仅是每天的数据不同而已.我遇到的一个问题是客户每天发送的几种数据有些excel中的字段顺序是一致的,有些是不 ...
- 一个由正则表达式引发的血案 vs2017使用rdlc实现批量打印 vs2017使用rdlc [asp.net core 源码分析] 01 - Session SignalR sql for xml path用法 MemCahe C# 操作Excel图形——绘制、读取、隐藏、删除图形 IOC,DIP,DI,IoC容器
1. 血案由来 近期我在为Lazada卖家中心做一个自助注册的项目,其中的shop name校验规则较为复杂,要求:1. 英文字母大小写2. 数字3. 越南文4. 一些特殊字符,如“&”,“- ...
- 个人永久性免费-Excel催化剂功能第86波-人工智能之图像OCR文本识别全覆盖
在上一年中,Excel催化剂已经送上一波人工智能系列功能,鉴于部分高端用户的需求,再次给予实现了复杂的图像OCR识别,包含几乎所有日常场景,让公司个人手头的图像非结构化数据瞬间变为可进行结构化处理分析 ...
随机推荐
- 【Linux】常见Linux默认的shell
常见的操作系统下的shell: Linux下默认的shell是Bourne Again shell(bash) Solaris和FreeBSD下默认的是Bourne shell(sh) AIX系统下默 ...
- .net通过url访问服务器获取服务器返回数据
一.url为http协议 1.普通调用: public string GetInfo(string url) { //访问http方法 string strBuff = ""; U ...
- RHEL7 DNS 服务 unbound 测试
测试环境: 物理机win10系统,虚拟机软件使用Oracle VirtualBox. rhel1.rusky.com 192.168.100.1 RHEL7(辅DNS) rhel2.rusky.com ...
- IP首部格式[转载]
TCP 传输首部是 IP首部,所以把IP首部格式 拿过来研究下,看IP首部解码过程: 来源:51CTO博客,地址:http://lihuan.blog.51cto.com/4391550/7999 ...
- 构建高性能数据库缓存之redis(二)
一.概述 在构建高性能数据库缓存之redis(一)这篇文档中,阐述了Redis数据库(key/value)的特点.功能以及简单的配置过程,相信阅读过这篇文档的朋友,对Redis数据库会有一点的了解,此 ...
- Linux中的lo回环接口详细介绍
1.linux的网络接口之扫盲 (1)网络接口的命名 这里并不存在一定的命名规范,但网络接口名字的定义一般都是要有意义的.例如: eth0: ethernet的简写,一般用于以太网接口. wifi0: ...
- (转)Groupon前传:从10个月的失败作品修改,1个月找到成功 并不挶泥在这个点子上面,它反而往后站一步,看看他们已经做好的这个网站,可以再怎么包装成另一个完完全全不同的网站?所有的人所做的每件失败的事情中, 一定有碰到或含有成功的答案」在里面,只是他们不知道而已。 人不怕失败」,只怕宣布失败」
(转)Groupon前传:从10个月的失败作品修改,1个月找到成功 今天读到 一个非常励志人心的故事 ,就像现在「叶问」有「前传」,最近很火红的团集购网站Groupon 也出现了「Groupon前传」 ...
- 【Algorithm】快速排序(续)
前面在常用的排序算法中,已经写过一篇关于快速排序算法的博客,但是最近看到<The C Programming Language>这本书中的快速排序算法写的不错,所以就拿过来分享一下,下面我 ...
- Linux日期时间显示输出
1.输出当前年月日 echo $(date +%F) 2014-02-21 2.输出当前时间(时分) echo $(date +%R) 12:45 3.输出当前时间(时分秒) echo $(date ...
- stm32开发 - 远离 Keil uVision, 回到 Visual Studio
学了8051单片机, 学了MSP430系列, 终于开始步入正轨, 开始学习 stm32(ARM Cortex-M3)系列微处理器~ 学51用Keil uVision开发环境, 提一下Keil uVis ...