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;
                }
            }
        }
  • 相关阅读:
    C++ SDL2事件处理
    C++ SDL_Image配置
    C++ TinyXML库读写XML
    C++ libcurl库使用
    C++ 配置使用libcurl
    C++ 正则使用
    C++使用cJSON
    Vue通过状态为页面切换添加loading、为ajax加载添加loading
    移动端真机调试工具--DebugGap (VIDE)
    new Date(str)返回的时间结果在移动端比PC端快了8小时
  • 原文地址:https://www.cnblogs.com/zlcom/p/3205090.html
Copyright © 2011-2022 走看看