点关注不迷路

Max

ExcelHelper

using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace EXCEL
{
    /// <summary>
    /// 最后一定要记得close
    /// </summary>
    public class ExcelEditHelper
    {
        public string mFilename;
        public Application app;
        public Workbooks wbs;
        public Workbook wb;
        public Worksheets wss;
        public Worksheet ws;

        /// <summary>
        /// 创建一个Excel对象
        /// </summary>
        public void Create()
        {
            app = new Application();
            wbs = app.Workbooks;
            wb = wbs.Add(true);
        }

        /// <summary>
        /// 打开一个Excel文件
        /// </summary>
        /// <param name="FileName">excel文件名,包括文件路径</param>
        public void Open(string FileName)
        {
            object missing = System.Reflection.Missing.Value;
            app = new Application();
            wbs = app.Workbooks;
            wb = wbs.Add(FileName);
            wb = wbs.Open(FileName, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing);
            mFilename = FileName;
            //取得第一个工作薄
            ws = (Worksheet)wb.Worksheets.get_Item(1);
            //取得总记录行数   (包括标题列)
            int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
            int columnsint = ws.UsedRange.Cells.Columns.Count;//得到列数
            
            //第一行第二列
            string ss = ws.Cells[1, 2].Value2.ToString();
            //取得数据范围区域 (不包括标题列 B2到Bn) 
            Range rng1 = ws.Cells.get_Range("B2", "B" + rowsint);   
            object[,] arryItem = (object[,])rng1.Value2;   //get range's value
            
            for (int i = 1; i <= rowsint - 1; i++)
            {
                string str = arryItem[i, 1].ToString();
            }
        }

        /// <summary>
        /// 获取一个工作表
        /// </summary>
        /// <param name="SheetName">工作表名称</param>
        /// <returns></returns>
        public Worksheet GetSheet(string SheetName)
        {
            Worksheet s = (Worksheet)wb.Worksheets[SheetName];
            return s;
        }

        /// <summary>
        /// 添加一个工作表
        /// </summary>
        /// <param name="SheetName">工作表名称</param>
        /// <returns></returns>
        public Worksheet AddSheet(string SheetName)
        {
            Worksheet s = (Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            s.Name = SheetName;
            return s;
        }

        /// <summary>
        /// 复制并添加一个工作表
        /// </summary>
        /// <param name="OldSheetName"> 被复制工作表</param>
        /// <param name="NewSheetName">新表</param>
        public void CloneSheet(string OldSheetName, string NewSheetName)
        {
            Microsoft.Office.Interop.Excel.Worksheet oldSheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[OldSheetName];
            oldSheet.Copy(oldSheet, Type.Missing);
            Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[OldSheetName + " (2)"];
            s.Name = NewSheetName;
        }

        /// <summary>
        /// 删除一个工作表
        /// </summary>
        /// <param name="SheetName">工作表名称</param>
        public void DelSheet(string SheetName)
        {
            ((Worksheet)wb.Worksheets[SheetName]).Delete();
        }

        /// <summary>
        /// 重命名一个工作表
        /// </summary>
        /// <param name="OldSheetName">被替换名称</param>
        /// <param name="NewSheetName">替换名称</param>
        /// <returns></returns>
        public Worksheet ReNameSheet(string OldSheetName, string NewSheetName)
        {
            Worksheet s = (Worksheet)wb.Worksheets[OldSheetName];
            s.Name = NewSheetName;
            return s;
        }

        /// <summary>
        /// 重命名一个工作表
        /// </summary>
        /// <param name="Sheet">被替换工作表</param>
        /// <param name="NewSheetName">替换名称</param>
        /// <returns></returns>
        public Worksheet ReNameSheet(Worksheet Sheet, string NewSheetName)
        {
            Sheet.Name = NewSheetName;
            return Sheet;
        }

        /// <summary>
        /// 设置单元格的值
        /// </summary>
        /// <param name="ws">工作表</param>
        /// <param name="x">行标</param>
        /// <param name="y">列标</param>
        /// <param name="value">数据</param>
        public void SetCellValue(Worksheet ws, int x, int y, object value)
        {
            ws.Cells[x, y] = value;
        }

        /// <summary>
        /// 设置单元格的值
        /// </summary>
        /// <param name="ws">工作表名称</param>
        /// <param name="x">行标</param>
        /// <param name="y">列标</param>
        /// <param name="value">数据</param>
        public void SetCellValue(string ws, int x, int y, object value)
        {
            GetSheet(ws).Cells[x, y] = value;
        }

        /// <summary>
        /// 设置单元格属性
        /// </summary>
        /// <param name="ws">工作表</param>
        /// <param name="Startx">起始行标</param>
        /// <param name="Starty">起始列标</param>
        /// <param name="Endx">终止行标</param>
        /// <param name="Endy">终止列标</param>
        /// <param name="size">字体大小</param>
        /// <param name="name">字体</param>
        /// <param name="color">颜色</param>
        /// <param name="HorizontalAlignment">对齐方式</param>
        public void SetCellProperty(Worksheet ws, int Startx, int Starty, int Endx, int Endy, int size, string name, Constants color, Constants HorizontalAlignment)
        {
            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;
        }

        /// <summary>
        /// 设置单元格属性
        /// </summary>
        /// <param name="ws">工作表名称</param>
        /// <param name="Startx">起始行标</param>
        /// <param name="Starty">起始列标</param>
        /// <param name="Endx">终止行标</param>
        /// <param name="Endy">终止列标</param>
        /// <param name="size">字体大小</param>
        /// <param name="name">字体</param>
        /// <param name="color">颜色</param>
        /// <param name="HorizontalAlignment">对齐方式</param>
        public void SetCellProperty(string wsn, int Startx, int Starty, int Endx, int Endy, int size, string name, Constants color, Constants HorizontalAlignment)
        {
            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;
        }

        /// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="ws">工作表</param>
        /// <param name="x1">起始行标</param>
        /// <param name="y1">起始列标</param>
        /// <param name="x2">终止行标</param>
        /// <param name="y2">终止列标</param>
        public void UniteCells(Worksheet ws, int x1, int y1, int x2, int y2)
        {
            ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Merge(Type.Missing);
        }

        /// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="ws">工作表名称</param>
        /// <param name="x1">起始行标</param>
        /// <param name="y1">起始列标</param>
        /// <param name="x2">终止行标</param>
        /// <param name="y2">终止列标</param>
        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);
        }

        /// <summary>
        /// 将内存中数据表格插入到Excel指定工作表的指定位置
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <param name="ws">工作表名称</param>
        /// <param name="startX">起始行标</param>
        /// <param name="startY">起始列标</param>
        public void InsertTable(System.Data.DataTable dt, string ws, int startX, int startY)
        {
            int itm = 0;
            foreach (var item in dt.Columns)
            {
                GetSheet(ws).Cells[startX, startY + itm] = item.ToString();
                itm++;
            }
            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {
                    GetSheet(ws).Cells[startX + i+1, j + startY] = dt.Rows[i][j];
                }
            }
        }
        
        /// <summary>
        /// 将内存中数据表格插入到Excel指定工作表的指定位置
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <param name="ws">工作表名称</param>
        /// <param name="startX">起始行标</param>
        /// <param name="startY">起始列标</param>
        public void InsertTable(System.Data.DataTable dt, Worksheet ws, int startX, int startY)
        {
            int itm = 0;
            foreach (var item in dt.Columns)
            {
                ws.Cells[startX, startY + itm] = item.ToString();
                itm++;
            }
            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                for (int j = 0; j <= dt.Columns.Count - 1; j++)
                {
                    ws.Cells[startX + i+1, j + startY] = dt.Rows[i][j];
                }
            }
        }

        /// <summary>
        /// 插入表头
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ws">工作表名称</param>
        public void InsertRowName<T>(string ws) where T : class
        {
            Dictionary<string, string> dic = Resolve<T>();
            int itm = 0;
            foreach (var item in dic)
            {
                GetSheet(ws).Cells[1, 1 + itm] = item.Value;
                itm++;
            }
        }
        //获取属性名称及显示名称
        private static Dictionary<string, string> Resolve<T>()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            var t = typeof(T);
            foreach (var property in t.GetProperties())
            {
                //获取displayname(需在类属性上添加 [DisplayName("**")]特性)
                DisplayNameAttribute[] x = property.GetCustomAttributes(typeof(DisplayNameAttribute), true) as DisplayNameAttribute[];
                if (x != null && x.Length > 0)
                {
                    if (!dic.ContainsKey(property.Name))
                    {
                        dic.Add(property.Name, x[0].DisplayName);
                    }
                }
            }
            return dic;
        }

        /// <summary>
        /// 保存文档
        /// </summary>
        /// <returns></returns>
        public bool Save()
        {
            if (mFilename == "")
            {
                return false;
            }
            else
            {
                try
                {
                    wb.Save();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 文档另存为
        /// </summary>
        /// <param name="FileName">文件名(包含路径)</param>
        /// <returns></returns>
        public bool SaveAs(object FileName)
        {
            try
            {
                wb.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 关闭一个Excel对象,销毁对象
        /// </summary>
        public void Close()
        {
            wb.Close(Type.Missing, Type.Missing, Type.Missing);
            wbs.Close();
            app.Quit();
            wb = null;
            wbs = null;
            app = null;
            GC.Collect();
        }
    }
}

 

posted @ 2018-04-27 13:48  Max麦克斯  阅读(411)  评论(2编辑  收藏  举报