zoukankan      html  css  js  c++  java
  • C# 操作excel类

     public class CExcel
        {
            private Application m_App = null;
            private Worksheet m_current_sheet = null;
            private Workbook m_work_book = null;
    
            public Range Border(int top, int left, int bottom, int right)
            {
                try
                {
                    if (this.m_current_sheet == null)
                    {
                        return null;
                    }
                    Worksheet worksheet = this.m_current_sheet;
                    Range range = worksheet.get_Range(worksheet.Cells[top, left], worksheet.Cells[bottom, right]);
                    range.Borders.LineStyle = 1;
                    return range;
                }
                catch
                {
                    return null;
                }
            }
    
            public void Close()
            {
                try
                {
                    this.m_App.Quit();
                    this.m_App = null;
                }
                catch
                {
                }
            }
    
            public bool Create()
            {
                try
                {
                    if (this.m_App != null)
                    {
                        this.Close();
                    }
                    this.m_App = new ApplicationClass();
                    if (this.m_App == null)
                    {
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
                return true;
            }
    
            public Workbook CreateWorkBook()
            {
                Workbooks workbooks = this.m_App.Workbooks;
                if (workbooks == null)
                {
                    this.m_App = null;
                    return null;
                }
                Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                if (workbook == null)
                {
                    this.m_App = null;
                    return null;
                }
                this.m_work_book = workbook;
                this.m_current_sheet = (Worksheet)workbook.Worksheets.get_Item(1);
                return workbook;
            }
    
            public string GetCellText(object row, object column)
            {
                try
                {
                    if (this.m_current_sheet == null)
                    {
                        return "";
                    }
                    Range range = (Range)this.m_current_sheet.Cells.get_Item(row, column);
                    if (range != null)
                    {
                        return (string)range.Text;
                    }
                }
                catch
                {
                    return null;
                }
                return null;
            }
    
            public Range GetRange(int top, int left, int bottom, int right)
            {
                try
                {
                    if (this.m_current_sheet == null)
                    {
                        return null;
                    }
                    Worksheet worksheet = this.m_current_sheet;
                    return worksheet.Cells.get_Range(worksheet.Cells[top, left], worksheet.Cells[bottom, right]);
                }
                catch
                {
                    return null;
                }
            }
    
            public Range Merge(int top, int left, int bottom, int right)
            {
                try
                {
                    if (this.m_current_sheet == null)
                    {
                        return null;
                    }
                    Worksheet worksheet = this.m_current_sheet;
                    Range range = this.m_current_sheet.Cells.get_Range(worksheet.Cells[top, left], worksheet.Cells[bottom, right]);
                    range.Merge(Missing.Value);
                    return range;
                }
                catch
                {
                    return null;
                }
            }
    
            public bool Open(string file)
            {
                try
                {
                    if (this.m_App != null)
                    {
                        this.Close();
                    }
                    this.m_App = new ApplicationClass();
                    if (this.m_App == null)
                    {
                        return false;
                    }
                    Workbooks workbooks = this.m_App.Workbooks;
                    if (workbooks == null)
                    {
                        this.m_App = null;
                        return false;
                    }
                    Workbook workbook = workbooks.Add(file);
                    if (workbook == null)
                    {
                        this.m_App = null;
                        return false;
                    }
                    this.m_work_book = workbook;
                    this.m_current_sheet = (Worksheet)workbook.Worksheets.get_Item(1);
                }
                catch
                {
                    return false;
                }
                return true;
            }
    
            public bool SetCellData(int row, int column, object data)
            {
                try
                {
                    if (this.m_current_sheet == null)
                    {
                        return false;
                    }
                    this.m_current_sheet.Cells[row, column] = data;
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            public bool SetCellColor(int row, int column, object color)
            {
                try
                {
                    if (this.m_current_sheet == null)
                    {
                        return false;
                    }
                    this.m_current_sheet.get_Range(this.m_current_sheet.Cells[row, column], this.m_current_sheet.Cells[row, column]).Interior.Color = color;
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            public Range SetCellText(int row, int column, string text)
            {
                try
                {
                    if (this.m_current_sheet == null)
                    {
                        return null;
                    }
                    Range range = (Range)this.m_current_sheet.Cells.get_Item(row, column);
                    range.NumberFormatLocal = "@";
                    this.m_current_sheet.Cells[row, column] = text;
                    return range;
                }
                catch
                {
                    return null;
                }
            }
    
            public Range SetColumnWidth(int column, double width)
            {
                try
                {
                    if (this.m_current_sheet == null)
                    {
                        return null;
                    }
                    Range range = (Range)this.m_current_sheet.Cells.get_Item(1, column);
                    range.ColumnWidth = width;
                    return range;
                }
                catch
                {
                    return null;
                }
            }
    
            public void SetCurrentSheet(Worksheet sheet)
            {
                if (sheet != null)
                {
                    this.m_current_sheet = sheet;
                }
            }
    
            public Range SetRowHeight(int row, double height)
            {
                try
                {
                    if (this.m_current_sheet == null)
                    {
                        return null;
                    }
                    Range range = (Range)this.m_current_sheet.Cells.get_Item(row, 1);
                    range.RowHeight = height;
                    range.Font.Name = "宋体";
                    range.Font.Size = 9;
                    return range;
                }
                catch
                {
                    return null;
                }
            }
    
            public void Show()
            {
                try
                {
                    if (this.m_App != null)
                    {
                        this.m_App.Visible = true;
                    }
                }
                catch
                {
                }
            }
    
            public Application app
            {
                get
                {
                    return this.m_App;
                }
            }
    
            public Worksheet sheet
            {
                get
                {
                    return this.m_current_sheet;
                }
            }
    
            public Workbook work_book
            {
                get
                {
                    return this.m_work_book;
                }
            }
        }
  • 相关阅读:
    ApacheCN 所有教程/文档集已备份到 Coding
    固态硬盘寿命天梯榜 2021.7
    一个垃圾佬的自我修养(一)工作站篇
    Java 向上转型
    记一次chromedriver与浏览器版本不匹配导致的问题(mac版本)
    关于C# 里面的axWindowsMediaPlayer的使用
    WCHAR的相关操作 范例 , 同时也是产生创建Sqlserver语句新表的 Sql
    C++ Win32 sokcet2.0版本 TCP 服务器
    C++ WIN 32 socket 2.0版本 TCP客户端
    数据库和传感器糅合 数据部分程序 正常运行
  • 原文地址:https://www.cnblogs.com/zlcom/p/3205090.html
Copyright © 2011-2022 走看看