将Excel行,列导入列表

时间:2017-10-13 07:08:09

标签: c# excel import

我正在寻找一种将Excel工作表的某些元素导入List的方法。我的目标是能够对Excel工作表的属性(第一行)进行排序(单击我想要查看的属性)并获取第一行下面的行的值。

2 个答案:

答案 0 :(得分:1)

我会在不使用Sheet接口但Worksheet类对象的情况下实现您想要的方式。

有一点需要注意的是,在获得二维数组中所有使用的范围后,我正在关闭excel表。这使得它更快,否则从范围读取会慢得多。可能有很多方法可以让它更快。

Application xlApp = new Application();
Workbook xlWorkBook = null;
Worksheet dataSheet = null;
Range dataRange = null;
List<string> columnNames = new List<string>();
object[,] valueArray;

try
{
    // Open the excel file
    xlWorkBook = xlApp.Workbooks.Open(fileFullPath, 0, true);

    if (xlWorkBook.Worksheets != null
        && xlWorkBook.Worksheets.Count > 0)
    {
        // Get the first data sheet
        dataSheet = xlWorkBook.Worksheets[1];

        // Get range of data in the worksheet
        dataRange = dataSheet.UsedRange;

        // Read all data from data range in the worksheet
        valueArray = (object[,])dataRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);

        if (xlWorkBook != null)
        {
            // Close the workbook after job is done
            xlWorkBook.Close();
            xlApp.Quit();
        }

        for (int colIndex = 0; colIndex < valueArray.GetLength(1); colIndex++)
        {
            if (valueArray[0, colIndex] != null
                && !string.IsNullOrEmpty(valueArray[0, colIndex].ToString()))
            {
                // Get name of all columns in the first sheet
                columnNames.Add(valueArray[0, colIndex].ToString());
            }
        }
    }

    // Now you have column names or to say first row values in this:
    // columnNames - list of strings
}
catch (System.Exception generalException)
{
    if (xlWorkBook != null)
    {
        // Close the workbook after job is done
        xlWorkBook.Close();
        xlApp.Quit();
    }
}

答案 1 :(得分:0)

工作副本可检索2行并使用Pwd Protected打开excel

class Program
{
    static void Main(string[] args)
    {
        Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkBook = xlApp.Workbooks.Open("YourPath", ReadOnly: true, Password: "PWD");
        Excel._Worksheet xlWorksheet = xlWorkBook.Sheets[1];
        Excel.Range xlRange = xlWorksheet.UsedRange;

        int rCnt;
        int rw = 0;
        int cl = 0;

        //get the total column count
        cl = xlRange.Columns.Count;

        List<MyRow> myRows = new List<MyRow>();
        for (rCnt = 1; rCnt <= 1; rCnt++)
        {
            if (rCnt % 6 == 1)
            {//get rows which ABC or XYZ is in

                for (int col = 2; col <= cl; col++)
                {//traverse columns (the first column is not included)

                    for (int rowABCD = rCnt; rowABCD <= rCnt + 5; rowABCD++)
                    {//traverse the following four rows after ABC row or XYZ row
                        MyRow myRow = new MyRow();
                        //get ABC or XYZ
                        myRow.Col1 = (string)(xlRange.Cells[rowABCD, 1] as Range).Value2.ToString();
                        // get the  value of current column  in ABC row or XYZ row
                        myRow.Col2 = (string)(xlRange.Cells[rowABCD, col] as Range).Value2.ToString();
                        // add the newly created myRow to the list
                        myRows.Add(myRow);
                    }
                }
            }
        }
        xlApp.Quit();
    }

    public class MyRow
    {
        public string Col1 { get; set; }
        public string Col2 { get; set; }
    }
}