Excel操作

C#获取Excel各类信息的方法
http://hi.baidu.com/it_program/item/66459ae9e7cd08395a7cfb33

Cell1
Type: System.Object
The name of the range in A1-style notation in the language of the application.It can include the range operator (a colon), the intersection operator (a space), or the union operator (a comma).It can also include dollar signs, but they are ignored.You can use a local defined name in any part of the range.If you use a name, the name is assumed to be in the language of the application.This parameter is required.

Cell2
Type: System.Object
The cell in the lower-right corner of the range.Can be a Microsoft.Office.Interop.Excel.Range that contains a single cell, an entire column, an entire row, or it can be a string that names a single cell in the language of the application.This parameter is optional.


综合代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Excel;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices; 



namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
             Application excelApp = new Application();
            try
            {
                Workbooks books = excelApp.Workbooks;
                Workbook book = books.Add(Type.Missing);
                Worksheet sheet = (Worksheet)book.Worksheets[1];
                for (int i = 1; i < 10; i++)
                {
                    for (int j = 1; j < 10; j++)
                    {
                        sheet.Cells[i,j] = "1";
                        Range rg = (Range)sheet.Cells[i, j];
                        Borders bds = rg.Borders;
                        bds.LineStyle = XlLineStyle.xlContinuous;
                        bds.Weight = XlBorderWeight.xlThick;

                        Range rgg = sheet.get_Range("$A$1", "$B$7");
                        rgg.Borders.LineStyle = XlLineStyle.xlSlantDashDot;
                        

                    }
                }

                book.Saved = true;
                book.SaveCopyAs(@"C:\Documents and Settings\Administrator\桌面\dldd\ee.xls");
                excelApp.Quit();
                excelApp = null;
            }
            catch(Exception ex)
            {
                IntPtr excelIPtr = new IntPtr(excelApp.Hwnd);
                excelApp.Quit();
                excelApp = null;

                int pid = -1;
                GetWindowThreadProcessId(excelIPtr, out pid);
                if (-1 != pid)
                {
                    Process pc = Process.GetProcessById(pid);
                    pc.Kill();
                }
                
                
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                
                throw ex;
                
            }

        }

        [DllImport("user32", EntryPoint = "GetWindowThreadProcessId")]
        private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int pid);

        
    }
}
posted @ 2012-11-21 22:18  wahgon  阅读(475)  评论(0编辑  收藏  举报