在excel中设置第一行的第一个单元格

时间:2012-06-15 05:59:21

标签: c# .net

我需要在c sharp中写一行,用于在读取后将第一行的第一个单元格存储在字符串中。

它类似于: -

str=read(row[index]);

c sharp中的确切陈述是什么?有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

private Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

private static string ReadSpreadsheet()
        {

            Workbook wb = null;
            wb = excel.Workbooks.Open("C:\APathToExcelSpreadsheet.xls", false, true);

            //Get the values in the sheet
            Range rng = wb.ActiveSheet.UsedRange;
            object[,] values;
            if (rng.Value2.GetType().IsArray)
            {
                values = (object[,])rng.Value2;
            }
            else
            {
                values = new object[2, 2];
                values[1, 1] = rng.Value2;
            }


            for (int row = 1; row <= values.GetUpperBound(0); row++)
            {
                for (int col = 1; col <= values.GetUpperBound(1); col++)
                {
                    if (values[row, col] != null)
                    {
....

答案 1 :(得分:0)

你可以通过首先使用excel interop来读它,首先包括你的c盘中office12或office14文件夹的引用,如果在你的电脑上安装了ms office,或者在导入参考文件后在visualstudio文件夹中这是代码

public static void GetExcelData(string _path)
    {



        Excel.Application xlApp = new Excel.ApplicationClass();
        Excel._Workbook xlWorkbook = xlApp.Workbooks.Open(_path);
        Excel._Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets.get_Item(1);
        Excel.Range xlRange = xlWorksheet.UsedRange;



        string firstcell == (xlRange.Cells[1, 1] as Excel.Range).Value2.ToString();


        xlWorkbook.Close(true, Type.Missing, Type.Missing);
        xlApp.Quit();



    }