locked
Passing Data to a Cell in Excel RRS feed

  • Question

  • User522919602 posted

    Hello,

    I have a webpage that is created in Visual Studio 2008 and programmed in C#.

    I have a web form that prompts the user to enter a value into a text box.  For now, I do not have any validation on that field (I will be adding that later).

    I need to pass the value that the user has entered to cell C1 of the excel spreadsheet (user_info.xls).

    I understand that using excel is not ideal, but unforutnately need to try to make this work with the excel spreadsheet.

    I do not have any code for this yet, because I honestly do not know how to begin.

    The action of poulating the excel spreadsheet will be intialized from the On Click event of a Submit button.

    Any suggestions would be much appreciated.

    Thank you in advance for your assistance,


    Allison



    Tuesday, August 3, 2010 7:35 PM

Answers

  • User-624088818 posted

    Try below code to insert the data into cells of excel

    static void Main(string[] args)
            {
                excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
                object missing = Type.Missing;
                excel.Range xlRange;
                excel.Sheets xlSheets = null;
                excel.Worksheet xlSheet = null;
                excel.Workbook xlWorkbook = null;
    
                xlWorkbook = excelApp.Workbooks.Open("D:\\test.xls", missing, missing, missing,
                                                missing, missing, missing, missing, missing, missing, missing,
                                                missing, missing, missing, missing);
    
                xlSheets = (excel.Sheets)xlWorkbook.Sheets;
                xlSheet = (excel.Worksheet)xlSheets[1];
    
                for (int iRow = 1; iRow <= 3; iRow++)
                {
                    xlRange = (Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[iRow, 1];
                    xlRange.Value2 = "Cell"+iRow;
                    xlRange = (Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[iRow, 2];
                    xlRange.Value2 = "Cell" + iRow;
                    xlRange = (Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[iRow, 3];
                    xlRange.Value2 = "Cell" + iRow;
                    xlRange = (Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[iRow, 4];
                    xlRange.Value2 = "Cell" + iRow;
                }
                xlWorkbook.Save();
                xlWorkbook.Close(missing, missing, missing);
    }

    "Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 4, 2010 7:27 AM

All replies

  • User-624088818 posted

    Try below code to insert the data into cells of excel

    static void Main(string[] args)
            {
                excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
                object missing = Type.Missing;
                excel.Range xlRange;
                excel.Sheets xlSheets = null;
                excel.Worksheet xlSheet = null;
                excel.Workbook xlWorkbook = null;
    
                xlWorkbook = excelApp.Workbooks.Open("D:\\test.xls", missing, missing, missing,
                                                missing, missing, missing, missing, missing, missing, missing,
                                                missing, missing, missing, missing);
    
                xlSheets = (excel.Sheets)xlWorkbook.Sheets;
                xlSheet = (excel.Worksheet)xlSheets[1];
    
                for (int iRow = 1; iRow <= 3; iRow++)
                {
                    xlRange = (Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[iRow, 1];
                    xlRange.Value2 = "Cell"+iRow;
                    xlRange = (Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[iRow, 2];
                    xlRange.Value2 = "Cell" + iRow;
                    xlRange = (Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[iRow, 3];
                    xlRange.Value2 = "Cell" + iRow;
                    xlRange = (Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[iRow, 4];
                    xlRange.Value2 = "Cell" + iRow;
                }
                xlWorkbook.Save();
                xlWorkbook.Close(missing, missing, missing);
    }

    "Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 4, 2010 7:27 AM
  • User522919602 posted

    Thank you so much!!!

    This is great, I will give it a shot and let you know.


    Allison

    Wednesday, August 4, 2010 11:11 AM
  • User1941662514 posted

    Hi

    By the nice post, i really appciate it. can you translate it to Visual Basic not in C#

    Thursday, August 12, 2010 4:45 AM