C# 작업 Excel 테이블

3904 단어 Excel
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;
using ExcelTest.Properties;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelTest
{
    public partial class Form1 : Form
    {
        public static string StrCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =C:\\08.xls;Extended Properties=Excel 8.0";
        public static DataSet Ds;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender,EventArgs e)
        {
 
        }

        private void button1_Click(object sender,EventArgs e)
        {
            var fileName = Application.StartupPath + "\\1.xls";
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            var excel = new Excel.ApplicationClass
                            {Visible = false, DisplayAlerts = false, AlertBeforeOverwriting = false};

            try
            {
                var book = excel.Workbooks.Add(true);
                var sheet = book.Worksheets[1] as Excel.Worksheet;
                if (sheet != null) sheet.Cells[1,1] = "123";
                

                book.Save();
                book.Close(Type.Missing, Type.Missing, Type.Missing);
                
                // excel    
                excel.Save(fileName);
                excel.SaveWorkspace(fileName);
            }
            catch (Exception)
            {
            }
            CloseExcelApp(excel);
        }

        private void button2_Click(object sender,EventArgs e)
        {
            var fileName = Application.StartupPath + "\\1.xls";
            if (!File.Exists(fileName))
            {
                MessageBox.Show(Resources.Form1_button2_Click_ _); return; ;
            }

            var excel = new Excel.ApplicationClass
                            {Visible = false,DisplayAlerts = false,AlertBeforeOverwriting = false};
            try
            {

                var workbooks = excel.Workbooks;
                workbooks.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
         
                var book = workbooks.get_Item(1);
                var sheet = book.Worksheets[1] as Excel.Worksheet;
                sheet.Cells[1,1] = "234";
                sheet = null;
                book.Save();

                // excel    
                excel.Save(fileName);
                excel.SaveWorkspace(fileName);
            }
            catch (Exception)
            {
                
            }

            CloseExcelApp(excel);
        }

        private void button3_Click(object sender,EventArgs e)
        {
            var conn = new OleDbConnection(StrCon);
            
            const string sql = "select * from [Sheet1$]";
            //const string sql = "delete from [Sheet1$]";
            conn.Open();
            var myCommand = new OleDbDataAdapter(sql,StrCon);
            Ds = new DataSet();
            
            myCommand.Fill(Ds,"[Sheet1$]");
            conn.Close();

            dataGridView1.DataSource = Ds.Tables[0];
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var excel = new Excel.ApplicationClass {Visible = false};
            excel.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
            GC.Collect();
        }

        void CloseExcelApp(Excel.ApplicationClass app)
        {
            app.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
            GC.Collect();
        }
    }
}

좋은 웹페이지 즐겨찾기