日期:2014-05-18  浏览次数:20831 次

C#中如何去读取Excel表格?
首先,如何去读取Excel表格?其次,怎样去读取特定的行,特定的列,特定的单元格?
请高手指教,有例子的话更好,谢谢各位大侠啊!

------解决方案--------------------
C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Data;
using System.Data.OleDb;

namespace xiaoh
{
    public class ExcelHelper
    {
        /// <summary>
        /// 写入Excel文档
        /// </summary>
        /// <param name="xPath">文件名称</param>
        /// <param name="source">需要写入的内容</param>
        /// <param name="sheetIndex">sheet序列</param>
        public static void writeExcel(string xPath, string[][] source, int sheetIndex)
        {
            Application ExcelRS = new ApplicationClass();
            Workbook RSbook = ExcelRS.Workbooks.Open(xPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            Worksheet RSsheet = (Worksheet)RSbook.Sheets.get_Item(sheetIndex);
            for (int i = 0; i < source.Length; i++)
            {
                for (int j = 0; j < source[i].Length; j++)
                {
                    RSsheet.Cells[i + 1, j + 1] = source[i][j];
                }
            }
            RSbook.Save();
            RSbook.Close();
            RSsheet = null;
            RSbook = null;
            ExcelRS = null;
            collect();
        }

        /// <summary>
        /// 读取Excel文档
        /// </summary>
        /// <param name="xPath">文件名称</param>
        /// <param name="sheetIndex">sheet序号</param>
        /// <returns>返回一个数据集</returns>
        public static DataSet readExcel(string xPath, int sheetIndex)
        {
            Microsoft.Office.Interop.Excel.Application ExcelRS = new ApplicationClass();
            Workbook RSbook = ExcelRS.Workbooks.Open(xPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            Worksheet RSsheet = (Worksheet)RSbook.Sheets.get_Item(sheetIndex);
            string sheetName = RSsheet.Name;
            RSbook.Close();
            RSsheet = null;
            RSbook = null;
            ExcelRS = null;
            collect();
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + xPath + ";" + "Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(strConn);
            try
            {
                conn.Open();
                string strExcel = string.Format("select * from [{0}$]", sheetName);
                OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
                DataSet ds = new DataSet();
                myCommand.Fill(ds);
                return ds;
            }
            catch
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
        }

        private static void collect()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}

------解决方案--------------------
http://topic.csdn.net/u/20110425/10/33fd08e6-54c2-4c89-adb4-e3507273ef66.html看这个帖子我发的例子
------解决方案--------------------
C# code
private void Button4_Click(object sender, System.EventArg