zoukankan      html  css  js  c++  java
  • C#中怎么在EXCEL中的单元格中画斜线啊 ??

    Code Snippet

    做法:

    1,先添加引用COM,找 Excel

    2,using Excel = Microsoft.Office.Interop.Excel;

    3,

    代码

            private Excel.Application m_objExcel = null;
            private Excel.Workbooks m_objBooks = null;
            private Excel._Workbook m_objBook = null;
            private Excel.Sheets m_objSheets = null;
            private Excel._Worksheet m_objSheet = null;
            private Excel.Range m_objRange = null;

            private object m_objOpt = System.Reflection.Missing.Value;

     

            private void button3_Click(object sender, EventArgs e)
            {
                m_objExcel = new Excel.Application();
                m_objExcel.Visible = false;
                m_objExcel.DisplayAlerts = false;

                if (m_objExcel.Version != "11.0")
                {
                    MessageBox.Show("您的 Excel 版本不是 11.0 (Office 2003),操作可能会出现问题。");
                    m_objExcel.Quit();
                    return;
                }

                m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;

                m_objBook = m_objBooks.Open(@"E:WebSite2Book2.xls", m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);

                m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
                m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
                m_objRange = m_objSheet.get_Range("B3", m_objOpt);
                m_objRange.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalDown].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
                m_objRange.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalUp].LineStyle = Excel.XlLineStyle.xlContinuous;
                m_objRange.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalUp].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
                m_objRange.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
                m_objRange.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
                m_objRange.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
                m_objRange.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
                m_objBook.Save();

                m_objBook.Close(false, m_objOpt, m_objOpt);
                m_objExcel.Quit();

                this.DisposeExcel();
            }

     

     

     

     /// <summary>
            /// 释放所引用的COM对象。注意:这个过程一定要执行。
            /// </summary>
            public void DisposeExcel()
            {

                ReleaseObj(m_objSheets);
                ReleaseObj(m_objBook);
                ReleaseObj(m_objBooks);
                ReleaseObj(m_objExcel);
                System.GC.Collect();
                System.GC.WaitForPendingFinalizers();
            }
            private void ReleaseObj(object o)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
                }
                catch { }
                finally { o = null; }
            }

     

  • 相关阅读:
    销售人员个人提升经典书籍推荐
    销售必看的书籍推荐
    我在公司敲代码,你却在家和老王………————程序员的那些梗
    某程序员动了公司的祖传代码"屎山",半年后怒交辞职报告!
    为什么说程序员的前三年不要太看重工资水平?
    好的员工关系应该是怎样的?
    粒子群优化算法(PSO)python 3实现
    Python基础篇-安装python
    Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围
    空TreeList添加节点不显示
  • 原文地址:https://www.cnblogs.com/gisoracle/p/3894859.html
Copyright © 2011-2022 走看看