Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Microsoft.Office.Interop.excel从excel 2010读取数据_C#_.net_Excel_Office Interop - Fatal编程技术网

C# 使用Microsoft.Office.Interop.excel从excel 2010读取数据

C# 使用Microsoft.Office.Interop.excel从excel 2010读取数据,c#,.net,excel,office-interop,C#,.net,Excel,Office Interop,我无法读取Excel中的数据。以下是我正在使用的代码: 使用Excel=Microsoft.Office.Interop.Excel; Excel.Application xlApp=新的Excel.Application(); Excel.Workbook xlWorkbook=xlApp.Workbooks.Open(@“Book1.xlsx”,0,true,5,“,”,true,Excel.XlPlatform.xlWindows,“\t”,false,false,0,true,1,0);

我无法读取Excel中的数据。以下是我正在使用的代码:

使用Excel=Microsoft.Office.Interop.Excel;
Excel.Application xlApp=新的Excel.Application();
Excel.Workbook xlWorkbook=xlApp.Workbooks.Open(@“Book1.xlsx”,0,true,5,“,”,true,Excel.XlPlatform.xlWindows,“\t”,false,false,0,true,1,0);
Excel._工作表xlWorksheet=(Excel._工作表)xlWorkbook.Sheets[1];
Excel.Range xlRange=xlWorksheet.UsedRange;
int rowCount=xlRange.Rows.Count;
int colCount=xlRange.Columns.Count;

for(inti=1;i还没有测试过,但我认为应该是

MessageBox.Show(xlRange.Cells[i,j].ToString());
或者

MessageBox.Show(xlRange.Cells[i,j].Value.ToString());

我找到了上面的解决方案,下面是代码:

string temp = (string)(xlRange.Cells[i, j] as Excel.Range).Value2;
MessageBox.Show(temp);
请尝试以下代码:

MessageBox.Show(((Excel.Range)xlRange.Cells[i,j]).Value2.ToString());
此代码对我来说非常成功。

请尝试以下操作:

MessageBox.Show(xlRange.Cells[i][j].Value);

使用以下函数获取数据作为第N张工作表的DATATABLE对象:

public DataTable GetWorkSheet(int workSheetID)
    {
        string pathOfExcelFile = fileFullName;
        DataTable dt = new DataTable();

        try
        {
            excel.Application excelApp = new excel.Application();

            excelApp.DisplayAlerts = false; //Don't want Excel to display error messageboxes

            excel.Workbook workbook = excelApp.Workbooks.Open(pathOfExcelFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //This opens the file

            excel.Worksheet sheet = (excel.Worksheet)workbook.Sheets.get_Item(workSheetID); //Get the first sheet in the file

            int lastRow = sheet.Cells.SpecialCells(excel.XlCellType.xlCellTypeLastCell, Type.Missing).Row;
            int lastColumn = sheet.Cells.SpecialCells(excel.XlCellType.xlCellTypeLastCell, Type.Missing).Column;

            excel.Range oRange = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[lastRow, lastColumn]);//("A1",lastColumnIndex + lastRow.ToString());

            oRange.EntireColumn.AutoFit();


            for (int i = 0; i < oRange.Columns.Count; i++)
            {
                dt.Columns.Add("a" + i.ToString());
            }

            object[,] cellValues = (object[,])oRange.Value2;
            object[] values = new object[lastColumn];

            for (int i = 1; i <= lastRow; i++)
            {

                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    values[j] = cellValues[i, j + 1];
                }
                dt.Rows.Add(values);
            }

            workbook.Close(false, Type.Missing, Type.Missing);
            excelApp.Quit();
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

        }
        return dt;

    }
public DataTable GetWorkSheet(int-workSheetID)
{
字符串pathOfExcelFile=fileFullName;
DataTable dt=新的DataTable();
尝试
{
excel.Application excelApp=新的excel.Application();
excelApp.DisplayAlerts=false;//不希望Excel显示错误消息框
excel.工作簿工作簿=excelApp.Workbooks.Open(PathFexCelfile,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);//这将打开文件
excel.Worksheet sheet=(excel.Worksheet)workbook.Sheets.get_Item(workSheetID);//获取文件中的第一张工作表
int lastRow=sheet.Cells.SpecialCells(excel.XlCellType.xlCellTypeLastCell,Type.Missing)。行;
int lastColumn=sheet.Cells.SpecialCells(excel.XlCellType.xlCellTypeLastCell,Type.Missing).Column;
excel.Range oRange=sheet.get_范围(sheet.Cells[1,1],sheet.Cells[lastRow,lastColumn])/(“A1”,lastColumnIndex+lastRow.ToString());
oRange.EntireColumn.AutoFit();
对于(int i=0;i