C# 2007Excel 파일 읽기

5739 단어 Excel
오늘 한 가지 문제가 발생했습니다.
C# Excel 파일 읽기
 
소스 코드 제공
잊어버릴까봐 여기 기록해 주세요.
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.IO;
using System.Reflection;
using MSExcel = Microsoft.Office.Interop.Excel;

namespace ExcelTool
{
    public partial class FrmMain : Form
    {
        string str = @"C:\Program Files\Data\SpaceKeyword.xlsx";
        object missing = Missing.Value;
        MSExcel.Application app = null;
        MSExcel.Workbook wb = null;
        MSExcel.Worksheet ws = null;
        MSExcel.Range r = null;
        /// <summary>
        ///  Excel
        /// </summary>
        private void InitExcel()
        { 
            // excel
            app = new Microsoft.Office.Interop.Excel.Application();
            wb = app.Workbooks.Open(str, false, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            app.Visible = false;// Excel 

            // WorkSheet 
            ws = (MSExcel.Worksheet)wb.Worksheets.get_Item(1);
        }
        /// <summary>
        ///  Excel 
        /// </summary>
        private void ReadExcel()
        {
            InitExcel();
            // A1 
            r = ws.get_Range("A1", Type.Missing);
            string strA1 = r.Value;
            app.Quit();// 
            MessageBox.Show(strA1);
        }
        /// <summary>
        ///  Excel(Win7 )
        /// </summary>
        private void WriteExcel()
        {
            InitExcel();
            ws.Cells[1, 1] = " ";
            // Excel
            wb.Save();
            wb.Close(null, null, null);
            app.Workbooks.Close();
            app.Application.Quit();
            app.Quit();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

            ws = null;
            wb = null;
            app = null;
        }
    }
}
 
 
 
 
 public class Excel
    {
        public static String[] getWorksheetList(String connectionString)
        {
            OleDbConnection objConn = null;
            DataTable sheets = null;
            try
            {
                objConn = new OleDbConnection(connectionString);
                objConn.Open();

                sheets = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                int k = 0;
                String[] worksheets = new String[sheets.Rows.Count];
                foreach (DataRow row in sheets.Rows)
                {
                    worksheets[k] = row["TABLE_NAME"].ToString().Replace("'", "").Replace("$", "");
                }
                return worksheets;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
            finally
            {
                if (objConn != null)
                {
                    objConn.Close();
                    objConn.Dispose();
                }
                if (sheets != null)
                    sheets.Dispose();
            }
        }
        public static void echoAsCSV(string connectionString)
        {
            try
            {
                var adapter1 = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
                var ds = new DataSet();
                adapter1.Fill(ds, "results");
                DataTable data = ds.Tables["results"];

                for (int i = 0; i < data.Rows.Count; i++)
                {
                    for (int j = 0; j < data.Columns.Count; j++)
                        Console.Write("\"" + data.Rows[i].ItemArray[j] + "\";");
                    Console.WriteLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static void Main(string[] args)
        {
            foreach (String arg in args)
            {
                Console.WriteLine(arg);
            }

            var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;", "E:/1.xlsx");

            String[] worksheetList = getWorksheetList(connectionString);
            if (worksheetList != null)
                foreach (String worksheetName in worksheetList)
                    Console.WriteLine(worksheetName);

            echoAsCSV(connectionString);

            Console.Read();
        }
    }

좋은 웹페이지 즐겨찾기