开发者

How do I read an Excel file into a matrix?

I have an xslx file which contains two开发者_StackOverflow columns: ID, and Date of creation. I don't know the length of the file. How can i read the file to a matrix (I also don't want the columns titles to be in the matrix, only the data)?


The code in the following link may help you:

How to read an Excel file in c sharp

You may give the path to your xlsx file as appropriate. Also you may use the following lines after the line range = xlWorkSheet.UsedRange; in the code:

string[,] requiredData = new string[range.Rows.Count - 1,range.Columns.Count];

to create the matrix.

Also you may write data to the above created matrix instead of showing MessageBox(You may modify the loop as follows):

for (rCnt = 2; rCnt <= range.Rows.Count; rCnt++)
            {
                for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                {
                    str = (range.Cells[rCnt, cCnt] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();
                    requiredData[rCnt - 2][cCnt - 1] = str;
                }
            }

Hope this helps...


    /// <summary>
    /// Import Function For Xlsx  File
    /// </summary>
    /// <param name="s">File Name</param>
    /// <returns> Datatable </returns>
    private DataTable Import4Xlsx(string s)
    {
        string conn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                      "Data Source=" + s + ";" +
                      "Extended Properties='Excel 12.0 Xml;Allow Zero DateTime=True;" +
                      "HDR=YES;IMEX=1\"'";

        string[] sheetname = GetExcelSheetNames(conn);
        try
        {
            var objConn = new OleDbConnection(conn);
            objConn.Open();
            var ds = new DataSet();
            var da = new OleDbDataAdapter("SELECT * FROM [" + sheetname[0] + "]", conn);
            da.Fill(ds);
            objConn.Close();
            return ds.Tables[0];//resultant data
        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.Message + ex.Source);
            return null;
        }
    }

    /// <summary>
    /// Get Excel Files Sheet Name
    /// </summary>
    /// <param name="con">Connection String</param>
    private String[] GetExcelSheetNames(string con)
    {
        OleDbConnection objConn = null;
        DataTable dt = null;
        try
        {
            objConn = new OleDbConnection(con);
            objConn.Open();
            dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            if (dt == null) return null;
            var excelSheets = new String[dt.Rows.Count];
            int i = 0;
            foreach (DataRow row in dt.Rows)
            {
                excelSheets[i] = row["TABLE_NAME"].ToString();
                i++;
            }
            return excelSheets;
        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.Message + ex.Source);
            return null;
        }
        finally
        {
            if (objConn != null)
            {
                objConn.Close();
                objConn.Dispose();
            }
            if (dt != null)
            {
                dt.Dispose();
            }
        }
    }

Try it works fine

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜