2012-06-15 114 views
-2

我需要在c sharp中写一行来读取第一行的第一个单元格。在excel中设置第一行的第一个单元格

它是这样的: -

str=read(row[index]); 

是什么c中的确切说法尖锐?任何帮助?

+0

你能阅读吗 –

+0

在这里检查:http://stackoverflow.com/questions/4811664/set-cell-value-using-excel-interop –

+0

@COLDTOLD是啊..我可以读它...但我想将索引存储在一个字符串中...所以我可以像str [1],str [6]那样调用单元格。我如何设置索引?我的excel文件有几行。 –

回答

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) 
        { 
.... 
0

您可以通过使用Excel互操作的,如果安装了微软Office在您的PC或在VisualStudio的文件夹位置导入refrences后首次包括在你的C盘从OFFICE12或OFFICE14文件夹及其refrence读它的代码是

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(); 



    } 
相关问题