.NET的ExcelOperate
using System;
using System.Web;
using Excel = Microsoft.Office.Interop.Excel; namespace Comm
{
/// <summary>
/// ExcelOperate 的摘要说明。Excel操作函数
/// </summary>
public class ExcelOperate
{
private object mValue = System.Reflection.Missing.Value; public ExcelOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
} /// <summary>
/// 合并单元格
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
public void Merge(Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
{
CurSheet.get_Range(objStartCell, objEndCell).Merge(mValue);
}
/// <summary>
/// 设置连续区域的字体大小
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="strStartCell">开始单元格</param>
/// <param name="strEndCell">结束单元格</param>
/// <param name="intFontSize">字体大小</param>
public void SetFontSize(Excel._Worksheet CurSheet, object objStartCell, object objEndCell, int intFontSize)
{
CurSheet.get_Range(objStartCell, objEndCell).Font.Size = intFontSize.ToString();
} /// <summary>
/// 横向打印
/// </summary>
/// <param name="CurSheet"></param>
public void xlLandscape(Excel._Worksheet CurSheet)
{
CurSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape; }
/// <summary>
/// 纵向打印
/// </summary>
/// <param name="CurSheet"></param>
public void xlPortrait(Excel._Worksheet CurSheet)
{
CurSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlPortrait;
} /// <summary>
/// 在指定单元格插入指定的值
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="Cell">单元格 如Cells[1,1]</param>
/// <param name="objValue">文本、数字等值</param>
public void WriteCell(Excel._Worksheet CurSheet, object objCell, object objValue)
{
CurSheet.get_Range(objCell, mValue).Value2 = objValue; } /// <summary>
/// 在指定Range中插入指定的值
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="StartCell">开始单元格</param>
/// <param name="EndCell">结束单元格</param>
/// <param name="objValue">文本、数字等值</param>
public void WriteRange(Excel._Worksheet CurSheet, object objStartCell, object objEndCell, object objValue)
{
CurSheet.get_Range(objStartCell, objEndCell).Value2 = objValue;
} /// <summary>
/// 合并单元格,并在合并后的单元格中插入指定的值
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
/// <param name="objValue">文本、数字等值</param>
public void WriteAfterMerge(Excel._Worksheet CurSheet, object objStartCell, object objEndCell, object objValue)
{
CurSheet.get_Range(objStartCell, objEndCell).Merge(mValue);
CurSheet.get_Range(objStartCell, mValue).Value2 = objValue; } /// <summary>
/// 为单元格设置公式
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objCell">单元格</param>
/// <param name="strFormula">公式</param>
public void SetFormula(Excel._Worksheet CurSheet, object objCell, string strFormula)
{
CurSheet.get_Range(objCell, mValue).Formula = strFormula;
} /// <summary>
/// 单元格自动换行
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
public void AutoWrapText(Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
{
CurSheet.get_Range(objStartCell, objEndCell).WrapText = true;
} /// <summary>
/// 设置整个连续区域的字体颜色
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
/// <param name="clrColor">颜色</param>
public void SetColor(Excel._Worksheet CurSheet, object objStartCell, object objEndCell, System.Drawing.Color clrColor)
{
CurSheet.get_Range(objStartCell, objEndCell).Font.Color = System.Drawing.ColorTranslator.ToOle(clrColor);
} /// <summary>
/// 设置整个连续区域的单元格背景色
/// </summary>
/// <param name="CurSheet"></param>
/// <param name="objStartCell"></param>
/// <param name="objEndCell"></param>
/// <param name="clrColor"></param>
public void SetBgColor(Excel._Worksheet CurSheet, object objStartCell, object objEndCell, System.Drawing.Color clrColor)
{
CurSheet.get_Range(objStartCell, objEndCell).Interior.Color = System.Drawing.ColorTranslator.ToOle(clrColor);
} /// <summary>
/// 设置连续区域的字体名称
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
/// <param name="fontname">字体名称 隶书、仿宋_GB2312等</param>
public void SetFontName(Excel._Worksheet CurSheet, object objStartCell, object objEndCell, string fontname)
{
CurSheet.get_Range(objStartCell, objEndCell).Font.Name = fontname;
} /// <summary>
/// 设置连续区域的字体为黑体
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
public void SetBold(Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
{
CurSheet.get_Range(objStartCell, objEndCell).Font.Bold = true;
} /// <summary>
/// 设置连续区域的边框:上下左右都为黑色连续边框
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
public void SetBorderAll(Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
{
CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlEdgeTop].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous; CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlEdgeBottom].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous; CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlEdgeLeft].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous; CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlEdgeRight].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous; CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlInsideHorizontal].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous; CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlInsideVertical].Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
CurSheet.get_Range(objStartCell, objEndCell).Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous; } /// <summary>
/// 设置连续区域水平居中
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
public void SetHAlignCenter(Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
{
CurSheet.get_Range(objStartCell, objEndCell).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
} /// <summary>
/// 设置连续区域水平居左
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
public void SetHAlignLeft(Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
{
CurSheet.get_Range(objStartCell, objEndCell).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
} /// <summary>
/// 设置连续区域水平居右
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
public void SetHAlignRight(Excel._Worksheet CurSheet, object objStartCell, object objEndCell)
{
CurSheet.get_Range(objStartCell, objEndCell).HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
} /// <summary>
/// 设置连续区域的显示格式
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
/// <param name="strNF">如"#,##0.00"的显示格式</param>
public void SetNumberFormat(Excel._Worksheet CurSheet, object objStartCell, object objEndCell, string strNF)
{
CurSheet.get_Range(objStartCell, objEndCell).NumberFormat = strNF;
} /// <summary>
/// 设置列宽
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="strColID">列标识,如A代表第一列</param>
/// <param name="dblWidth">宽度</param>
public void SetColumnWidth(Excel._Worksheet CurSheet, string strColID, double dblWidth)
{
((Excel.Range)CurSheet.Columns.GetType().InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, CurSheet.Columns, new object[] { (strColID + ":" + strColID).ToString() })).ColumnWidth = dblWidth;
} /// <summary>
/// 设置列宽
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
/// <param name="dblWidth">宽度</param>
public void SetColumnWidth(Excel._Worksheet CurSheet, object objStartCell, object objEndCell, double dblWidth)
{
CurSheet.get_Range(objStartCell, objEndCell).ColumnWidth = dblWidth;
} /// <summary>
/// 设置行高
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objStartCell">开始单元格</param>
/// <param name="objEndCell">结束单元格</param>
/// <param name="dblHeight">行高</param>
public void SetRowHeight(Excel._Worksheet CurSheet, object objStartCell, object objEndCell, double dblHeight)
{
CurSheet.get_Range(objStartCell, objEndCell).RowHeight = dblHeight;
} /// <summary>
/// 为单元格添加超级链接
/// </summary>
/// <param name="CurSheet">Worksheet</param>
/// <param name="objCell">单元格</param>
/// <param name="strAddress">链接地址</param>
/// <param name="strTip">屏幕提示</param>
/// <param name="strText">链接文本</param>
public void AddHyperLink(Excel._Worksheet CurSheet, object objCell, string strAddress, string strTip, string strText)
{
CurSheet.Hyperlinks.Add(CurSheet.get_Range(objCell, objCell), strAddress, mValue, strTip, strText);
} /// <summary>
/// 另存为xls文件
/// </summary>
/// <param name="CurBook">Workbook</param>
/// <param name="strFilePath">文件路径</param>
public void Save(Excel._Workbook CurBook, string strFilePath)
{
CurBook.SaveCopyAs(strFilePath);
} /// <summary>
/// 保存文件
/// </summary>
/// <param name="CurBook">Workbook</param>
/// <param name="strFilePath">文件路径</param>
public void SaveAs(Excel._Workbook CurBook, string strFilePath)
{
CurBook.SaveAs(strFilePath, mValue, mValue, mValue, mValue, mValue, Excel.XlSaveAsAccessMode.xlShared, mValue, mValue, mValue, mValue, mValue);
} /// <summary>
/// 另存为html文件
/// </summary>
/// <param name="CurBook">Workbook</param>
/// <param name="strFilePath">文件路径</param>
public void SaveHtml(Excel._Workbook CurBook, string strFilePath)
{
CurBook.SaveAs(strFilePath, Excel.XlFileFormat.xlHtml, mValue, mValue, mValue, mValue, Excel.XlSaveAsAccessMode.xlNoChange, mValue, mValue, mValue, mValue, mValue);
} /// <summary>
/// 释放内存
/// </summary>
public void Dispose(Excel._Worksheet CurSheet, Excel._Workbook CurBook, Excel._Application CurExcel)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(CurSheet);
CurSheet = null;
CurBook.Close(false, mValue, mValue);
System.Runtime.InteropServices.Marshal.ReleaseComObject(CurBook);
CurBook = null; CurExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(CurExcel);
CurExcel = null; GC.Collect();
GC.WaitForPendingFinalizers();
}
catch (System.Exception ex)
{
HttpContext.Current.Response.Write("在释放Excel内存空间时发生了一个错误:" + ex);
}
finally
{
foreach (System.Diagnostics.Process pro in System.Diagnostics.Process.GetProcessesByName("Excel"))
//if (pro.StartTime < DateTime.Now)
pro.Kill();
}
System.GC.SuppressFinalize(this); } }
}
.NET的ExcelOperate的更多相关文章
- Npoi导入导出Excel操作
之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...
- C#导出EXCEL的几种方法
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.W ...
- 读取本地excel发短信
package com.cmcc.zysoft.sellmanager.controller; import java.io.File; import java.io.FileInputStream; ...
- POI读入excel文件到Java中
package Poi_Test; //导入java自带的包 import java.io.BufferedInputStream; import java.io.File; import java. ...
- C#导出EXCEL的方法
/// using System; using System.Collections.Generic; using System.Text; using System.Data; using Syst ...
- java 读取excel 正常 xls
package com.sun.test; import java.io.BufferedInputStream;import java.io.File;import java.io.FileInpu ...
- java 读取excel文件(只读取xls文件)
package com.sun.test; import java.io.BufferedInputStream;import java.io.File;import java.io.FileInpu ...
- java读取Excel文档插入mysql
/** * 读取excel插入myslq */package com.excel; import java.io.BufferedInputStream;import java.io.File;imp ...
- C# 导出Excel的示例(转)
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.W ...
随机推荐
- C#方法参数传递机制
1:value(值传递).ref(引用传递).out(输出传递) ref和out使用效果上面是等效的,它们的区别在于:参数标记为ref,那么必须在调用函数之前初始化参数的值:参数标记为out,调用函数 ...
- django使用ldap认证
pip3 install django-auth-ldap python-ldap urls.py, from app0104 import views urlpatterns = [ url(r'^ ...
- LCD相关知识点
1.LCD即液晶显示器,控制原理是控制其中的电子枪,在n行*n列的屏幕上投射不同颜色从而形成图像 2.编程步骤: ①打开LCD背光将LCD背光对应的GPIO设置为禁止上拉(GPxUP相应位写入1),选 ...
- DES带IV向量加密解密工具
链接:http://pan.baidu.com/s/1kVAV80J 密码:sgys 鉴于网上的DES加密解密都是不带IV向量的 我就自制了一个带IV向量的DES加密解密的小工具 © 2016-20 ...
- mac平台多个php版本快速切换
mac平台多个php版本快速切换 要求所有php版本都是由brew安装 使用brew安装php多版本方法 # brew install php56 # brew install php70 安装切换工 ...
- ios网络知识
http://www.cocoachina.com/bbs/read.php?tid-31300.html 启蒙内容 http://blog.csdn.net/nono_love_lilith/ar ...
- PowerDesigner怎样才能在修改表的字段Name的时候Code不自动跟着变
怎样才能在修改表的字段Name的时候,Code不自动跟着变 tools-> General Options-> Dialog:Operation Modes: 去掉 NameToC ...
- nginx限制单个IP访问配置
最近公司做了一个砸金蛋的活动,经过几天的发酵宣传后,每天以几万的的用户数在增长,后面才发现原来有人专门为此开发了一个全自动注册的软件 一时间网站被刷得打开异常缓慢,查看日志发现大部分都是用软件在刷,于 ...
- xmind的第十三天笔记
- HDFS的运行原理
简介 HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的.论文为GFS(Google File System)Go ...