473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

appending row in excel2007,c#

hi,
i need to open already existing excel file(.xlsx format),and add a row by collecting data from textboxes . am using code following. but am getting error at opening file:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using Excel=Microsoft.Office.Interop.Excel;
  11.  
  12. namespace Hand_Held_Data_Transporter
  13. {
  14.     public partial class AddAccount : Form
  15.     {
  16.  
  17.  
  18.         private static Microsoft.Office.Interop.Excel.Workbook mWorkBook;
  19.         private static Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
  20.         private static Microsoft.Office.Interop.Excel.Worksheet mWSheet1;
  21.         private static Microsoft.Office.Interop.Excel.Application oXL;
  22.         object Missing = System.Reflection.Missing.Value;
  23.  
  24.         public AddAccount()
  25.         {
  26.             InitializeComponent();
  27.         }
  28.  
  29.  
  30.         private void btnsave_Click(object sender, EventArgs e)
  31.         {
  32.  
  33.             oXL = new Microsoft.Office.Interop.Excel.Application();
  34.             oXL.Visible = true;
  35.            oXL.DisplayAlerts = true;
  36.             //error on this line
  37.             mWorkBook = oXL.Workbooks.Open(txtbrowse.Text, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
  38.            // mWorkBook = oXL.Workbooks.Open(txtbrowse.Text);
  39.  
  40.             //Get all the sheets in the workbook
  41.             mWorkSheets = mWorkBook.Worksheets;
  42.  
  43.             //Get the allready exists sheet
  44.             mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");
  45.  
  46.             Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;
  47.  
  48.             int colCount = range.Columns.Count;
  49.             int rowCount = range.Rows.Count;
  50.  
  51.             //for (int index = 1; index < 15; index++)
  52.             //{
  53.             mWSheet1.Cells[rowCount + 1, 1] = txtid.Text;
  54.             mWSheet1.Cells[rowCount + 1, 2] = txtname.Text;
  55.             mWSheet1.Cells[rowCount + 1, 3] = txtplace.Text;
  56.             mWSheet1.Cells[rowCount + 1, 8] = txtdue.Text;
  57.  
  58.             //}
  59.  
  60.             mWorkBook.SaveAs(txtbrowse.Text, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
  61.             Missing, Missing, Missing, Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
  62.             Missing, Missing, Missing,
  63.             Missing, Missing);
  64.  
  65.             //mWorkBook.Close(Missing, Missing, Missing);
  66.            // mWSheet1 = null;
  67.  
  68.            // mWorkBook = null;
  69.  
  70.             //oXL.Quit();
  71.             GC.WaitForPendingFinalizers();
  72.             GC.Collect();
  73.             GC.WaitForPendingFinalizers();
  74.             GC.Collect();
  75. }
  76.  
  77.  
  78.         private void btnbrowse_Click(object sender, EventArgs e)
  79.         {
  80.             OpenFileDialog fdlg = new OpenFileDialog();
  81.             fdlg.Filter = "All Files(*.*)|*.*";
  82.             if (fdlg.ShowDialog() == DialogResult.OK)
  83.             {
  84.                 txtbrowse.Text = fdlg.FileName;
  85.                 File.ReadAllText(txtbrowse.Text);
  86.  
  87.             }
  88.         }
  89.  
  90.  
  91.  
  92.  
  93.     }
  94. }
  95.  
Feb 17 '14 #1
0 1126

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bharat Bhushan | last post by:
Hi, I need to generate random bytes for x number of times and keep appending it to a bigger byte array. How can I do this ? for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){ ...
3
by: ketulp_baroda | last post by:
Hi I am having a problem in appending to sys.path I am doing it this way: >>> sys.path >>> sys.path.append(r'D:\Python23\Lib\site-packages\code') >>> sys.path But when I close the shell...
4
by: bucket79 | last post by:
Hi is there anyway appending to dictionary? list has this feature >>>a = >>>a.append(1) >>>print a but dictionary can't
1
by: Thomas Heller | last post by:
I want to append/insert additional data to an xml file. Context: I use gccxml to parse C header files. gccxml creates an xml file containing all the definitions from the header files. The xml...
1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
3
by: MLH | last post by:
I have a query, qryAppend30DayOld260ies that attempts to append records to tblCorrespondence. When run, it can result in any of the following: appending no records, appending 1 record or appending...
4
by: John A Grandy | last post by:
could someone explain the following to me : Appending the literal type character I to a literal forces it to the Integer data type. Appending the identifier type character % to any identifier...
1
by: Frank | last post by:
Hi, Let's say I have a file named myFile.xml Within that file I have blocks of data which I'd like to add at different times during the day. e.g. <LogEntry>
2
by: sarada purkait | last post by:
hii i have to write into a file from the start and then go on appending to it .. i tried using ( ios::out|ios::app) but by this the file keeps on appending every time i run the program and the...
1
by: JaheerHussain | last post by:
Hi This Is Zahe,Here in my Page is Displayed some Information on TextBox,TextBox property is set Multiline,Information has some fields,but here all information are unformated,my need is, i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.