C#: How to access an Excel cell?

后端 未结 7 1089
忘掉有多难
忘掉有多难 2020-12-05 18:25

I am trying to open an Excel file and populate its cells with data? I have done the following coding so far.

Currently I am at this stage with the following code but

相关标签:
7条回答
  • 2020-12-05 19:02

    I think, that you have to declare the associated sheet!

    Try something like this

    objsheet(1).Cells[i,j].Value;
    
    0 讨论(0)
  • 2020-12-05 19:03

    Simple.

    To open a workbook. Use xlapp.workbooks.Open()

    where you have previously declared and instanitated xlapp as so.. Excel.Application xlapp = new Excel.Applicaton();

    parameters are correct.

    Next make sure you use the property Value2 when assigning a value to the cell using either the cells property or the range object.

    0 讨论(0)
  • 2020-12-05 19:10

    How I work to automate Office / Excel:

    1. Record a macro, this will generate a VBA template
    2. Edit the VBA template so it will match my needs
    3. Convert to VB.Net (A small step for men)
    4. Leave it in VB.Net, Much more easy as doing it using C#
    0 讨论(0)
  • 2020-12-05 19:12

    You can use the below code; it's working fine for me:

    newWorkbook = appExcel.Workbooks.Add();
    
    0 讨论(0)
  • 2020-12-05 19:23

    Try:

    Excel.Application oXL;
    Excel._Workbook oWB;
    Excel._Worksheet oSheet;
    Excel.Range oRng;
    
    oXL = new Excel.Application();
    oXL.Visible = true;
    oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
    
    oSheet = (Excel._Worksheet)oWB.Worksheets;
    oSheet.Activate();
    
    oSheet.Cells[3, 9] = "Some Text"
    
    0 讨论(0)
  • 2020-12-05 19:25

    This works fine for me

           Excel.Application oXL = null;
            Excel._Workbook oWB = null;
            Excel._Worksheet oSheet = null;
    
            try
            {
                oXL = new Excel.Application();
                string path = @"C:\Templates\NCRepTemplate.xlt";
                oWB = oXL.Workbooks.Open(path, 0, false, 5, "", "",
                    false, Excel.XlPlatform.xlWindows, "", true, false,
                    0, true, false, false);
    
                oSheet = (Excel._Worksheet)oWB.ActiveSheet;
                oSheet.Cells[2, 2] = "Text";
    
    0 讨论(0)
提交回复
热议问题