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的更多相关文章

  1. Npoi导入导出Excel操作

    之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...

  2. C#导出EXCEL的几种方法

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.W ...

  3. 读取本地excel发短信

    package com.cmcc.zysoft.sellmanager.controller; import java.io.File; import java.io.FileInputStream; ...

  4. POI读入excel文件到Java中

    package Poi_Test; //导入java自带的包 import java.io.BufferedInputStream; import java.io.File; import java. ...

  5. C#导出EXCEL的方法

    /// using System; using System.Collections.Generic; using System.Text; using System.Data; using Syst ...

  6. java 读取excel 正常 xls

    package com.sun.test; import java.io.BufferedInputStream;import java.io.File;import java.io.FileInpu ...

  7. java 读取excel文件(只读取xls文件)

    package com.sun.test; import java.io.BufferedInputStream;import java.io.File;import java.io.FileInpu ...

  8. java读取Excel文档插入mysql

    /** * 读取excel插入myslq */package com.excel; import java.io.BufferedInputStream;import java.io.File;imp ...

  9. C# 导出Excel的示例(转)

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.W ...

随机推荐

  1. c# 远程连接ORACLE数据库

    使用该方法,只需要传入几个必要的参数就可以进行数据库的远程连接测试了,连接成功返回TRUE,失败返回false. 说明: 第一个参数表示你在数据库中的用户,具有可以登录权限的 第二个参数表示用户的密码 ...

  2. 1st贝塞尔函数的使用

    x=-100:0.1:100; y1=besselj(7,x);y2=besselj(10,x);y3=besselj(20,x);y4=besselj(40,x);y5=besselj(60,x); ...

  3. CRYPTO-MD5

    这是昨天WHUCTF比赛的一道题目,本属于crypto,其实和crypto没多大关系, 比赛时其实差不多有这种思路了,但不相信自己,就没这样做下去,回来之后,照做了,果然是这样 链接:http://p ...

  4. POJ2653判断直线是否相交

    bool judge(node p1,node p2,node p3,node p4){    if(min(p1.x,p2.x)>max(p3.x,p4.x)||min(p1.y,p2.y)& ...

  5. Xcode6 storyboard new push segue 后的视图控制器没有navigation item bug.

    手动切一下 老的push,再切回来,就会出有了,我想是一个bug. Xcode 6 Segue with UINavigationItem up vote0down votefavorite   I' ...

  6. Doherty Threshold

    Prior to the publication of the IBM technical paper behind what commonly known today as the Doherty ...

  7. Linux chroot 并使用之前系统设备节点

    /********************************************************************************* * Linux chroot 并使 ...

  8. robotframework接口测试初探1

    robotframework这个框架最近很多人在使用它,大部分是和selenium结合的,大概看了下,然后发现这个做接口测试感觉也还不错,初步研究了下 环境安装: robotframework这个环境 ...

  9. Latex 数学符号

    本文完全转自 <常用数学符号的 LaTeX 表示方法>,在此转载仅仅为了便于查阅,谨向原作者致以崇高的敬意. 常用数学符号的 LaTeX 表示方法 (以下内容主要摘自“一份不太简短的 LA ...

  10. web安全之sql注入的防御

    自动把引号转义     1.防御sql注入的基本原则           任何时候不应该改变用户的输入                    比如用户输入单引号,那输出也要是单引号.   几种基本的防 ...