Excel操作类
'引入Excel的COM组件
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Core
Namespace ExcelEdit
''' <summary>
''' ExcelEdit 的摘要说明
''' </summary>
Public Class ExcelEdit
Public mFilename As String
Public app As Excel.Application
Public wbs As Excel.Workbooks
Public wb As Excel.Workbook
Public wss As Excel.Worksheets
Public ws As Excel.Worksheet
'
' TODO: 在此处添加构造函数逻辑
'
Public Sub New()
End Sub
Public Sub Create()
'创建一个Excel对象
app = New Excel.Application()
wbs = app.Workbooks
wb = wbs.Add(True)
End Sub
Public Sub Open(ByVal FileName As String)
'打开一个Excel文件
app = New Excel.Application()
wbs = app.Workbooks
wb = wbs.Add(FileName)
'wb = wbs.Open(FileName, 0, true, 5,"", "", true,
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,Excel.XlPlatform.xlWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
mFilename = FileName
End Sub
Public Function GetSheet(ByVal SheetName As String) As Excel.Worksheet
'获取一个工作表
Dim s As Excel.Worksheet = DirectCast(wb.Worksheets(SheetName), Excel.Worksheet)
Return s
End Function
Public Function AddSheet(ByVal SheetName As String) As Excel.Worksheet
'添加一个工作表
Dim s As Excel.Worksheet =
DirectCast(wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing,
Type.Missing), Excel.Worksheet)
s.Name = SheetName
Return s
End Function
Public Sub DelSheet(ByVal SheetName As String)
'删除一个工作表
DirectCast(wb.Worksheets(SheetName), Excel.Worksheet).Delete()
End Sub
Public Function ReNameSheet(ByVal OldSheetName As String, ByVal NewSheetName As String) As Excel.Worksheet
'重命名一个工作表一
Dim s As Excel.Worksheet = DirectCast(wb.Worksheets(OldSheetName), Excel.Worksheet)
s.Name = NewSheetName
Return s
End Function
Public Function ReNameSheet(ByVal Sheet As Excel.Worksheet, ByVal NewSheetName As String) As Excel.Worksheet
'重命名一个工作表二
Sheet.Name = NewSheetName
Return Sheet
End Function
Public Sub SetCellValue(ByVal ws As Excel.Worksheet, ByVal x As Integer, ByVal y As Integer, ByVal value As Object)
'ws:要设值的工作表 X行Y列 value 值
ws.Cells(x, y) = value
End Sub
Public Sub SetCellValue(ByVal ws As String, ByVal x As Integer, ByVal y As Integer, ByVal value As Object)
'ws:要设值的工作表的名称 X行Y列 value 值
GetSheet(ws).Cells(x, y) = value
End Sub
Public Sub SetCellProperty(ByVal ws As Excel.Worksheet, ByVal
Startx As Integer, ByVal Starty As Integer, ByVal Endx As Integer, ByVal
Endy As Integer, ByVal size As Integer, _
ByVal name As String, ByVal color As Excel.Constants, ByVal HorizontalAlignment As Excel.Constants)
'设置一个单元格的属性 字体, 大小,颜色 ,对齐方式
name = "宋体"
size = 12
color = Excel.Constants.xlAutomatic
HorizontalAlignment = 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
End Sub
Public Sub SetCellProperty(ByVal wsn As String, ByVal Startx As
Integer, ByVal Starty As Integer, ByVal Endx As Integer, ByVal Endy As
Integer, ByVal size As Integer, _
ByVal name As String, ByVal color As Excel.Constants, ByVal HorizontalAlignment As Excel.Constants)
'name = "宋体";
'size = 12;
'color = Excel.Constants.xlAutomatic;
'HorizontalAlignment = Excel.Constants.xlRight;
Dim ws As Excel.Worksheet = 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
End Sub
Public Sub UniteCells(ByVal ws As Excel.Worksheet, ByVal x1 As
Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer)
'合并单元格
ws.get_Range(ws.Cells(x1, y1), ws.Cells(x2, y2)).Merge(Type.Missing)
End Sub
Public Sub UniteCells(ByVal ws As String, ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer)
'合并单元格
GetSheet(ws).get_Range(GetSheet(ws).Cells(x1, y1), GetSheet(ws).Cells(x2, y2)).Merge(Type.Missing)
End Sub
Public Sub InsertTable(ByVal dt As System.Data.DataTable, ByVal ws As
String, ByVal startX As Integer, ByVal startY As Integer)
'将内存中数据表格插入到Excel指定工作表的指定位置 为在使用模板时控制格式时使用一
For i As Integer = 0 To dt.Rows.Count - 1
For j As Integer = 0 To dt.Columns.Count - 1
GetSheet(ws).Cells(startX + i, j + startY) = dt.Rows(i)(j).ToString()
Next
Next
End Sub
Public Sub InsertTable(ByVal dt As System.Data.DataTable, ByVal ws As
Excel.Worksheet, ByVal startX As Integer, ByVal startY As Integer)
'将内存中数据表格插入到Excel指定工作表的指定位置二
For i As Integer = 0 To dt.Rows.Count - 1
For j As Integer = 0 To dt.Columns.Count - 1
ws.Cells(startX + i, j + startY) = dt.Rows(i)(j)
Next
Next
End Sub
Public Sub AddTable(ByVal dt As System.Data.DataTable, ByVal ws As String, ByVal startX As Integer, ByVal startY As Integer)
'将内存中数据表格添加到Excel指定工作表的指定位置一
For i As Integer = 0 To dt.Rows.Count - 1
For j As Integer = 0 To dt.Columns.Count - 1
GetSheet(ws).Cells(i + startX, j + startY) = dt.Rows(i)(j)
Next
Next
End Sub
Public Sub AddTable(ByVal dt As System.Data.DataTable, ByVal ws As
Excel.Worksheet, ByVal startX As Integer, ByVal startY As Integer)
'将内存中数据表格添加到Excel指定工作表的指定位置二
For i As Integer = 0 To dt.Rows.Count - 1
For j As Integer = 0 To dt.Columns.Count - 1
ws.Cells(i + startX, j + startY) = dt.Rows(i)(j)
Next
Next
End Sub
Public Sub InsertPictures(ByVal Filename As String, ByVal ws As String)
'插入图片操作一
GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, _
150)
'后面的数字表示位置
End Sub
'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 Sub InsertActiveChart(ByVal ChartType As Excel.XlChartType, ByVal
ws As String, ByVal DataSourcesX1 As Integer, ByVal DataSourcesY1 As
Integer, ByVal DataSourcesX2 As Integer, ByVal DataSourcesY2 As Integer,
_
ByVal ChartDataType As Excel.XlRowCol)
'插入图表操作
ChartDataType = 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(Excel.XlChartLocation.xlLocationAsObject, ws)
End Sub
Public Function Save() As Boolean
'保存文档
If mFilename = "" Then
Return False
Else
Try
wb.Save()
Return True
Catch ex As Exception
Return False
End Try
End If
End Function
Public Function SaveAs(ByVal FileName As Object) As Boolean
'文档另存为
Try
wb.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _
Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Sub Close()
'关闭一个Excel对象,销毁对象
'wb.Save();
wb.Close(Type.Missing, Type.Missing, Type.Missing)
wbs.Close()
app.Quit()
wb = Nothing
wbs = Nothing
app = Nothing
GC.Collect()
End Sub
End Class
End Namespace
Excel操作类的更多相关文章
- C#EXCEL 操作类--C#ExcelHelper操作类
主要功能如下1.导出Excel文件,自动返回可下载的文件流 2.导出Excel文件,转换为可读模式3.导出Excel文件,并自定义文件名4.将数据导出至Excel文件5.将指定的集合数据导出至Exce ...
- Excel 操作类
转载:http://www.cnblogs.com/fellowcheng/archive/2010/08/21/1805158.html ExcelHelper(Excel2007) Code hi ...
- C#常用工具类——Excel操作类
/// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> / ...
- C# Excel操作类
/// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> / ...
- C#常用工具类——Excel操作类(ZT)
本文转载于: http://www.cnblogs.com/zfanlong1314/p/3916047.html /// 常用工具类——Excel操作类 /// <para> ----- ...
- C#封装的VSTO Excel操作类
自己在用的Excel操作类,因为经常在工作中要操作Excel文件,可是使用vba实现起来实在是不方便,而且编写也很困难,拼接一个字符串都看的眼花. 这个时候C#出现了,发现使用C#来操作Excel非常 ...
- C#自定义Excel操作类
C#自定义Excel操作类,可以用于将DataTable导出到Excel文件,从Excel文件读取数据. using System; using System.IO; using System.Dat ...
- C# Excel操作类 ExcelHelper
实现C#与Excel文件的交互操作,实现以下功能: 1.DataTable 导出到 Excel文件 2.Model数据实体导出到 Excel文件[List<Model>] 3.导出数据到模 ...
- [.Net] C# Excel操作类 ExcelHelper
实现C#与Excel文件的交互操作,实现以下功能: 1.DataTable 导出到 Excel文件 2.Model数据实体导出到 Excel文件[List<Model>] 3.导出数据到模 ...
- 2019.06.05 ABAP EXCEL 操作类代码 OLE方式(模板下载,excel上传,内表下载)
一般使用标准的excel导入方法9999行,修改了标准的excel导入FM 整合出类:excel的 模板下载,excel上传,ALV内表下载功能. 在项目一开始可以SE24创建一个类来供整体开发使用, ...
随机推荐
- Windows下尝试PHP7提示丢失VCRUNTIME140.DLL的问题解决
前天PHP7.0.0正式版发布了,有一些比较好的改进,官方也说速度比php5.6快了两倍,性能上有了很大提升,并且也发布了从php5.x向php7迁移的问题,所以今后php网站迁移后能够大幅度的提升网 ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 如何在Ubuntu下安装”.deb“、”.bin“、”.tar.gz“、”.tar.bz2“格式的软件包!
今天在Ubuntu11.10中安装Google chrome浏览器是遇到了问题,下载好的“.deb”格式的安装文件google-chrome-stable.deb双击后或者右键快捷菜单选择 Synap ...
- HDU 5651 xiaoxin juju needs help (组合数)
xiaoxin juju needs helpTime Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64uSu ...
- windows设置java环境变量
JAVA_HOME C:\Program Files\java\jdk1.6.0_38 path %JAVA_HOME%\bin; CLASSPATH .;%JAVA_HOME%\lib\dt.jar ...
- 矩形覆盖(codevs 1101)
题目描述 Description 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4 ...
- Servlet获取参数
package action; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet ...
- LINUX_bash
$ myname=xor$ echo $myname xor 内容间空格$var="lang is $myname" echo $var lang is xor $ var='la ...
- java 学习之路
一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http://www.jcp.org/en/jsr/detail?id=133 http://i ...
- angularjs教程
http://www.runoob.com/angularjs/angularjs-routing.htmlA