C#中怎麼在EXCEL中的儲存格中畫斜線啊 ??

來源:互聯網
上載者:User

標籤:des   style   color   os   io   for   ar   問題   

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:\WebSite2\Book2.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; }
        }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.