Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Inserting records into a .xlsm excel workbook

$
0
0

Hi

I am trying to update an existing excel template using .aspx.cs file. I am using VS 2010 SP1 as the development environment. I have MS office 2010 and 2003 installed on my PC.

The code below works fine if the file is a plane .xls file with no macros in it. The code below insert records from a datatable starting from excel row A19.

Not sure if there is some setting that I am missing? Below is the snippet of the code -

                

                var excel = new Excel.Application();
                 excel.Application.Visible = false;
                 excel.UserControl = false;

                 object missing = System.Reflection.Missing.Value;

//full path is the location of the template file on local drive. This code works if there are no VBA macros in the file
                 var workbook = excel.Workbooks.Open(full_path, missing, false, missing, missing, missing,
                                                        missing, missing, missing, true, missing, missing,
                                                        missing, missing, missing);

                
                
                 Excel.Worksheet wrk_sheet;
                 wrk_sheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1);


                 //Array to hold the data from taskTable, taskTable has all the data from datatable
                 object[,] arr = new object[taskTable.Rows.Count, taskTable.Columns.Count];
                 for (int r = 0; r < taskTable.Rows.Count; r++)
                 {
                     DataRow dr = taskTable.Rows[r];
                     for (int c = 0; c < taskTable.Columns.Count; c++)
                     {
                         arr[r, c] = dr[c];
                     }
                 }
  
  
                 Excel.Range range = wrk_sheet.get_Range("B18", missing);

          //hardcoded the range for now
          range = range.get_Resize(47, 13);

          //assing array to excel range                                  
          range.set_Value(missing, arr);
                                
                 excel.Quit();
                 //excel = null;
                 System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>