在VS下开发应用程序是很方便的,对Excel也一样,下面是我对操作Excel的总结
 ExcelBorders.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Reflection;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
namespace TN.Office.Access
{
    /// <summary>
    /// Excel边框处理处理类
    /// </summary>
    public class ExcelBorders
    {
        /// <summary>
        /// 设置Excel边框
        /// </summary>
        /// <param name="Range">目标区域对象</param>
        public void SetBorders(Range Range)
        {
            Range.Borders.LineStyle = 1;//设置表格的边框
            //Range.Interior.ColorIndex = 15;//设置背景颜色
            Range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
            Range.Borders.Weight = XlBorderWeight.xlThin;
            //Range.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlDash;//设置线条
            //Range.Borders.get_Item(XlBordersIndex.xlEdgeBottom).Weight = XlBorderWeight.xlThick;
        }
    }
}
 
ExcelChart.cs
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Reflection;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
namespace TN.Office.Access
{
    /// <summary>
    /// Excel图表处理类
    /// </summary>
    public class ExcelChart
    {
        /// <summary>
        /// 图表对象
        /// </summary>
        private _Chart m_Chart = null;
        /// <summary>
        /// 图象中各个列的说明对象
        /// </summary>
        private Series m_Series = null;
        /// <summary>
        /// 图象自动套用格式
        /// </summary>
        private int Format = 1;
        /// <summary>
        /// 指定系列中的数据是来自于行还是列
        /// </summary>
        private object PlotBy = XlRowCol.xlColumns;
        /// <summary>
        /// 图表标题
        /// </summary>
        private string ChartTitle = string.Empty;
        /// <summary>
        /// 横坐标标题
        /// </summary>
        private string XTitle = string.Empty;
        /// <summary>
        /// 纵坐标标题
        /// </summary>
        private string YTitle = string.Empty;
        /// <summary>
        /// 左边距
        /// </summary>
        private float Left = 0.0f;
        /// <summary>
        /// 上边距
        /// </summary>
        private float Top = 0.0f;
        /// <summary>
        /// 图表标题数组
        /// </summary>
        private object[] ObjectHeader = null;
        /// <summary>
        /// 获取或设置图表标题数组
        /// </summary>
        public object[] _ObjectHeader
        {
            get
            {
                return ObjectHeader;
            }
            set
            {
                ObjectHeader = value;
            }
        }
        /// <summary>
        /// 获取或设置图象套用格式编号
        /// </summary>
        public int _Format
        {
            get
            {
                return Format;
            }
            set
            {
                Format = value;
            }
        }
        /// <summary>
        /// 获取或设置系列中的数据是来自于行还是列
        /// </summary>
        public object _PlotBy
        {
            get
            {
                return PlotBy;
            }
            set
            {
                PlotBy = value;
            }
        }
        /// <summary>
        /// 设置或获取图表标题
        /// </summary>
        public string _ChartTitle
        {
            get
            {
                return ChartTitle;
            }
            set
            {
                ChartTitle = value;
            }
        }
        /// <summary>
        /// 设置或获取横标题
        /// </summary>
        public string _XTitle
        {
            get
            {
                return XTitle;
            }
            set
            {
                XTitle = value;
            }
        }
        /// <summary>
        /// 设置或获取纵标题
        /// </summary>
        public  string _YTitle
        {
            get
            {
                return YTitle;
            }
            set
            {
                YTitle = value;
            }
        }
        /// <summary>
        /// 设置或获取左边距
        /// </summary>
        public float _Left
        {
            get
            {
                return Left;
            }
            set
            {
                Left = value;
            }
        }
        /// <summary>
        /// 设置或获取上边距
        /// </summary>
        public float _Top
        {
            get
            {
                return Top;
            }
            set
            {
                Top = value;
            }
        }
        /// <summary>
        /// 创建图表
        /// </summary>
        /// <param name="strInfoMsg">出错信息</param>
        /// <param name="Range">Excel区域对象</param>
        public void CreateChart(out string strInfoMsg,Range Range)
        {
            try
            {
                ExcelManager Manager = new ExcelManager();
                string MyString = string.Empty;
                m_Chart = (_Chart)Manager._objBook.Charts.Add(Missing.Value, Missing.Value,
                             Missing.Value, Missing.Value);//获得空图表对象
                m_Chart.ChartWizard(Range, XlChartType.xlLine,Format,PlotBy, 1, 1, true,ChartTitle,XTitle,YTitle, Missing.Value);//创建图象向导
                for (int i = 1; i <= ObjectHeader.Length; i++)
                {
                    m_Series = (Series)m_Chart.SeriesCollection(i);
                    MyString = "=\"" + ObjectHeader[i - 1] + "";
                    MyString = MyString + "\"";
                    m_Series.Name = MyString;
                }
                //图表定位
                m_Chart.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAsObject,Manager._objSheet.Name);
                Manager._objSheet.Shapes.Item(0).Left = Left;
                Manager._objSheet.Shapes.Item(0).Top = Top;
                strInfoMsg = string.Empty;
            }
            catch (Exception er)
            {
                strInfoMsg = er.Message.ToString();
                return;
            }
        }
    }
}
 
ExcelFont.cs
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Reflection;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
namespace TN.Office.Access
{
    /// <summary>
    /// Excel字体处理类
    /// </summary>
    public class ExcelFont
    {
        /// <summary>
        /// 字体名称
        /// </summary>
        private string FontName = string.Empty;
        /// <summary>
        /// 字体大小
        /// </summary>
        private int Size = 0;
         /// <summary>
        /// 是否为粗体
        /// </summary>
        private bool Bold =false;
        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="FontName">字体名称</param>
        public ExcelFont(string FontName)
        {
            this.FontName = FontName;
        }
        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="FontName">字体名称</param>
        /// <param name="Size">字体大小</param>
        public ExcelFont(string FontName,int Size)
        {
            this.FontName = FontName;
            this.Size = Size;
        }
        /// <summary>
        /// 重载构造函数
        /// </summary>
        /// <param name="FontName">字体名称</param>
        /// <param name="Size">字体大小</param>
        /// <param name="Bold">是否为粗体,默认为正常体</param>
        public ExcelFont(string FontName, int Size, bool Bold)
        {
            this.FontName = FontName;
            this.Size = Size;
            this.Bold = Bold;
        }
        /// <summary>
        /// 设置Excel字体
        /// </summary>
        /// <param name="Range">目标区域对象</param>
        public void SetFont(Range Range)
        {
            Range.WrapText = true;
            Range.Font.Bold = Bold;
            Range.Font.Size = Size;
            Range.Font.Name = FontName;
            Range.VerticalAlignment = XlVAlign.xlVAlignCenter;
            Range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
        }
    }
}
 
ExcelManager.cs
 
namespace TN.Office.Access
{
    using Microsoft.Office.Core;
    using Microsoft.Office.Interop.Excel;
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;
    public class ExcelManager : IDisposable
    {
        private static _Workbook m_objBook = null;
        private static Workbooks m_objBooks = null;
        private static ApplicationClass m_objExcel = null;
        private static _Worksheet m_objSheet = null;
        private static Range m_Range = null;
        public ExcelManager()
        {
            m_objExcel = new ApplicationClass();
            m_objExcel.Visible = false;
            m_objBooks = m_objExcel.Workbooks;
            m_objBook = m_objBooks.Add(Missing.Value);
            m_objSheet = (Worksheet) m_objBook.ActiveSheet;
            m_objExcel.Application.DisplayAlerts = false;
        }
        public ExcelManager(string TemplateFilePath)
        {
            m_objExcel = new ApplicationClass();
            m_objExcel.Workbooks.Open(TemplateFilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            m_objExcel.Visible = false;
            m_objBooks = m_objExcel.Workbooks;
            m_objBook = m_objBooks.Add(Missing.Value);
            m_objSheet = (Worksheet) m_objBook.ActiveSheet;
            m_objExcel.Application.DisplayAlerts = false;
        }
        public void AddActiveSheet(string CurrentSheetName)
        {
            m_objSheet = (Worksheet) m_objBook.Sheets.Add(Missing.Value, Missing.Value, Missing.Value, XlSheetType.xlWorksheet);
            m_objSheet.Name = CurrentSheetName;
        }
        public void AddActiveSheet(string BeforeSheetName, string CurrentSheetName)
        {
            Worksheet after = (Worksheet) m_objBooks.get_Item(1).Sheets.get_Item(BeforeSheetName);
            m_objSheet = (Worksheet) m_objBook.Sheets.Add(Missing.Value, after, Missing.Value, XlSheetType.xlWorksheet);
            m_objSheet.Name = CurrentSheetName;
            m_objSheet.Activate();
        }
        public string CellText()
        {
            m_Range = (Range) m_objSheet.Cells[Property._nBeginX, Property._nBeginY];
            return m_Range.Text.ToString();
        }
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(true);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.Disposing();
            }
        }
        protected internal void Disposing()
        {
            m_objBook.Close(false, Missing.Value, Missing.Value);
            m_objBooks.Close();
            ShutExcel();
            Marshal.ReleaseComObject(m_objSheet);
            m_objSheet = null;
            Marshal.ReleaseComObject(m_objBook);
            m_objBook = null;
            Marshal.ReleaseComObject(m_objBooks);
            m_objBooks = null;
            Marshal.ReleaseComObject(m_objExcel);
            m_objExcel = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        public void ExcelTotal(string Method, string strTotalBegin, string strTotalEnd)
        {
            m_Range = (Range) m_objSheet.Cells[Property._nBeginX, Property._nBeginY];
            m_Range.Formula = "=" + Method + "(" + strTotalBegin + ":" + strTotalEnd + ")";
        }
        public void FillExcel(object objdata)
        {
            m_Range = (Range) m_objSheet.Cells[Property._nBeginX, Property._nBeginY];
            m_Range.Cells.Value2 = objdata;
            m_Range.EntireColumn.AutoFit();
            m_Range.EntireRow.AutoFit();
        }
        public void FillExcel(object[,] objdata)
        {
            m_Range = m_objSheet.get_Range(m_objSheet.Cells[Property._nBeginX, Property._nBeginY], m_objSheet.Cells[Property._nBeginY + Property._nColumn, Property._nBeginX + Property._nRow]);
            m_Range.Value2 = objdata;
            m_Range.EntireColumn.AutoFit();
            m_Range.EntireRow.AutoFit();
        }
        public void FillExcel(object objdata, object Cell1, object Cell2)
        {
            m_Range = m_objSheet.get_Range(Cell1, Cell2);
            m_Range.Value2 = objdata;
            m_Range.EntireColumn.AutoFit();
            m_Range.EntireRow.AutoFit();
        }
        public Range GetRange()
        {
            m_Range = m_objSheet.get_Range(m_objSheet.Cells[Property._nBeginX, Property._nBeginY], m_objSheet.Cells[Property._nBeginY + Property._nColumn, Property._nBeginX + Property._nRow]);
            return m_Range;
        }
        public Range GetRange(object Cell1, object Cell2)
        {
            m_Range = m_objSheet.get_Range(Cell1, Cell2);
            return m_Range;
        }
        [DllImport("User32.dll", CharSet=CharSet.Auto)]
        private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        public void InsertPicture(string RangeName, string PicturePath)
        {
            m_Range = m_objSheet.get_Range(RangeName, Missing.Value);
            m_Range.Select();
            ((Pictures) m_objSheet.Pictures(Missing.Value)).Insert(PicturePath, Missing.Value);
        }
        public void InsertPicture(string RangeName, string PicturePath, float PictuteWidth, float PictureHeight)
        {
            m_Range = m_objSheet.get_Range(RangeName, Missing.Value);
            m_Range.Select();
            float left = Convert.ToSingle(m_Range.Left);
            float top = Convert.ToSingle(m_Range.Top);
            m_objSheet.Shapes.AddPicture(PicturePath, MsoTriState.msoFalse, MsoTriState.msoTrue, left, top, PictuteWidth, PictureHeight);
        }
        public void MergeCell(object CellIndex1, object CellIndex2)
        {
            m_Range = m_objSheet.get_Range(CellIndex1, CellIndex2);
            m_Range.Merge(false);
        }
        public void PrintExcel()
        {
            m_objSheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;
            m_objSheet.PageSetup.Orientation = XlPageOrientation.xlPortrait;
            m_objSheet.PageSetup.HeaderMargin = 0.0;
            m_objSheet.PageSetup.FooterMargin = 0.0;
            m_objSheet.PageSetup.LeftMargin = m_objExcel.InchesToPoints(0.354330708661417);
            m_objSheet.PageSetup.RightMargin = m_objExcel.InchesToPoints(0.354330708661417);
            m_objSheet.PageSetup.TopMargin = m_objExcel.InchesToPoints(0.393700787401575);
            m_objSheet.PageSetup.BottomMargin = m_objExcel.InchesToPoints(0.393700787401575);
            m_objSheet.PageSetup.CenterHorizontally = true;
            m_objSheet.PrintOut(Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        }
        public void SaveExcel(out string strInfoMsg, string strSavePath, bool IsShow)
        {
            try
            {
                m_objExcel.Visible = IsShow;
                m_objBook.SaveAs(strSavePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                strInfoMsg = string.Empty;
            }
            catch (Exception exception)
            {
                strInfoMsg = exception.Message.ToString();
            }
        }
        public void SaveExcel(out string strInfoMsg, string strSavePath, string strFileName, bool IsShow)
        {
            try
            {
                m_objExcel.Visible = IsShow;
                m_objBook.SaveAs(Path.Combine(strSavePath, strFileName), Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                strInfoMsg = string.Empty;
            }
            catch (Exception exception)
            {
                strInfoMsg = exception.Message.ToString();
            }
        }
        protected internal static void ShutExcel()
        {
            IntPtr hwnd = new IntPtr(m_objExcel.Hwnd);
            int iD = 0;
            GetWindowThreadProcessId(hwnd, out iD);
            Process.GetProcessById(iD).Kill();
        }
        public _Workbook _objBook
        {
            get
            {
                return m_objBook;
            }
        }
        public Workbooks _objBooks
        {
            get
            {
                return m_objBooks;
            }
        }
        public _Worksheet _objSheet
        {
            get
            {
                return m_objSheet;
            }
        }
        public int CurrentWorkSheetIndex
        {
            set
            {
                if ((value <= 0) || (value > m_objBook.Worksheets.Count))
                {
                    throw new Exception("索引超出范围");
                }
                object obj2 = value;
                m_objSheet = m_objBook.Worksheets[obj2] as _Worksheet;
            }
        }
    }
}
 
 
ExcelObject.cs
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Reflection;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
namespace TN.Office.Access
{
    /// <summary>
    /// Excel处理类
    /// </summary>
    public class ExcelObject
    {
        #region Excel对象
        /// <summary>
        /// Excel程序对象
        /// </summary>
        private static ApplicationClass m_objExcel=null;
        /// <summary>
        /// Excel工作簿对象集合
        /// </summary>
        private static Workbooks m_objBooks = null;
        /// <summary>
        /// Excel工作簿对象
        /// </summary>
        private static _Workbook m_objBook = null;
        /// <summary>
        /// Excel工作空间对象
        /// </summary>
        private static _Worksheet m_objSheet = null;
        /// <summary>
        /// Excel获取的空间对象
        /// </summary>
        private static Range m_Range = null;
        /// <summary>
        /// 图表对象
        /// </summary>
        private static _Chart m_Chart = null;
        /// <summary>
        /// 图象中各个列的说明对象
        /// </summary>
        private static Series m_Series = null;
        #endregion
        /// <summary>
        /// 构造函数
        /// </summary>
        public ExcelObject()
        {
           m_objExcel = new ApplicationClass();
           m_objExcel.Visible = false;
           m_objBooks = (Microsoft.Office.Interop.Excel.Workbooks)m_objExcel.Workbooks;
           m_objBook = (Microsoft.Office.Interop.Excel.Workbook)(m_objBooks.Add(Missing.Value));
           m_objSheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.ActiveSheet;
        }
        /// <summary>
        /// 创建Excel文件
        /// </summary>
        /// <param name="objdata">Excel二维表数组</param>
        public void CreateExcel(object[,] objdata)
        {
            m_Range = m_objSheet.get_Range(m_objSheet.Cells[ExcelProperty._nBeginX, ExcelProperty._nBeginY], m_objSheet.Cells[ExcelProperty._nBeginY + ExcelProperty._nColumn, ExcelProperty._nBeginX + ExcelProperty._nRow]);//Excel中目标区域
            m_Range.Value2 = objdata;
            m_Range.EntireColumn.AutoFit();//完全覆盖单元格
        }
        #region 统计数据
        /// <summary>
        /// 统计和值
        /// </summary>
        /// <param name="Method">统计方法</param>
        /// <param name="strTotalBegin">统计的开始区域</param>
        /// <param name="strTotalEnd">统计的结束区域</param>
        public Range ExcelTotalSum(string Method,string strTotalBegin,string strTotalEnd)
        {
            m_Range = m_objSheet.get_Range(strTotalBegin, strTotalEnd);//Excel中目标区域
            m_Range.Formula = "="+Method+"(" + strTotalBegin + ":" + strTotalEnd + ")";
            return m_Range;
        }
        /// <summary>
        /// 统计平均值
        /// </summary>
        /// <param name="strTotalBegin">统计的开始区域</param>
        /// <param name="strTotalEnd">统计的结束区域</param>
        public Range ExcelTotalAverage(string strTotalBegin, string strTotalEnd)
        {
            m_Range = m_objSheet.get_Range(m_objSheet.Cells[ExcelProperty._nBeginX, ExcelProperty._nBeginY], m_objSheet.Cells[ExcelProperty._nBeginY + ExcelProperty._nColumn, ExcelProperty._nBeginX + ExcelProperty._nRow]);//Excel中目标区域
            m_Range.Formula = "=AVERAGE(" + strTotalBegin + ":" + strTotalEnd + ")";
            m_Range.NumberFormat = "0.00";
            return m_Range;
        }
        #endregion
        /// <summary>
        /// 创建图表
        /// </summary>
        /// <param name="strInfoMsg">出错信息</param>
        public void CreateChart(out string strInfoMsg,object[] objHeaders)
        {
            try
            {
                string MyString = string.Empty;
                m_Range = m_objSheet.get_Range(m_objSheet.Cells[ExcelProperty._nBeginX, ExcelProperty._nBeginY], m_objSheet.Cells[ExcelProperty._nBeginY + ExcelProperty._nColumn,ExcelProperty._nBeginX + ExcelProperty._nRow]);//Excel中目标区域
                m_Chart = (_Chart)m_objBook.Charts.Add(Missing.Value, Missing.Value,
                             Missing.Value,Missing.Value);//获得空图表对象
                m_Chart.ChartWizard(m_Range, XlChartType.xlLine,
                    ExcelProperty._Format, ExcelProperty._PlotBy, 1, 1, true,
                    ExcelProperty._ChartTitle, ExcelProperty._XTitle, ExcelProperty._YTitle, Missing.Value);//创建图象向导
                for (int i = 1; i <=objHeaders.Length; i++)
                {
                    m_Series = (Series)m_Chart.SeriesCollection(i);
                    MyString = "=\"" + objHeaders[i-1] + "";
                    MyString = MyString + "\"";
                    m_Series.Name = MyString;
                }
               
                //图表定位
                m_Chart.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAsObject, m_objSheet.Name);
                m_objSheet.Shapes.Item(0).Left = ExcelProperty._Left;
                m_objSheet.Shapes.Item(0).Top = ExcelProperty._Top;
                strInfoMsg = string.Empty;
            }
            catch (Exception er)
            {
                strInfoMsg = er.Message.ToString();
                return;
            }
        }
        /// <summary>
        /// 设置Excel样式
        public void SetStyle()
        {
            m_Range = m_objSheet.get_Range(m_objSheet.Cells[ExcelProperty._nBeginX, ExcelProperty._nBeginY], m_objSheet.Cells[ExcelProperty._nBeginY + ExcelProperty._nColumn, ExcelProperty._nBeginX + ExcelProperty._nRow]);//Excel中目标区域
            m_Range.WrapText = true;
            m_Range.Font.Name = "黑体";
            m_Range.Font.Bold = true;
            m_Range.Borders.LineStyle = 1;//设置表格的边框
            //m_Range.Interior.ColorIndex = 35;//设置背景颜色
            m_Range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
            m_Range.VerticalAlignment = XlVAlign.xlVAlignCenter;
            m_Range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
            m_Range.Borders.Weight = XlBorderWeight.xlThin;
            //m_Range.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlDash;//设置线条
            //m_Range.Borders.get_Item(XlBordersIndex.xlEdgeBottom).Weight = XlBorderWeight.xlThick;
        }
        /// <summary>
        /// 保存Excel文件
        /// </summary>
        /// <param name="strInfoMsg">出错信息</param>
        /// <param name="strSheetName">工作簿名称</param>
        /// <param name="strSavePath">工作簿名称</param>
        public void SaveExcel(out string strInfoMsg,string strSheetName, string strSavePath)
        {
            try
            {
                m_objSheet.Name = strSheetName;//工作簿名称
                m_objExcel.Visible = true;//显示Excel程序
                m_objBook.SaveAs(strSavePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                strInfoMsg = string.Empty;
            }
            catch(Exception er)
            {
                strInfoMsg = er.Message.ToString();
            }
            finally
            {
                //关闭Excel应用程序
                m_objBook.Close(true, Missing.Value, Missing.Value);
                m_objBooks.Close();
                Disposing();//释放资源
            }
        }
        /// <summary>
        /// 释放Excel程序占有的系统资源
        /// </summary>
        public static void Disposing()
        {
            //释放COM对象引用
            System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Range);
            m_Range = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
            m_objSheet = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
            m_objBook = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
            m_objBooks = null;
            m_objExcel.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
            m_objExcel = null;
            GC.Collect();
        }
    }
}
 
ExcelProperty.cs
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
namespace TN.Office.Access
{
    public class ExcelProperty
    {
        #region 定义图表
        /// <summary>
        /// 开始单元格横序号
        /// </summary>
        private static int nBeginX=0;
        /// <summary>
        /// 开始单元格纵序号
        /// </summary>
        private static int nBeginY = 0;
        /// <summary>
        /// 横向移动长度
        /// </summary>
        private static int nRow = 0;
        /// <summary>
        /// 纵向移动长度
        /// </summary>
        private static int nColumn = 0;
        /// <summary>
        /// 图象自动套用格式
        /// </summary>
        private static int Format = 1;
        /// <summary>
        /// 指定系列中的数据是来自于行还是列
        /// </summary>
        private static object PlotBy = XlRowCol.xlColumns;
        /// <summary>
        /// 图表标题
        /// </summary>
        private static string ChartTitle=string.Empty;
        /// <summary>
        /// 横坐标标题
        /// </summary>
        private static string XTitle=string.Empty;
        /// <summary>
        /// 纵坐标标题
        /// </summary>
        private static string YTitle=string.Empty;
        /// <summary>
        /// 左边距
        /// </summary>
        private static float Left = 0.0f;
        /// <summary>
        /// 上边距
        /// </summary>
        private static float Top = 0.0f;
        /// <summary>
        /// 获取或设置开始单元格横序号
        /// </summary>
        public static int _nBeginX
        {
            get
            {
                return nBeginX;
            }
            set
            {
                nBeginX = value;
            }
        }
        /// <summary>
        /// 获取或设置开始单元格纵序号
        /// </summary>
        public static int _nBeginY
        {
            get
            {
                return nBeginY;
            }
            set
            {
                nBeginY = value;
            }
        }
        /// <summary>
        /// 获取或设置横向移动长度
        /// </summary>
        public static int _nRow
        {
            get
            {
                return nRow;
            }
            set
            {
                nRow = value;
            }
        }
        /// <summary>
        /// 获取或设置纵向移动长度
        /// </summary>
        public static int _nColumn
        {
            get
            {
                return nColumn;
            }
            set
            {
                nColumn = value;
            }
        }
        /// <summary>
        /// 获取或设置图象套用格式编号
        /// </summary>
        public static int _Format
        {
            get
            {
                return Format;
            }
            set
            {
                Format = value;
            }
        }
        /// <summary>
        /// 获取或设置系列中的数据是来自于行还是列
        /// </summary>
        public static object _PlotBy
        {
            get
            {
                return PlotBy;
            }
            set
            {
                PlotBy = value;
            }
        }
        /// <summary>
        /// 设置或获取图表标题
        /// </summary>
        public static string _ChartTitle
        {
            get
            {
                return ChartTitle;
            }
            set
            {
                ChartTitle = value;
            }
        }
        /// <summary>
        /// 设置或获取横标题
        /// </summary>
        public static string _XTitle
        {
            get
            {
                return XTitle;
            }
            set
            {
                XTitle = value;
            }
        }
        /// <summary>
        /// 设置或获取纵标题
        /// </summary>
        public static string _YTitle
        {
            get
            {
                return YTitle;
            }
            set
            {
                YTitle = value;
            }
        }
        /// <summary>
        /// 设置或获取左边距
        /// </summary>
        public static float _Left
        {
            get
            {
                return Left;
            }
            set
            {
                Left = value;
            }
        }
        /// <summary>
        /// 设置或获取上边距
        /// </summary>
        public static float _Top
        {
            get
            {
                return Top;
            }
            set
            {
                Top = value;
            }
        }
        #endregion
    }
}
 
Property.cs
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
namespace TN.Office.Access
{
    /// <summary>
    /// EXCEL属性
    /// </summary>
    public class Property
    {
        #region 定义图表
        /// <summary>
        /// 开始行索引
        /// </summary>
        private static int BeginRowIndex = 0;
        /// <summary>
        /// 开始列索引
        /// </summary>
        private static int BeginColumnIndex = 0;
        /// <summary>
        /// 横向移动长度
        /// </summary>
        private static int RowCount = 0;
        /// <summary>
        /// 纵向移动长度
        /// </summary>
        private static int ColumnCount = 0;
      
        /// <summary>
        /// 获得或设置开始行索引,必须为大于等于1整数
        /// </summary>
        public static int _nBeginX
        {
            get
            {
                return BeginRowIndex;
            }
            set
            {
                BeginRowIndex = value;
            }
        }
        /// <summary>
        /// 获得或设置开始列索引,必须为大于等于1整数
        /// </summary>
        public static int _nBeginY
        {
            get
            {
                return BeginColumnIndex;
            }
            set
            {
                BeginColumnIndex = value;
            }
        }
        /// <summary>
        /// 获取或设置横向移动长度
        /// </summary>
        public static int _nRow
        {
            get
            {
                return RowCount;
            }
            set
            {
                RowCount = value;
            }
        }
        /// <summary>
        /// 获取或设置纵向移动长度
        /// </summary>
        public static int _nColumn
        {
            get
            {
                return ColumnCount;
            }
            set
            {
                ColumnCount = value;
            }
        }
      
        #endregion
    }
}

转载于:https://blog.51cto.com/yerik/324673

DotNet操作Excel汇总相关推荐

  1. Fintech系列(三) -- python对excel操作模块汇总||推荐指数||用法示例

    python对excel操作模块汇总||推荐指数||用法示例 Working with Excel Files in Python 总览 读写Excel的python第三方开源模块 Excel写操作插 ...

  2. Python操作excel常用模块汇总

    #收集的python相关信息#可能是全网最完整的 Python 操作 Excel库总结! #https://zhuanlan.zhihu.com/p/353669230#:~:text=%E5%8F% ...

  3. asp操作excel,显示数据及统计方法

    这次做的活是asp操作excel,全程序是每行都标出,然后像填空一样做的.现在被我改成了动态显示每行的项,再逐行显示,最后做一个汇总. 1.FSO找到excel文件 <% Dim fso,fol ...

  4. 告别ASP.NET操作EXCEL的烦恼

    Copy From 告别ASP.NET操作EXCEL的烦恼(总结篇) 公元19XX年前,关于EXCEL的操作就如滔滔江水,连绵不绝,真正操作EXCEL我也是从去年下半年开始的,有些比较复杂的年度报表之 ...

  5. dotNET面试题汇总系列连载(1):基础语法

    点击上方"dotNET全栈开发","设为星标" 加"星标★",每天11.50,好文必达 全文约4000字,预计阅读时间8分钟 马上要到202 ...

  6. asp.net 设置 excel alignment_教你如何用Python轻轻松松操作Excel、Word、CSV,一文就够了,赶紧码住!!!...

    作者:奈何缘浅wyj https://juejin.im/post/6868073137263607821 Python 操作 Excel 常用工具 数据处理是 Python 的一大应用场景,而 Ex ...

  7. java excel 数据有效性_poi操作excel设置数据有效性

    private void setDataValidationList(short firstRow,short endRow,short firstCol, short endCol,String d ...

  8. .NET/C#使用NPOI操作Excel

    前言 Asp.net/C#操作Excel最惨的就是环境配置了:使用NPOI能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ...

  9. 告别ASP.NET操作EXCEL的烦恼(总结篇)

    公元19XX年前,关于EXCEL的操作就如滔滔江水,连绵不绝,真正操作EXCEL我也是从去年下半年开始的,有些比较复杂的年度报表之类的,做起来也有点费力,不过还是都能画出来了,关于EXCEL的报表导出 ...

最新文章

  1. 【转】Android下编译jni库的二种方法(含示例) -- 不错
  2. Maven 的获取、安装与环境变量设置方法
  3. mysql 主从配置(master slave)
  4. 线性回归csv数据集_测试数据科学家线性回归的30个问题
  5. spring MVC中页面添加锚点
  6. github 国内加速镜像
  7. 【转】vue项目打包部署——nginx代理访问
  8. animate改变背景颜色_3D MAX2016视口背景设置里各参数的含义详解 - 3dmax基础操作入门教程-3dmax材质教程,3d材质贴图教程参数,vray材质参数,3dmax贴图教程...
  9. [2]2019-CVPR-Learning Loss for Active Learning 论文笔记
  10. SNE T分布 t-SNE数据降维与可视化
  11. caffe框架的介绍
  12. 读取 RV1126 CPU温度 NPU CPU频率
  13. BZOJ3110 K大数查询
  14. 程序员如何改善精神内耗?
  15. Problem G: 薪酬计算
  16. 哥哥教你学嵌入式 之 智芯科技 开发板 Z20K11x系列 教程(一)
  17. ecshop mysql 标题表_ECshop每个数据库表结构说明_MySQL
  18. AOSP: 下载 android 源码
  19. 思博伦设备修改接口速率的三种方式
  20. 定时清理linux 服务器日志 crontab

热门文章

  1. C/C++面试例题讲解
  2. 汇编语言---统计数据区的正、负数并分开存放
  3. 二叉树先序、中序和后序遍历
  4. PostGIS mysql_fdw安装(Linux)
  5. [转]EXCEL截取字符串中某几位的函数——LeftMIDRight及Find函数的使用
  6. kafka调试工具kafkacat的使用
  7. CentOS7配置JAVA环境变量
  8. Crashlytics功能集成
  9. VC编写程序在debug下正常,在release下错误
  10. 服务器控件开发之复杂属性